The xsl:apply-templates Element

The <xsl:apply-templates> Element

In the basic template weve already written, the root node has been matched with the expression / and replaced with a literal result element. However, when you match the root node, you usually have the whole rest of the document to work on, and well do that with the <xsl:apply-templates> element.

The following list includes the attributes of the <xsl:apply-templates> element:

  • select (optional). Node-set to be processed. If omitted, all children of the node are processed automatically. Set to an expression.

  • mode (optional). Sets the processing mode. Template rules with a matching mode are applied to this node. Set to a QName .

The <xsl:apply-templates> element can contain zero or more <xsl: sort > elements, or zero or more <xsl: with-param > elements.

In the following example, the template matches the root node, and replaces it with the <HTML> literal result element:

 <?xml version="1.0">  <xsl:stylesheet version="1.0" xmlns:xsl="">      <xsl:template match="/">          <HTML>          </HTML>      </xsl:template>          .          .          . 

On the other hand, weve only matched the root node, and the planets.xml data tree has a number of nodes under the root node:

 <?xml version="1.0"?>  <?xml-stylesheet type="text/xml" href="planets.xsl"?>  <PLANETS>      <PLANET>          <NAME>Mercury</NAME>          <MASS UNITS="(Earth = 1)">.0553</MASS>          <DAY UNITS="days">58.65</DAY>          <RADIUS UNITS="miles">1516</RADIUS>          <DENSITY UNITS="(Earth = 1)">.983</DENSITY>          <DISTANCE UNITS="million miles">43.4</DISTANCE><!--At perihelion-->      </PLANET>          .          .          . 

To process more than just the root node, you can use <xsl:apply-templates> by adding that element like this:

 <?xml version="1.0">  <xsl:stylesheet version="1.0" xmlns::xsl="">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>          .          .          . 

This element makes the XSLT processor look at any child nodes of the root node and try to find any template that matches those nodes. For example, you might want to replace all <PLANET> elements with <P>Planet</P> . The <PLANET> elements are children of the <PLANETS> element, so I add a new template for <PLANETS> first, just telling the XSLT processor to keep searching for child nodes:

 <?xml version="1.0">  <xsl:stylesheet version="1.0" xmlns::xsl="">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANETS">              <xsl:apply-templates/>      </xsl:template>          .          .          . 

Now I can add another template for the next level down, which includes the <PLANET> elements. In this case, Ill just replace each <PLANET> element with the literal result element <P>Planet</P> :

Listing 2.3 Using <xsl:apply-templates/>
 <?xml version="1.0"?>  <xsl:stylesheet version="1.0" xmlns::xsl="">      <xsl:template match="/">          <HTML>              <xsl:apply-templates/>          </HTML>      </xsl:template>      <xsl:template match="PLANETS">              <xsl:apply-templates/>      </xsl:template>      <xsl:template match="PLANET">          <P>              Planet          </P>      </xsl:template>  </xsl:stylesheet> 

Heres the result of this stylesheet:

 <HTML>      <P>          Planet      </P>      <P>          Planet      </P>      <P>          Planet      </P>  </HTML> 

As you can see, there is nothing left of the <PLANETS> element at all. All thats left is the three literal result elements <P>Planet</P> that were substituted for the three <PLANET> elements.

Omitting the select Attribute

If you omit the select attribute, then only the child nodes of the current node are processed, which does not include attribute or namespace nodes, because they are not considered children. If you want to process those kinds of nodes, youll have to use the select attribute, as youll see in Chapter 3.

This is all very interesting, but not too useful. It would be far better, for example, to be able to access the actual value of each element (such as the name of each planet) and make use of that data. And, of course, you can.

Inside XSLT
Inside Xslt
ISBN: B0031W8M4K
Year: 2005
Pages: 196

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: