The ID, IDREF, and IDREFs Datatypes

page 83 To round out our example, the seealso element contains an attribute of type IDREFS . This datatype contains one or more values, each of which must match a value of an ID elsewhere in the document. Multiple values, if present, are separated by whitespace. There are some complications of ID and related datatypes, but well discuss them later. For now, well focus on how the id function works.

5.1.2 An XML Document in Need of Links

To illustrate the value of linking, well use a small glossary written in XML. The glossary contains some glentry elements, each of which contains a single term and one or more defn elements. In addition, a definition is allowed to contain a cross-reference xref to another term . Heres a short sample document: ?xml version=1.0 ? DOCTYPE glossary SYSTEM glossary.dtd glossary glentry term id=appletappletterm defn 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 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 xref refid=servlet. seealso refids=wildcard-char DMZlong pattern-matching defn glentry glentry term id=DMZlong xreftext=demilitarized zonedemilitarized zone DMZterm defn 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 glentry glentry term id=DMZDMZterm defn See xref refid=DMZlong. defn glentry glentry term id=pattern-matchingpattern-matching characterterm defn A special character such as an asterisk or a question mark ? that can be used to represent zero or more characters. Any character or set of characters can replace a pattern-matching character. defn glentry page 84 glentry term id=servletservletterm defn 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 xref refid=applet . defn glentry glentry term id=wildcard-charwildcard characterterm defn See xref refid=pattern-matching. defn glentry glossary In this XML listing, each term element has an id attribute that identifies it uniquely. Many xref elements also refer to other terms in the listing. Notice that each time we refer to another term, we dont use the actual text of the referenced term. When we write our stylesheet, well use the XPath id function to retrieve the text of the referenced term; if the name of a term changes as buzzwords go in and out of fashion, some marketing genius might want to rename the pattern-matching character, for example, we can rerun our stylesheet and be confident that all references to the new term contain the correct text. Finally, some term elements have an xreftext element because some of the actual terms are longer than wed like to use in a cross-reference. When we have an xref to the term ASCII American Standard Code for Information Interchange, it would get pretty tedious if the entire text of the term appeared throughout our document. For this term, well use the xreftext attributes value, ensuring that the cross-reference contains the less-intimidating text ASCII .

5.1.3 A Stylesheet That Uses the id Function

Lets look at our desired output. What we want is an HTML document, such as that shown in Figure 5-1 , that displays the various definitions in an easy-to-read format, with the cross- references formatted as hyperlinks. In the HTML document, well need to address several things in our stylesheet: • The title and the h1 contain the first and last terms in the glossary. We can use XPath expressions to generate that information. • The xref elements have been replaced with the xreftext attribute of the referenced term element, if there is one. If that attribute doesnt exist, xref is replaced by the text of the term element. Well use the id function to find the referenced term , and well use XSLTs control elements to check if the xreftext attribute exists. • The hyperlinks generated from the xref elements refer to a named anchor point elsewhere in the HTML document. If xref elements refer to a given term , we have to create a named anchor a name=... at the location of the referenced term . To simplify things, well generate a named anchor for each term automatically, using the id attribute required to be unique by our DTD as the name of the anchor.