2011-03-20 70 views
0

我有xsd和xml文件。在验证关闭时xml很好地解析。 但用xsd验证它抱怨xsd中的根元素为空。xom xsd验证失败,因为根元素为空

  1. 我的xsd文件有多个全局元素。所以基本上这可能是一个问题。 我想从xsd中,XOM将根元素视为null。如果你能在它确认

  2. 如何声明的XSD文件和根元素什么做的最好的办法,在XSD限制全局元素,只是1元does not看起来对我好


<?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> 

回答

0

你的架构基本上是好的,只知道它允许最多一个< entry>元素;你可能想在这一点上maxOccurs =“unbounded”。

但是,要解决您的问题,我们需要了解更多关于如何设置解析/验证以及使用哪些工具的信息。如果你使用xom.nu,确保你传递一个名称空间感知的,架构验证XmlReader到Builder instance

+0

是的我需要改变它maxOccurs无界但这不是问题在这一点上。为验证我创建一个生成器实例传递TRUE作为参数,我猜是哪些设置它是验证生成器。我传递给构建器的xml文件具有schemaLocation atrribute,因此应该进行验证。但生成器抱怨模式中的根元素为null,而xml文件的根元素为adress_book,这是一个未命中匹配,因此错误。所以我认为问题在于模式中的多个全局元素,因此它不会选择根元素。请分享这里可能会出错的地方 – nBhati 2011-03-24 18:51:07

+0

我认为这会创建一个解析器来验证DTD。在这里看看如何创建一个解析器来验证模式:http://www.xom.nu/faq.xhtml#d0e390 – 2011-03-25 15:20:54

+0

感谢大家指出这一点....我在其他前面努力奋斗,没有运气 – nBhati 2011-03-26 13:52:16

-1

,请注意以下属性: XSI:的schemaLocation =“HTTP://www.popcornmonsters .com/address_book.xsd“ 它应该是一对值:URI和文件名。因此,address_book.xsd之前的空格是必填空格之一: xsi:schemaLocation =“http://www.popcornmonsters.com/ address_book.xsd” 没有空间,没有与XML文档关联的模式。

+0

这并没有回答这个问题,并且是不正确的,因为这两种格式都可以接受,具体取决于具体情况。 – psubsee2003 2012-10-21 09:23:51