I have an XML structure similar to this:
<root>
<hierarchy1>
<foo id="foo1" /> <!-- id is of type id -->
<bar id="bar1" /> <!-- id is of type id -->
</hierarchy1>
<hierarchy2>
<foohandler ref="foo1" /> <!-- ref is of type idref -->
<barhandler ref="bar1" /> <!-- ref is of type idref -->
</hierarchy2>
</root>
I.e. I have different types of Objects that have ids and I have different types of target Objects that need to reference these IDs using IDREF.
Is there a way to ensure that the IDREF is of the correct expected type, i.e.
<foohandler ref="foo1" />is valid whereas<foohandler ref="bar1" />isn’t?
I’ve almost posted an answer which used pattern based restrictions (foo[0-9]+, etc.) to achieve what the OP asked. However before posting I found out about keys and keyrefs, which seemed to be the solution. So I came up with the following schema; but, at first it didn’t work because of a silly namespace declaration issue. I’m posting it primarily, because I didn’t know how to do this before either and I was going crazy, because I knew I’m almost there.
The schema above validates the XML document below.
If
<so:barhandler so:keyref="2"/>is changed to<so:barhandler so:keyref="1"/>or<so:barhandler so:keyref="3"/>the document becomes invalid.The catch here is that without specifying the namespace
xmlns:so="http://stackoverflow.com"and using this same namepsace in thexpathattribute ofxs:selectorandxs:fieldsome parsers won’t care about the integrity of the document.Eclipse’s XML parser/validator and this online tool didn’t give a crap about my key, keyref declarations until I specified which namespace should the XPath expressions refer.
The bottom lime could be use namespaces everyhwere?
This answer lead me to my relevation.