page 88
•
You must define the
ID
and
IDREF
relationship in the XML document. It would be nice to have the XML document define the data only, with the relationships between parts
of the document defined externally say, in a stylesheet. That way, if you needed to define a new relationship between parts of the document, you could do it by creating a
new stylesheet, and you wouldnt have to modify your XML document. Requiring the XML document structure to change every time you need to define a new relationship
between parts of the document will become unwieldy quickly.
•
An element can have at most one attribute of type
ID
. If youd like to refer to the same element in more than one way, you cant use the
id
function.
•
Any given
ID
value can be found on at most one element. If youd like to refer to more than one element with a single value, you cant use the
id
function for that, either.
•
Only one set of
ID
s exists for the entire document. In other words, if you declare the attributes
customer_number
,
part_number
, and
order_number
to be of type
ID
, the value of a
customer_number
must be unique across all the attributes of type
ID
. It is illegal in this case for a
customer_number
to be the same as a
part_number
, even though those attributes might belong to different elements.
•
An
ID
can only be an attribute of an XML element. The only way you can use the
id
function to refer to another element is through its attribute of type
ID
. If you want to find another element based on an attribute that isnt an
ID
, based on the elements content, based on the elements children, etc., the
id
function is of no use whatsoever.
•
The value of an
ID
must be an XML name. In other words, it cant contain spaces, it cant start with a number, and its subject to the other restrictions of XML names.
Section 2.3 of the XML Recommendation defines these restrictions; see http:www.w3.orgTRREC-xml
if youd like more information. To get around all of these limitations, XSLT defines the
key
function. Well discuss that function in the next section.
5.2 Generating Links with the key Function
Now that weve covered the
id
function in great detail, well move on to XSLTs
key
function. Each
key
function effectively creates an index of the document. You can then use that index to find all elements that have a particular property. Conceptually,
key
works like a database index. If you have a database of U.S. postal addresses, you might want to index
that database by the peoples last names, by the states in which they live, by their Zip Codes, etc. Each index takes a certain amount of time to build, but it saves processing time later. If
you want to find all the people who live in the state of Idaho, you can use the index to find all those people directly; you dont have to search the entire database.
Well discuss the details of how the
key
function works, then well compare it to the
id
function.
page 89
5.2.1 Defining a key
You define a
key
function with the
xsl:key
element:
xsl:key name=language-index match=defn use=language
The key has three elements:
•
A
name
, used to refer to this particular key. When you want to find parts of your XML document, use the
name
to indicate the key you want to use.
•
A
match
attribute containing an XPath expression. This specifies what part of the document you want to index. The previous example created an index on all of the
defn
elements. When we call the
key
function, it will return a
defn
element. Note: according to Section 12.2 of the XSLT specification, the value of the
match
attribute cant contain a variable.
•
A
use
attribute containing another XPath expression. This attribute is interpreted in the context of the
match
attribute. In other words, the previous
xsl:key
element created an index of all the
defn
elements, and used the
language
attribute to retrieve them. Note: according to Section 12.2 of the XSLT specification, the value of the
use
attribute cant contain a variable.
5.2.2 A Slightly More Complicated XML Document in Need of Links
To illustrate the full power of the
key
function, well modify our original glossary slightly. Heres an excerpt:
glentry term id=DMZlong xreftext=demilitarized zonedemilitarized
zone DMZterm defn topic=security language=en
In network security, a network that is isolated from, and serves as a neutral zone between, a trusted network for example,
a private intranet and an untrusted network for example, the Internet. One or more secure gateways usually control access
to the DMZ from the trusted or the untrusted network. defn
defn topic=security language=it [Pretend this is an Italian definition of DMZ.]
defn defn topic=security language=es
[Pretend this is a Spanish definition of DMZ.] defn
defn topic=security language=jp [Pretend this is a Japanese definition of DMZ.]
defn defn topic=security language=de
[Pretend this is a German definition of DMZ.] defn
glentry glentry
term id=DMZ acronym=yesDMZterm defn topic=security language=en
See xref refid=DMZlong. defn
glentry