With XML Schema we can define ID/IDREF links between components in an XML document. I'm looking for a way, using XPath, to ensure that only certain IDs can be used as IDREFs - i.e. only those values which logically make senses in the context of the schema design.
Using the example below, how do I ensure that a book's library_id is actually one of the available library_ids and not some other id which could appear in the message and be used for some entirely different purpose (but still be schema valid).
<books>
<book library_id="lib1">
<author>A</author>
<title>B</title>
</book>
<book library_id="lib2">
<author>C</author>
<title>D</title>
</book>
<library id="lib1">
<name>X</name>
<city>Y</city>
</library>
<library id="lib2">
<name>X</name>
<city>Y</city>
</library>
</books>
The closest I've come to getting this to work is to use the XPath contains function e.g.
contains(//library/@id,//book/@library_id)
but this throws an error because a sequence of more than one item is not allowed as the first argument of contains() ("lib1","lib2").
Thanks
I hope that understood correctly what you want
Books with library_id present in library list
//book[@library_id = //library/@id]
Books with incorrect library_id
//book[not(@library_id = //library/@id)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With