需要XSD基本帮助 - 模式中的命名空间?

问题描述:

我有像下面的XML文件的东西。需要XSD基本帮助 - 模式中的命名空间?

<credits> 
    <property name="tag"> 
     <item>v0003</item> 
    </property> 
    <property 
     name="tag"> 
     <item>mhma03h</item> 
    </property> 
</credits> 

第一个要求是这个XML无论如何都不能改变。请不要在下面做这个。

我需要写一个模式来验证这一点。和验证的Java代码。

我完全被卡住了,我不知道到底发生了什么。

什么是这样的模式?我已经得到一个,但它是如此糟糕,我不打扰张贴。 :P我不想将名称空间追加到XML元素。它们设置在石头上。^ _^

我该如何制作它,以便所有这些元素都是解析器“发现”的?我可以告诉它忽略所有这个命名空间的废话。通过这种对模式进行验证的应用程序,命名空间冲突根本不可能。

我试图把

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="books" elementFormDefault="unqualified"> 

我的命名空间信息和

更新:我已经更新我在做什么,以反映目前给出的答案! :)

XSD

<?xml version="1.0" encoding="UTF-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="property"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="item"/> 
     </xs:sequence> 
     <xs:attribute name="name" use="required"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
        <xs:enumeration value="tag"/> 
       </xs:restriction> 
      </xs:simpleType> 
     </xs:attribute> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="item"> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="mhma03h"/> 
      <xs:enumeration value="v0003"/> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:element> 
    <xs:element name="credits"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="property" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

XML V0003

代码加载和验证肯定。在你问之前,文件能够被加载。我检查过20次。 :P

SAXParserFactory factory = SAXParserFactory.newInstance(); 

     SchemaFactory schemaFactory = 
      SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema"); 

     factory.setSchema(schemaFactory.newSchema(
      new Source[] {new StreamSource("small.xsd")})); 


     javax.xml.parsers.SAXParser parser = factory.newSAXParser(); 

     org.xml.sax.XMLReader reader = parser.getXMLReader(); 
     reader.setFeature("http://xml.org/sax/features/validation", true); 
     reader.setFeature("http://apache.org/xml/features/validation/schema", true); 
reader.parse(new InputSource("small.xml")); 

这里是对应于您发布的XML文件的模式:

<?xml version="1.0" encoding="UTF-8"?> 
<!--W3C Schema generated by XMLSpy v2011 sp1 (http://www.altova.com)--> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="property"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="item"/> 
     </xs:sequence> 
     <xs:attribute name="name" use="required"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
        <xs:enumeration value="tag"/> 
       </xs:restriction> 
      </xs:simpleType> 
     </xs:attribute> 
    </xs:complexType> 
    </xs:element> 
    <xs:element name="item"> 
    <xs:simpleType> 
     <xs:restriction base="xs:string"> 
      <xs:enumeration value="mhma03h"/> 
      <xs:enumeration value="v0003"/> 
     </xs:restriction> 
    </xs:simpleType> 
    </xs:element> 
    <xs:element name="credits"> 
    <xs:complexType> 
     <xs:sequence> 
      <xs:element ref="property" maxOccurs="unbounded"/> 
     </xs:sequence> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

这可能不是正确捕捉每个元素和属性的要求(检查最小/最大时,必需/可选proprerties等)设置正确),但它应该让您开始使用将正确验证的XML模式。该架构没有定义目标名称空间,因此您不必担心修改现有XML以将名称空间前缀添加到现有元素。

+0

我通过我的java验证代码运行了你的模式,并得到这个异常。 cvc-elt.1:找不到元素'xs:schema'的声明。是否有特定的设置或我需要设置在我的SAXParser对象上的东西不会失败? – bobber205 2011-03-04 21:44:54

+0

这个错误是因为我把.xsd放在了我应该放的地方.xsl xD – bobber205 2011-03-04 21:50:09

您需要找到一种方法来告诉XML处理器在处理没有显式名称空间的文档时使用您的名称空间作为默认名称空间。大多数处理器有办法做到这一点,IIRC有一个名为setNoNamespaceSchema或类似的方法。您将编写一个带有名称空间的XML模式,并告诉处理器(验证器,无论如何)使用您的名称空间来处理没有明确命名空间的文档。

+0

如何用“命名空间”写一个模式? – bobber205 2011-03-04 21:52:06

+0

哦,在'xs:schema'元素中包含['targetNamespace'](http://www.w3.org/TR/2004/REC-xmlschema-0-20041028/#UnqualLocals)属性。 – 2011-03-04 22:02:38

+0

好的。我添加了“targetNamespace = books”。我正在使用SAXParser。我可以设置什么选项? :D – bobber205 2011-03-04 23:23:02