2010-11-22 56 views
1

我需要做的就是驗證XML片段:XSD:相同的元素,不同的屬性

<token type="email">qwqe12e12e1</token> 
<token type="mobile">12e12313121w</token> 

我知道如何驗證這些元素具有屬性和內容,但我的問題是如何防止令牌鍵入例如電子郵件發生不止一次?我需要兩個令牌,但每個令牌只能出現一次。

回答

0

鑑於以下XML示例:

<tokens> 
    <token type="email">qwqe12e12e1</token> 
    <token type="mobile">12e12313121w</token> 
    <token type="mobile">1234</token> 
</tokens> 

和以下XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="tokens"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element ref="token" maxOccurs="unbounded" /> 
     </xs:sequence> 
    </xs:complexType> 
    <!-- token[type] to only occur once --> 
    <xs:unique name="type"> 
     <xs:selector xpath="token" /> 
     <xs:field xpath="@type" /> 
    </xs:unique> 
    </xs:element> 

    <xs:element name="token"> 
    <xs:complexType> 
     <xs:simpleContent> 
     <xs:extension base="xs:string"> 
      <xs:attribute name="type" use="required"> 
      <xs:simpleType> 
       <xs:restriction base="xs:string"> 
       <xs:enumeration value="email"/> 
       <xs:enumeration value="mobile"/> 
       </xs:restriction> 
      </xs:simpleType> 
      </xs:attribute> 
     </xs:extension> 
     </xs:simpleContent> 
    </xs:complexType> 
    </xs:element> 
</xs:schema> 

的驗證將失敗上的第二個 「移動」 記號類型。

當從輸入XML中移除第二個「移動」令牌類型時,驗證將成功。

相關問題