9

I'm trying to validate the following XML against a XSD schema using Ruby. It simply won't work, stops with an error message telling me

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

Maybe it's the namespace? Any ideas?

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 code

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

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 に答える