An Unstructured XML Document in Need of Links
For our example here, we’ll take out all of the id and refid attributes that have served us well so
far. This is a contrived example, but it demonstrates how we can use
the key()
and generate-id() functions to
generate links between parts of our document.
In our new sample document, we’ve stripped out the references that tied things together so neatly before:
<?xml version="1.0"?>
<!-- parts-list4.xml -->
<parts-list>
<component>
<name>Turnip Twaddler</name>
<partref>Paring Knife</partref>
<partref>Spanner</partref>
<partref>Feather Duster</partref>
<partref>Grommet</partref>
<description>
If you've got turnips to twaddle, this is the tool for you!
Comes with a <partref>Feather Duster</partref>.
</description>
</component>
...
<part>
<name>Pitter</name>
<description>
Removes pits from olives and cherries in no time at all.
</description>
</part>
<part>
<name>Patter</name>
<description>
We're not sure what these things do, but people seem
to like 'em.
</description>
</part>
...
</parts-list>We’ve removed all of the IDs
and IDREFs in the document. For
elements such as <partref> that formerly used
attributes to link parts of the document together, we simply use the
text of the item we’re referring to. To generate the cross-references
we created before, we’ll need to do three things:
Define two keys for all parts and components. One key lets us get the
<part>that matches a given name, and the other lets us find a<component>with a<partref> ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access