9

我正在尝试使用 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>

红宝石代码

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
4

3 回答 3

10

这是一个神秘的错误,但可能是因为您的 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>
于 2009-06-17T15:20:30.700 回答
2

在这里只是从臀部拍摄,但是您是否尝试过将保存模式的 XML::Document 转换为 XML::Schema?

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

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

于 2009-06-17T15:15:53.550 回答
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">

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

于 2015-02-12T17:08:13.203 回答