Creating a Template

Creating a Template

In Chapter 2, I created a basic template to match the nodes in planets.xml and convert that document to HTML. You create templates with <xsl:template> elements in your stylesheets, which specify the rules for the transformations you want to apply. We created a template that matched the <PLANETS> root element by matching the pattern /PLANETS, which stands for all <PLANETS> elements that are children of the root node, as follows :

 <?xml version="1.0"?>  <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">      <xsl:template match="/PLANETS">          .          .          .      </xsl:template>          .          .          .  </xsl:stylesheet> 

When the XSLT processor finds a node that matches your templates pattern, that node becomes the context node for the template, which means that all operations are performed with respect to that node. You can refer to the current node as "." which is an XPath expression. Youll see more XPath expressions in this chapter and in Chapter 7.

Upcoming in XSLT 2.0

XSLT 1.0 has trouble matching elements or attributes whose values have been set to null. Thats one of the topics that XSLT 2.0 is supposed to address.

Inside this template, put the HTML markup that starts the table you want to create; this kind of direct markup insertion is called a literal result element . When the processor encounters a literal, it copies it to the result tree:

 <?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>                  <TABLE BORDER="2">                      <TR>                          <TD>Name</TD>                          <TD>Mass</TD>                          <TD>Radius</TD>                          <TD>Day</TD>                      </TR>                  </TABLE>              </BODY>          </HTML>      </xsl:template>          .          .          .  </xsl:stylesheet> 

However, this rule processes only the <PLANETS> element, not any of its <PLANET> children, which contain the actual data. By the default template rules, the <PLANET> elements would also be processed if I set up a template to match them. Thats not good enough, however, because I want to insert the result of processing the <PLANET> elements in a specific place in the HTML Im creating. To do that, I need to use the <xsl:apply-templates> element.



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

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