2011-03-04 66 views
1

我有像下面的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")); 

回答

2

這裏是對應於您發佈的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

-1

您需要找到一種方法來告訴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