Reputation: 3393
I have the following XML file:
<?xml version="1.0" encoding="UTF-8" ?>
<catalog>
<chainList>
<chain chainName="Chain">
<handlerList>
<handler handlerName="This is a name">com.lorescianatico.chain.handler.DummyHandler</handler>
<handler handlerName="This is another name">com.lorescianatico.chain.handler.AnotherDummyHandler</handler>
</handlerList>
</chain>
<chain chainName="AnotherChain">
<handlerList>
<handler handlerName="This is a name">com.lorescianatico.chain.handler.DummyHandler</handler>
<handler handlerName="This is another name">com.lorescianatico.chain.handler.AnotherDummyHandler</handler>
</handlerList>
</chain>
</chainList>
</catalog>
I need to perform a validation using an XSD Schema. The schema I defined is the following:
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="chainList" minOccurs="1" maxOccurs="1">
<xs:complexType >
<xs:sequence>
<xs:element name="chain" minOccurs="0" maxOccurs="unbounded" type="chain"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueChainName">
<xs:selector xpath="."/>
<xs:field xpath="chain/@chainName"/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="catalogName" type="xs:string"/>
</xs:complexType>
</xs:element>
<xs:complexType name="chain">
<xs:sequence>
<xs:element name="handlerList" minOccurs="1" maxOccurs="1">
<xs:complexType>
<xs:sequence>
<xs:element name="handler" minOccurs="1" maxOccurs="unbounded" type="handler"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueHandlerClass">
<xs:selector xpath="."/>
<xs:field xpath="handler"/>
</xs:unique>
<xs:unique name="uniqueHandlerName">
<xs:selector xpath="."/>
<xs:field xpath="handler/@handlerName"/>
</xs:unique>
</xs:element>
</xs:sequence>
<xs:attribute name="chainName" use="required" type="xs:string"/>
</xs:complexType>
<xs:complexType name="handler">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="handlerName" type="xs:string" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:schema>
Now, if you test this xml with the schema I have pasted, validation fails beacause I defined the same handler value in different chains. Is there a way for defining the unique constraint to check if a value is unique only in a sub-xml? Like a sort of scope for the XPath query for defining unique constraint? I want the attribute handlerName and the value of handler to be unique only within the same chain.
Thanks in advance.
Upvotes: 1
Views: 594
Reputation: 3393
Sometimes you just need to stop and think. After few hours I found the solution:
<xs:unique name="uniqueHandlerName">
<xs:selector xpath="handler"/>
<xs:field xpath="@handlerName"/>
</xs:unique>
Writing in this way the constraint worked for me.
Upvotes: 1