Modes


Modes allow a document to be processed multiple times; each time through the document, different output is provided to the result tree. Why would you want to do this? The next time you're on the Internet, take a look at one of the W3C documents on the W3C's Web site at www.w3c.org. An example would be the XML specification at http://www.w3.org/TR/2000/REC-xml-20001006. Notice how they all start with a complete document table of contents? This wasn't generated by hand, I can guarantee you. It was generated utilizing modes with an XSL stylesheet. Generation of tables of content and indices is a common use of this technique.

Here's how it's done. Both xsl:template and xsl:apply-templates have an optional mode attribute.

We'll use our resumes XML document and write a stylesheet to generate a table of contents that lists the name of each person in the XML document, and we'll make each of these names a hyperlink to each of their data. The list of names will be followed by the data applicable to each person. To do this, we'll write a stylesheet that has an <xsl:apply-templates select="..." mode=" namelist "> to generate the name list that has a hyperlink to the name of each person in the file. We'll also have another <xsl:apply-templates select="..." mode="namedata"> to generate the body of the document.

A list of HTML hyperlinks is written like this:

 <UL>  <LI><A HREF="#...">...</A></LI>  <LI><A HREF="#...">...</A></LI> 

The anchor points in the HTML page to which these links point are written like this:

 <A NAME="...">...</A><P>  data here  </P>  ... 

Listing 2.27 shows the stylesheet.

Listing 2.27 XSLT Stylesheet That Generates a Table of Contents
 <?xml version="1."?>  <xsl:stylesheet    xmlns:xsl=http://www.w3.org/XSL/Transform/1.0>  <xsl:template match="RESUMES">      <HTML>        <HEAD>          <TITLE>Candidates</TITLE>        </HEAD>        <BODY>          <H4>Candidate Resumes</H4>          <UL>            <xsl:apply-templates select="PERSON" mode="namelist">          </UL>  <H4>Candidate Information</H4>          <xsl:apply-templates select="PERSON" mode="namedata">  </BODY>      </HTML>    </xsl:template>    <xsl:template match="PERSON" mode="namelist">      <LI><A>        <xsl:attribute name="HREF">#          <xsl:value-of select="@PERSONID"        </xsl:attribute>        <xsl:value-of select="NAME/LAST"/>, <!--comma & space -->        <xsl:value-of select="NAME/FIRST"/>      </A></LI>    </xsl:template>  <xsl:template match="PERSON" mode="namedata">      <A>        <xsl:attribute name="NAME">          <xsl:value-of select="@PERSONID"        </xsl:attribute>        <xsl:value-of select="NAME/LAST"/>, <!--comma & space -->        <xsl:value-of select="NAME/FIRST"/>      </A><P>      Address:      <xsl:value-of select="STREET"/><BR>      <xsl:value-of select="CITY"/><BR>            </P>    </xsl:template>  </xsl:stylesheet> 

The first time through the stylesheet, the parser will notice that there are two different modes in the document. It now knows that it must make two passes through the data. During the first pass, the PERSON template is parsed because it was associated with the first mode. This results in all person names being listed as hyperlinks in "last name, first name" order. During the second pass through the data, the second mode ( namelist ) causes an anchor list to be generated consisting of the person's name, street, and city. These anchors are pointed to by the hyperlinks created during the first data pass-through.



XML and SQL Server 2000
XML and SQL Server 2000
ISBN: 0735711127
EAN: 2147483647
Year: 2005
Pages: 104
Authors: John Griffin

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