验证XML:验证根元素时没有匹配的全局声明。

9
我正在尝试使用Ruby对以下XML文件进行XSD模式验证。 但是它根本无法工作,会停止并显示错误消息,告诉我:

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

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

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
3个回答

10

这是一个晦涩的错误提示,但很可能是因为您的XSD文件格式不正确。例如,channel、hotel(内部和外部元素)、room和request的xsd:element标记的内容都应该包含在xsd:complexType标记中。此外,use属性仅适用于xsd:attribute,而不适用于xsd:element。对于元素,请使用minOccurs和maxOccurs属性(尽管它们默认值均为1,在此情况下不一定需要)。此外,您的外部hotel元素包含一个room元素,而该room元素必须包含一个hotel元素,从而创建了一个无限循环。此外,您未正确命名用户名和密码元素。最后,那个内部hotel元素可能应该是date。这是我认为您正在寻找的内容:

<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
Pesto - 非常感谢您的回复。我遇到了与Matt相同的错误,并且很难诊断出根本原因。您的帖子非常有道理,让我解决了这个问题。谢谢。 - Elliot

2

我因为不同的原因收到了相同的晦涩错误信息。

我的模式文件的第一行有一个未加前缀的命名空间:

<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">

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


2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接