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:

  1. 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> ...

Get XSLT, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.