2010-06-18 142 views
0

我試圖創建一個XML文件,它看起來像下面的XML模式元素XSD與具有相同名稱的

<attributes> 
<attribute name="article_artextref">123213213</attribute> 
<attribute name="ProviderID">ABC</attribute> 
</attributes> 

我試圖做到的是檢查,如果屬性名爲「article_artextref」存在,請確保它的值長度大於1.我不想驗證屬性名稱「ProviderID」的長度,提供者ID的長度可以爲0.

請幫忙。

我正在添加到目前爲止檢查這兩個元素的長度的xml模式。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:simpleType name="ST_attribute"> 
     <xs:restriction base="xs:string"> 
      <xs:minLength value="1"/> 
     </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="attributes"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="attribute" maxOccurs="unbounded"/> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 
    <xs:element name="attribute"> 
     <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="ST_attribute"> 
        <xs:attribute name="name" use="required"> 
         <xs:simpleType> 
          <xs:restriction base="xs:string"> 
           <xs:enumeration value="ProviderID"/> 
           <xs:enumeration value="article_artextref"/> 
          </xs:restriction> 
         </xs:simpleType> 
        </xs:attribute> 
       </xs:extension> 
      </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 
</xs:schema> 

回答

0

很抱歉,XML Schema無法處理類似的事情。如果你想要不同的元素內容,你需要不同的元素名稱。

+0

我剛剛在我最初的問題中添加了架構。請檢查一下。我正在檢查article_artextref和Provider ID的長度。 – 2010-06-18 22:30:19

+0

您的XSD所做的只是驗證「name」屬性的值。您不驗證「屬性」元素的內容,並且您當然不會根據「name」屬性驗證內容是否具有不同的類型。 XML Schema不能做這些事情。 – 2010-06-18 22:47:31