微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ruby – 验证XML:验证根目录没有可用的匹配全局声明

我正在尝试使用 Ruby针对XSD架构验证以下XML.
它根本不起作用,停止并显示错误消息

Error: Element ‘request’: No matching global declaration available for the validation root.

也许它是命名空间?有任何想法吗?

XML

<?xml version="1.0" encoding="UTF-8"?>
<request type="test" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <channel name="channel">
    <username>user</username>
    <password>pass</password>
  </channel>

  <hotel id="1">
    <date from="2009-07-07" to="2009-07-17"/>
    <room id="1">
      <allocation>10</allocation>
    </room>
  </hotel>
</request>

XSD

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <!-- channel -->
  <xsd:element name="channel">
    <xsd:attribute name="name" use="required" type="xsd:string" />
    <xsd:sequence>
      <xsd:element username="name" use="required" type="xsd:string"/>
      <xsd:element password="country" use="required" type="xsd:string"/>
    </xsd:sequence>
  </xsd:element>

  <!-- hotel -->
  <xsd:element name="hotel">
    <xsd:attribute name="id" use="required" type="xsd:string" />
    <xsd:sequence>
      <xsd:element name="hotel">
        <xsd:attribute name="from" use="required" type="xsd:string" />
        <xsd:attribute name="to" use="required" type="xsd:string" />
      </xsd:element>
      <xsd:element ref="room" minOccurs="1"/>
    </xsd:sequence>
  </xsd:element>


  <!-- room -->
  <xsd:element name="room">
    <xsd:sequence>
      <xsd:element name="allocation" type="xsd:string"></xsd:element>
      <xsd:element ref="hotel" minOccurs="1"/>
    </xsd:sequence>
    <xsd:attribute name="id" use="required" type="xsd:string" />
  </xsd:element>

  <!-- building all together -->
  <xsd:element name="request">
    <xsd:attribute name="type" use="required" type="xsd:string" />
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="channel" maxOccurs="1"/>
        <xsd:element ref="hotel" maxOccurs="1"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Ruby代码

require "xml"

document = LibXML::XML::Document.file("/tmp/test.xml")
schema = LibXML::XML::Document.file("/tmp/request.xsd")

result = document.validate_schema(schema) do |message,flag|
  log.debug(message)
  puts message
end

解决方法

这是一个神秘的错误,但可能是因为你的XSD格式不正确.例如,渠道,酒店(内部和外部元素),房间和请求xsd:element标签内容都应包含在xsd:complexType标签中.此外,use仅对xsd:attribute有效,而不对xsd:element有效.对于元素,使用minOccurs和maxOccurs(尽管两者都认为1,因此在这种情况下它们实际上不是必需的).此外,您的外部酒店元素包含一个房间元素,必须包含一个酒店元素,创建一个无限循环.此外,您没有正确命名您的用户名和密码元素.最后,内部酒店元素应该是约会.以下是我认为您正在寻找的内容
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

  <!-- channel -->
  <xsd:element name="channel">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="username" type="xsd:string"/>
        <xsd:element name="password" type="xsd:string"/>
      </xsd:sequence>
      <xsd:attribute name="name" use="required" type="xsd:string" />
    </xsd:complexType>
  </xsd:element>

  <!-- hotel -->
  <xsd:element name="hotel">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="date">
          <xsd:complexType>
            <xsd:attribute name="from" use="required" type="xsd:string" />
            <xsd:attribute name="to" use="required" type="xsd:string" />
          </xsd:complexType>
        </xsd:element>
        <xsd:element ref="room" minOccurs="1"/>
      </xsd:sequence>
      <xsd:attribute name="id" use="required" type="xsd:string" />
    </xsd:complexType>
  </xsd:element>


  <!-- room -->
  <xsd:element name="room">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="allocation" type="xsd:string"></xsd:element>
      </xsd:sequence>
      <xsd:attribute name="id" use="required" type="xsd:string" />
    </xsd:complexType>
  </xsd:element>

  <!-- building all together -->
  <xsd:element name="request">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element ref="channel" maxOccurs="1"/>
        <xsd:element ref="hotel" maxOccurs="1"/>
      </xsd:sequence>
    <xsd:attribute name="type" use="required" type="xsd:string" />
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

原文地址:https://www.jb51.cc/ruby/270710.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐