XML Schema - Unique attribute value + parent attribute value
Wanting to write a schema that tests to see if the combination of an attribute + the parent node attribute is unique.
Have the following so far
<xs:unique name="specify_req_once_in_traceability_section"开发者_运维问答>
<xs:selector xpath="./artefact/doc/relationship"/>
<xs:field xpath="@parent_sec"/>
</xs:unique>
but if I try to include the parent_doc using .. I get unexpected token --- I am guessing the parent axis is not allowed
xml looks like below
<?xml version="1.0" encoding="UTF-8"?>
<root>
<artefact>
<doc parent_doc="fred">
<relationship parent_sec="125"/>
</doc>
<doc parent_doc="geoff">
<relationship parent_sec="119"/>
<relationship parent_sec="118"/>
<relationship parent_sec="117"/>
<relationship parent_sec="118"/>
</doc>
<doc parent_doc="fred">
<relationship parent_sec="125"/>
</doc>
<doc parent_doc="james">
<relationship parent_sec="125"/>
</doc>
</artefact>
</root>
I tested my hypothesis in the comment and it works using the xerces version in JDK 6. I simplified the schema a bit, but the general structure is the same:
Schema
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="doc" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="relationship" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="ps" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="pd" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
<xs:unique name="u1">
<xs:selector xpath="doc"/>
<xs:field xpath="@pd"/>
<xs:field xpath="relationship/@ps" />
</xs:unique>
</xs:element>
</xs:schema>
Instance document
<root>
<doc pd="fred">
<relationship ps="125" />
</doc>
<doc pd="geoff">
<relationship ps="119" />
<relationship ps="118" />
<relationship ps="117" />
<relationship ps="118" />
</doc>
<doc pd="fred">
<relationship ps="125" />
</doc>
<doc pd="james">
<relationship ps="125" />
</doc>
</root>
When validated, xerces give error message:
[Error] :13:28: Duplicate unique value [fred,125] declared for identity constraint of element "root".
If you change the second james' relationship ps to 126, the document validates without errors.
精彩评论