I have some xml data with a list of allowed types at the top and a recursive list type of objects and sub-objects that refer to the allowed types:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="KEYREF_TEST.xsd">
<ALLOWED_SHAPE_TYPES>
<SHAPE_TYPE>Circle</SHAPE_TYPE>
<SHAPE_TYPE>Triangle</SHAPE_TYPE>
<SHAPE_TYPE>Square</SHAPE_TYPE>
</ALLOWED_SHAPE_TYPES>
<SHAPES>
<SHAPE>
<TYPE>Triangle</TYPE>
<SUB_SHAPES>
<SHAPE>
<TYPE>Circle</TYPE>
<SUB_SHAPES/>
</SHAPE>
<SHAPE>
<TYPE>Square</TYPE>
<SUB_SHAPES>
<SHAPE>
<TYPE>Triangle</TYPE>
<SUB_SHAPES/>
</SHAPE>
</SUB_SHAPES>
</SHAPE>
</SUB_SHAPES>
</SHAPE>
<SHAPE>
<TYPE>Square</TYPE>
<SUB_SHAPES/>
</SHAPE>
</SHAPES>
</ROOT>
I have defined the schema that validates this document as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ROOT">
<xs:complexType>
<xs:sequence>
<xs:element name="ALLOWED_SHAPE_TYPES">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="SHAPE_TYPE" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SHAPES" type="shape-list-type"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="SHAPE_TYPE_UK">
<xs:selector xpath="ALLOWED_SHAPE_TYPES/SHAPE_TYPE"/>
<xs:field xpath="."/>
</xs:unique>
</xs:element>
<xs:complexType name="shape-list-type">
<xs:sequence>
<xs:element name="SHAPE" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TYPE" type="xs:string">
<xs:keyref name="SHAPE_TYPE_FK" refer="SHAPE_TYPE_UK">
<xs:selector xpath="."></xs:selector>
<xs:field xpath="."></xs:field>
</xs:keyref>
</xs:element>
<xs:element name="SUB_SHAPES" type="shape-list-type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
When validating in oXygen 13.2 I get the following error:
System ID: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
Main validation file: C:\pvcswork\CodeSource\XMLSchema\KEYREF_DATA.xml
Schema: C:\pvcswork\CodeSource\XMLSchema\KEYREF_TEST.xsd
Engine name: Xerces
Severity: error
Description: Identity Constraint error: identity constraint "KeyRef@1df2ead" has a keyref which refers to a key or unique that is out of scope.
Start location: 10:28
I believe this is due to the xs:keyref being a child of the xs:complexType definition which is not a sub-element of the xs:element where the xs:unique is defined.
I could move the xs:keyref into the ROOT definition and target the TYPE elements with an XPath, but the // syntax is not permitted so I would lose the recursion and have to define a new xs:keyref for each level of nesting. ** UPDATE - // is permitted when at the start of the XPath, preceeded by a dot! **
Is there a solution I have missed (still using XSDs)? Thanks in advance..
The XSD below would be my solution to your problem. I basically fixed some of the selectors and put them at the same level.
The XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ROOT">
<xs:complexType>
<xs:sequence>
<xs:element name="ALLOWED_SHAPE_TYPES">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="SHAPE_TYPE" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="SHAPES" type="shape-list-type"/>
</xs:sequence>
</xs:complexType>
<xs:unique name="SHAPE_TYPE_UK">
<xs:selector xpath="ALLOWED_SHAPE_TYPES/SHAPE_TYPE"/>
<xs:field xpath="."/>
</xs:unique>
<xs:keyref name="SHAPE_TYPE_FK" refer="SHAPE_TYPE_UK">
<xs:selector xpath=".//SHAPE/TYPE"></xs:selector>
<xs:field xpath="."></xs:field>
</xs:keyref>
</xs:element>
<xs:complexType name="shape-list-type">
<xs:sequence>
<xs:element name="SHAPE" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="TYPE" type="xs:string"/>
<xs:element name="SUB_SHAPES" type="shape-list-type"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>