2012-03-26 53 views
0

有人可以幫助我如何爲此定義一個xsd。自反方案驗證和定義

我還希望在模式級強制進行驗證,以便根據給定的屬性值僅顯示相應的子標記(基於屬性值自反)。

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
    <sample-tag type="one"> 
    <one></one> 
</sample-tag> 
<sample-tag type="two"> 
    <two></two> 
</sample-tag> 
<sample-tag type="three"> 
    <three></three> 
</sample-tag> 
<sample-tag type="onetwo"> 
    <one></one> 
    <two></two> 
</sample-tag> 
</root> 

回答

0

如果我將您的示例XML作爲近似值,嚴格來說,有一種方法可以實現您想要的效果:稱爲xsi:type。我個人不喜歡xsi:type,所以我認爲這個答案而不是學術練習。

如果這個XML同意:

<?xml version="1.0" encoding="UTF-8"?> 
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <sample-tag xsi:type="one"> 
     <one></one> 
    </sample-tag> 
    <sample-tag xsi:type="two"> 
     <two></two> 
    </sample-tag> 
    <sample-tag xsi:type="three"> 
     <three></three> 
    </sample-tag> 
    <sample-tag xsi:type="onetwo"> 
     <one></one> 
     <two></two> 
    </sample-tag> 
</root> 

...那麼這個XSD會給你想要的東西:

<?xml version="1.0" encoding="utf-8"?> 
<!--W3C Schema generated by QTAssistant/W3C Schema Refactoring Module (http://www.paschidev.com)--> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="root"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element maxOccurs="unbounded" name="sample-tag" type="sample-tag"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
    <xsd:complexType name="sample-tag" abstract="true"/> 
    <xsd:complexType name="one"> 
     <xsd:complexContent> 
      <xsd:extension base="sample-tag"> 
       <xsd:sequence> 
        <xsd:element name="one"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 
    <xsd:complexType name="two"> 
     <xsd:complexContent> 
      <xsd:extension base="sample-tag"> 
       <xsd:sequence> 
        <xsd:element name="two"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 
    <xsd:complexType name="three"> 
     <xsd:complexContent> 
      <xsd:extension base="sample-tag"> 
       <xsd:sequence> 
        <xsd:element name="three"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 
    <xsd:complexType name="onetwo"> 
     <xsd:complexContent> 
      <xsd:extension base="sample-tag"> 
       <xsd:sequence> 
        <xsd:element name="one"/> 
        <xsd:element name="two"/> 
       </xsd:sequence> 
      </xsd:extension> 
     </xsd:complexContent> 
    </xsd:complexType> 
</xsd:schema> 

如果XSI:類型是毫無疑問的,和XSD 1.1 ,那麼你必須去@Pangea的答案。