Using generate-id to Create Unique Identifiers

Using generate-id to Create Unique Identifiers

Its important to consider another topic when youre substantially reorganizing documents: how to create identifiers in the result document that you can use to identify elements and so refer to them if needed. For example, imagine that you want to use an XSLT stylesheet to add a table of contents to a document and to make the entries in the table of contents hyperlinks so the user has only to click them to jump to the right section. In that case, you need some way of identifying elements in the result document, and the generate-id function is useful here.

In the following example, I add a hyperlinked table of contents to plan-ets.html. To generate that table of contents, I use <xsl:for-each> to loop over all the planets. Each time through the loop, I create a hyperlink and use an attribute value template to create an HREF attribute that is set to a unique identifier for the current planet. Note that despite the name , the generate-id function generates only a string-based identifier for an element; it does not create ID attributes :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <xsl:for-each select="PLANET">                      <H2><A HREF="#{generate-id()}">                          <xsl:value-of select="NAME"/></A>                      </H2>                      <P/>                  </xsl:for-each>                   .                   .                  . 

This adds an identifier to each planet and creates the necessary hyperlinks. The generate-id function not only creates a new identifier for an element, but it also returns that identifier when you use generate-id on the element from then on. Thats useful here, because it means I can create the hyperlink anchors in the planetary data HTML table, setting the anchors NAME attribute to the identifier for each <PLANET> element in turn so that it becomes a hyperlink target:

Listing 6.12 Using the generate-id Function
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          <HTML>              <HEAD>                  <TITLE>                      The Planets Table                  </TITLE>              </HEAD>              <BODY>                  <H1>                      The Planets Table                  </H1>                  <xsl:for-each select="PLANET">                      <H2><A HREF="#{generate-id()}">                          <xsl:value-of select="NAME"/></A>                      </H2>                      <P/>                  </xsl:for-each>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                      <xsl:apply-templates/>                  </TABLE>              </BODY>          </HTML>      </xsl:template>      <xsl:template match="PLANET">         <TR>            <TD><A NAME="{generate-id(.)}">            <xsl:value-of select="NAME"/></A></TD>            <TD><xsl:apply-templates select="MASS"/></TD>            <TD><xsl:apply-templates select="RADIUS"/></TD>            <TD><xsl:apply-templates select="DAY"/></TD>         </TR>     </xsl:template>      <xsl:template match="MASS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="RADIUS">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>      <xsl:template match="DAY">          <xsl:value-of select="."/>          <xsl:text> </xsl:text>          <xsl:value-of select="@UNITS"/>      </xsl:template>  </xsl:stylesheet> 

Thats all it takes; now Ive created hyperlinks that have an HREF attribute that is set to an identifier for a <PLANET> element, and Ive used the same identifier to make each <PLANET> element a hyperlink target. When the user clicks a hyperlink in the table of contents, the browser scrolls to the corresponding planets entry in the HTML table of data. (Note that the HTML table must be off the screen before most browsers scroll the display.) Each XSLT processor creates its own identifiers; heres the result if I use Xalan:

 <HTML>      <HEAD>          <TITLE>              The Planets Table          </TITLE>      </HEAD>      <BODY>          <H1>              The Planets Table          </H1>          <H2>          <A href="#N5">Mercury</A>          </H2>          <P></P>          <H2>          <A href="#N20">Venus</A>          </H2>          <P></P>          <H2>          <A href="#N3B">Earth</A>          </H2>          <P></P>          <TABLE BORDER="2">              <TR>                  <TD>Name</TD>                  <TD>Mass</TD>                  <TD>Radius</TD>                  <TD>Day</TD>              </TR>              <TR>                  <TD><A NAME="N5">Mercury</A>                  </TD><TD>.0553 (Earth = 1)</TD>                  <TD>1516 miles</TD>                  <TD>58.65 days</TD>              </TR>              <TR>                  <TD><A NAME="N20">Venus</A></TD>                  <TD>.815 (Earth = 1)</TD>                  <TD>3716 miles</TD>                  <TD>116.75 days</TD>              </TR>              <TR>                  <TD><A NAME="N3B">Earth</A></TD>                  <TD>1 (Earth = 1)</TD>                  <TD>2107 miles</TD>                  <TD>1 days</TD>              </TR>          </TABLE>      </BODY>  </HTML> 

You can see the result in Figure 6.3, including the hyperlinked table of contents. All the user has to do is click a hyperlink to the corresponding table entry.

Figure 6.3. Using generated IDs in hyperlinks.
graphics/06fig03.gif


Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
EAN: N/A
Year: 2005
Pages: 196

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net