xom xsd 验证失败,因为根元素为 null
我有 xsd 和 xml 文件。当验证关闭时,xml 解析正常。 但通过 xsd 验证,它会抱怨 xsd 中的根元素为空。
我的 xsd 文件有多个全局元素。所以基本上这可能是一个问题。 我猜从xsd,XOM将根元素设为null。如果您可以确认
,如何在xsd文件中声明根元素以及最好的方法是什么,在xsd中将全局元素限制为只有1个元素对我来说看起来不太好
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.popcornmonsters.com/"
xmlns="http://www.popcornmonsters.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="address_book" >
<xs:complexType>
<xs:sequence>
<xs:element ref="entry" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="email" type="xs:string"/>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
<xs:element name="entry">
<xs:complexType>
<xs:sequence>
<xs:element ref="first_name" minOccurs="0"/>
<xs:element ref="last_name" minOccurs="0"/>
<xs:element ref="email" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<address_book xmlns="http://www.popcornmonsters.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd">
<entry>
<first_name>Ken</first_name>
<last_name>Cochrane</last_name>
<email>[email protected]</email>
</entry>
<entry>
<first_name>Emily</first_name>
<last_name>Cochrane</last_name>
<email>[email protected]</email>
</entry>
</address_book>
i have xsd and xml file. xml parse fine when validation is turned off.
but with xsd validation it complains about root element in xsd being null.
my xsd file is having multiple global elements. so basically this can be a problem.
i guess from xsd,XOM take root element as null. if you can confirm on ithow to declare root element in xsd file and whats best way to do it, in xsd restricting global elements to just 1 element doesnt look good to me
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://www.popcornmonsters.com/"
xmlns="http://www.popcornmonsters.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="address_book" >
<xs:complexType>
<xs:sequence>
<xs:element ref="entry" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="email" type="xs:string"/>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
<xs:element name="entry">
<xs:complexType>
<xs:sequence>
<xs:element ref="first_name" minOccurs="0"/>
<xs:element ref="last_name" minOccurs="0"/>
<xs:element ref="email" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?>
<address_book xmlns="http://www.popcornmonsters.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd">
<entry>
<first_name>Ken</first_name>
<last_name>Cochrane</last_name>
<email>[email protected]</email>
</entry>
<entry>
<first_name>Emily</first_name>
<last_name>Cochrane</last_name>
<email>[email protected]</email>
</entry>
</address_book>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的模式基本上是好的,只是它最多允许一个。元素;此时您可能需要 maxOccurs="unbounded" 。
但是,为了解决您的问题,我们需要更多地了解您如何设置解析/验证以及您使用的工具。如果您使用的是 xom.nu,请确保将命名空间感知、模式验证 XMLReader 传递给 构建器实例,
Your schema is basically Ok, only that it allows at most one <entry> element; you probably want maxOccurs="unbounded" at that point.
However, to solve your problem we need to know more about how you set up the parsing/validation and what tools you use. If it is xom.nu you're using, make sure you pass a namespace aware, schema validating XMLReader to the Builder instance,
请注意以下属性:
xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd"
它应该是一对值:URI 和文件名。因此,address_book.xsd 之前的空格是强制性的:
xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd"
如果没有空格,则没有与 XML 文档关联的模式。
Pay attention to the following attribute:
xsi:schemaLocation="http://www.popcornmonsters.com/address_book.xsd"
it's supposed to be a pair of values: URI and file name. So a space before address_book.xsd is mandatory one:
xsi:schemaLocation="http://www.popcornmonsters.com/ address_book.xsd"
Without space there is no schema associated with XML document.