Hi
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? I'm desperate. Please help. Thanks!
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>
And the ruby piece I use:
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