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.