【问题标题】:Validating XML: No matching global declaration available for the validation root验证 XML:没有可用于验证根的匹配全局声明
【发布时间】:2009-06-17 14:37:52
【问题描述】:

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

错误:元素“请求”:没有可用于验证根的匹配全局声明。

也许是命名空间?有什么想法吗?

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

【问题讨论】:

    标签: ruby xml xsd libxml2


    【解决方案1】:

    这是一个神秘的错误,但可能是因为您的 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>
    

    【讨论】:

    • 就是这样。我猜我写 XSD 的时候太累了。感谢您的帮助!
    • 香蒜酱 - 非常感谢您的回复。我收到了与 Matt 相同的错误,并且无法诊断根本原因。你的帖子很有意义,让我解决了这个问题。谢谢。
    【解决方案2】:

    这里只是从臀部拍摄,但是您是否尝试过将包含架构的 XML::Document 转换为 XML::Schema?

    http://libxml.rubyforge.org/rdoc/classes/LibXML/XML/Schema.html

    我不知道这会有所作为,但值得一试。

    【讨论】:

      【解决方案3】:

      出于不同的原因,我收到了相同的神秘错误消息。

      我的架构文件的第一行有一个不带前缀的命名空间:

      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.sec.gov/edgar/document/thirteenf/informationtable" xmlns:ns1="http://www.sec.gov/edgar/common" targetNamespace="http://www.sec.gov/edgar/document/thirteenf/informationtable" elementFormDefault="qualified" attributeFormDefault="unqualified">
      

      注意“xmlns=”属性。这会将架构中声明的所有元素放入命名空间http://www.sec.gov/edgar/document/thirteenf/informationtable(除非另外指定命名空间前缀)。但我试图验证的 XML 文件没有匹配的无前缀/默认命名空间:

      <informationTable xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      

      因此它的元素与架构不匹配,因为它们位于“不同”的命名空间中。我希望这对其他人有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-19
        • 1970-01-01
        • 1970-01-01
        • 2012-01-15
        • 2021-02-26
        • 2013-02-16
        相关资源
        最近更新 更多