An Unstructured XML Document in Need of Links

page 102 defn glentry glentry termwildcard characterterm defn See reftermpattern-matching characterrefterm. defn glentry glossary To generate cross-references between the refterm elements and the associated term elements, well need to do three things: 1. Define a key for all terms. Well use this key to find terms that match the text of the refterm element. 2. Generate a new ID for each term we find. 3. For each refterm , use the key function to find the term element that matches the text of refterm . Once weve found the matching term , we call generate-id to find the newly created ID. Well go through the relevant parts of the stylesheet. First, we define the key : xsl:key name=terms match=term use=. Notice that we use the value of the term element itself as the lookup value for the key . Given a string, we can find all term elements with that same text. Second, we need to generate a named anchor point for each term element: xsl:template match=glentry p b a name={generate-idterm} xsl:value-of select=term xsl:text: xsl:text a b xsl:apply-templates select=defn p xsl:template Third, we find the appropriate reference for a given refterm . Given the text of a refterm , we can use the key function to find the term that matches. Passing the term to the generate-id function returns the same ID generated when we created the named anchor for that term : xsl:template match=refterm a href={generate-idkeyterms, .} xsl:value-of select=. a xsl:template Our generated HTML output creates cross-references similar to those in our earlier stylesheets: h1Glossary Listing: applet - wildcard characterh1 p ba name=N11applet: ab An application program, written in the Java programming language, that can be retrieved from a web server and executed by a web browser. A reference to an applet appears in the markup for a web page, in the same way that a reference to a graphics file appears; a browser retrieves an applet in the same page 103 way that it retrieves a graphics file. For security reasons, an applets access rights are limited in two ways: the applet cannot access the file system of the client upon which it is executing, and the applets communication across the network is limited to the server from which it was downloaded. Contrast with a href=N53servleta. p ... p ba name=N53servlet: ab An application program, written in the Java programming language, that is executed on a web server. A reference to a servlet appears in the markup for a web page, in the same way that a reference to a graphics file appears. The web server executes the servlet and sends the results of the execution if there are any to the web browser. Contrast with a href=N11appleta. p Using the key and generate-id functions, weve been able to create IDs and references automatically. This approach isnt perfect; we have to make sure the text of the refterm element matches the text of the term exactly. This example, like all of the examples weve shown so far, uses a single input file. A more likely scenario is that we have one XML document that contains terms, and we want to reference definitions in a second XML document that contains definitions, but no IDs. We can combine the technique weve described here with the document function to import a second XML document and generate links between the two. Well talk about the document function in a later chapter; for now, just remember that there are ways to use more than one XML input document in your transformations.

5.3.2 The generate-id Function

Before we leave the topic of linking, well go over the details of the generate-id function. This function takes a node-set as its argument, and works as follows: • For a given transformation, every time generate-id is invoked against a given node, it returns the same ID. The ID doesnt change while youre doing a given transformation. If you run the transformation again, theres no guarantee generate- id will generate the same ID the second time around. All calls to generate-id in the second transformation will return the same ID, but that ID might not be the same as in the first transformation. • If you invoke generate-id against two different nodes, the two generated IDs will be different. • Given a node-set, generate-id returns an ID for the node in the node-set that occurs first in document order. • If the node-set you pass to the function is empty you invoke generate-idfleeber , and there are no fleeber elements in the current context, generate-id returns an empty string. • If no node-set is passed in you invoke generate-id , the function generates an ID for the context node. page 104

5.4 Summary

In this chapter, weve examined a several ways to generate links and cross-references between different parts of a document. If your XML document has a reasonable amount of structure, you can use the id and key functions to define many different relationships between the parts of a document. Even if your XML document isnt structured, you may be able to use key and generate-id to create simple references. In the next chapter, well look at sorting and grouping, two more ways to organize the information in our XML documents. The generate-id function is not required to check if an ID it generates duplicates an ID thats already in the document. In other words, if your document has an attribute of type ID with a value of sdk3829a , theres a possibility that an ID returned by generate-id will also be sdk3829a . Its not likely, but be aware that it could happen.