中唯一我有這樣的XSD約束ID屬性是XSD文件
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/XXX"
xmlns:tns="http://www.example.org/XXX" elementFormDefault="qualified">
<simpleType name="Name">
<restriction base="string"></restriction>
</simpleType>
<simpleType name="Phone">
<restriction base="string">
<pattern value="\d+"></pattern>
</restriction>
</simpleType>
<simpleType name="Id">
<restriction base="string">
<pattern value="[a-zA-Z]\w+"></pattern>
</restriction>
</simpleType>
<complexType name="User">
<attribute name="Id" type="tns:Id" use="required"></attribute>
<attribute name="Name" type="tns:Name" use="required"></attribute>
<attribute name="Phone" type="tns:Phone" use="required"></attribute>
</complexType>
<complexType name="ROOT">
<sequence>
<element name="User" type="tns:User" minOccurs="0" maxOccurs="unbounded">
<key name="idUser">
<selector xpath="User"></selector>
<field xpath="@Id"></field>
</key>
</element>
</sequence>
</complexType>
<element name="root" type="tns:ROOT">
</element>
</schema>
,我想限制ID字段使得它獨特里面沒有這個文件,但只是樹的一個分支內。顯然在這個例子中沒有任何變化,但是我的想法是,我可以在根元素內部有一系列服務器,並且對於每個服務器,用戶必須是唯一的,但同時可以存在具有相同Id的兩個用戶,如果他們不屬於同一臺服務器。 例如,如果只有s1和s2,這個文檔將是有效的,但它是無效的,因爲s3有兩個內部具有相同id的用戶。
<?xml version="1.0" encoding="UTF-8"?>
<tns:XXX xmlns:tns="http://www.example.org/XXX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/XXX schema.xsd ">
<tns:Server Id="s1">
<tns:User Id="u1" Name="Name" Phone="12" />
<tns:User Id="u2" Name="Name1" Phone="87654" />
</tns:Server Id="s2">
<tns:Server Id="a2">
<tns:User Id="u2" Name="Name" Phone="12" />
<tns:User Id="u4" Name="Name1" Phone="87654" />
</tns:Server>
<tns:Server Id="s3">
<tns:User Id="u2" Name="Name" Phone="12" />
<tns:User Id="u2" Name="Name1" Phone="87654" />
</tns:Server>
</tns:ACSInfo>
我試着用鑰匙標籤,但如果我嘗試驗證這個xml它通過,我希望它不能!
<?xml version="1.0" encoding="UTF-8"?>
<tns:XXX xmlns:tns="http://www.example.org/XXX" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/XXX schema.xsd ">
<tns:User Id="a1" Name="Name" Phone="12" />
<tns:User Id="a1" Name="Name1" Phone="87654" />
</tns:XXX>
如果它對你有幫助,請給予回覆/獎勵答案。 –