gpt4 book ai didi

ruby - 验证 XML : No matching global declaration available for the validation root

转载 作者:数据小太阳 更新时间:2023-10-29 01:44:42 28 4
gpt4 key购买 nike

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

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

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

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

最佳答案

这是一个神秘的错误,但可能是因为您的 XSD 格式不正确。例如channel、hotel(内外层元素)、room、request等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>

关于ruby - 验证 XML : No matching global declaration available for the validation root,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1007454/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com