2017-03-09 29 views
-1

我正在學習xml和schema.I想要一個模式,其中電話元素值必須是唯一的。我嘗試獨特但不能理解它是如何工作的。 對不起,這個愚蠢的問題,但我正在學習。我想要電話元素的唯一值

XML

<?xml version="1.0" encoding="utf-8" ?> 
<Company xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <Employee> 
    <Name>ABC</Name> 
    <Telephone>9998887770</Telephone> 
    </Employee> 
    <Employee> 
     <Name>DEF</Name> 
     <Telephone>9998887770</Telephone> 
    </Employee> 
    <Employee> 
     <Name>GHI</Name> 
     <Telephone>1234567890</Telephone> 
    </Employee> 
</Company> 

模式

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema id="EmployeeSchema" 
     elementFormDefault="qualified" 
     attributeFormDefault="unqualified" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

    <xs:element name="Name"/> 
    <xs:element name="Telephone" /> 

    <xs:simpleType name="string32"> 
     <xs:restriction base="xs:token"> 
     <xs:maxLength value="32"/> 
    </xs:restriction> 

    <xs:element name="Company"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="Employee" maxOccurs="unbounded"/> 
      </xs:sequence> 
     </xs:complexType> 
    <!--Here i try to implement unique---> 
     <xs:unique name="Company"> 
      <xs:selector xpath="Telephone"/> 
      <xs:field xpath="Telephone"/> 
     </xs:unique> 
    </xs:element> 

     <xs:element name="Employee"> 
      <xs:complexType> 
      <xs:sequence>  
       <xs:element ref="Name"/> 
       <xs:element ref="Telephone"/>  
      </xs:sequence> 
     </xs:complexType> 
     </xs:element> 
</xs:schema> 
+0

「我嘗試獨特但不明白它是如何工作的」。你讀過什麼書?我會推薦Priscilla Walmsley's。 –

+0

我從**學習 「XML模式由Eric範德瓦VLIST」 ** @MichaelKay –

回答

1

嘗試

<xs:unique name="Company-Employee-Phone"> 
    <xs:selector xpath="Employee"/> 
    <xs:field xpath="Telephone"/> 
</xs:unique> 

的規則是這樣的:如果你在X中希望每Y個有獨特價值爲Z,然後defin e在X的定義中約束xs:unique,使用從X中選擇Y作爲xs:selector的路徑,以及從Y中選擇Z作爲xs:field的路徑。

+0

** __我在這裏寫你的代碼,但仍然不工作_ ** –

+0

然後再次閱讀我的答案。 X =公司,Y =員工,Z =電話,所以約束在X =公司。 –

相關問題