问题
I am learning xml and schema.I want a schema where Telephone element value have to be unique. I try with unique but can't understand how it's work. Sorry for this silly question but i am learning.
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>
schema
<?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>
回答1:
Try
<xs:unique name="Company-Employee-Phone">
<xs:selector xpath="Employee"/>
<xs:field xpath="Telephone"/>
</xs:unique>
The rule is this: if you want every Y within an X to have a distinct value for Z, then define the xs:unique
constraint in the definition of X, use a path that selects Y from X as the xs:selector
, and a path that selects Z from Y as the xs:field
.
来源:https://stackoverflow.com/questions/42689135/i-want-unique-value-for-telephone-element