O'Reilly logo

XSLT, 2nd Edition by Doug Tidwell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required