Suppose I have the following XML instance document.
Code:
<computer>
<component number="78" name="chassis">
<related-to>79</related-to>
<related-to>80</related-to>
</component>
<component number="79" name="hard-disk">
<related-to>80</related-to>
</component>
<component number="80" name="screen">
<related-to>78</related-to>
</component>
</computer>
I need to validate that every
related-to text content refers to an existing
number attribute i.e. all components should relate to existing components.
Now I can do this very well in my XML Schema and I have tested and it works ok. The XML Schema is as follows
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="computer">
<xs:complexType>
<xs:sequence>
<xs:element name="component" type="partType" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:key name="key">
<xs:selector xpath="component"/>
<xs:field xpath="@number"/>
</xs:key>
<xs:keyref name="numKey" refer="key">
<xs:selector xpath="component/related-to"/>
<xs:field xpath="."/>
</xs:keyref>
</xs:element>
<xs:complexType name="partType">
<xs:sequence>
<xs:element name="related-to" type="xs:positiveInteger" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="name" type="xs:token" use="required"/>
<xs:attribute name="number" type="xs:positiveInteger" use="required"/>
</xs:complexType>
</xs:schema>
Now I need to write an XSL Stylesheet to check the same dependencies that the XML Schema does. This is because the server side uses the Schema and on the client side, a stylesheet validation is required. Not particularly sure why can not use Schema on both client/server.
Basically, I need to go through every
component and find its
relate-to text contents. Then I need to check to make sure this actually relates to some
@number attribute in any other
component but the one I am currently looking at.
I am wondering if people can help me on what the best approach is to this. Actual XSLT code is helpful but I really need to understand on how to go about it.
I am thinking of doing this
1. creating a node set of all
//related-to contents and another node set of the number attribute like
//@number
2. For each element in the
//related-to node set, loop through the
//@number and see if I find one that matches
Basically, I want to do the equivalent of this portion of my Schema
Code:
<xs:key name="key">
<xs:selector xpath="component"/>
<xs:field xpath="@number"/>
</xs:key>
<xs:keyref name="numKey" refer="key">
<xs:selector xpath="component/related-to"/>
<xs:field xpath="."/>
</xs:keyref>
Thanks
Shumba