Templates

At the heart of the XSLT stylesheet are template elements. All the information in your stylesheet that is output will pass through a template. In short, they are the most important element in a stylesheet, and they are where you will define all the information for how text, elements, and attributes are to be output by your stylesheet.

There are a few elements that are related to templates and their use, so let's take a closer look at templates and how they function in XSL.

xsl:template

The most important element in the stylesheet is xsl:template. This element has several attributes, including priority, mode, name, and match. The priority attribute allows you to specify a numeric priority level for the template. The mode attribute allows you to assign a mode name to the template, so that you can apply it only to elements with the same mode name. The two attributes with which you will become intimately familiar as you use XSL are name and match.

The name attribute allows you to specify a name for a template, so that the template can be called later by the call-templates element, which we will discuss later.

The match attribute is a very important attribute, because this is what will determine what component your template will be applied to in the XML document. The value of the match attribute takes the form of an XPath expression, as we discussed in Chapter 8. XPath will also be covered in greater detail in Chapter 11, “Locating Components in XML Documents with XPath.”

Now, let's see how templates work. Take our sample XML document. Let's say that we wanted to write a template, which would output our <email> element as an HTML tag with italic:

<xsl:stylesheet version="1.0" 
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="address_book">
 <html>
 <xsl:apply-templates select="contact/email"/>
 </html>
</xsl:template>

<xsl:template match="email">
  Email: <i><xsl:value-of select="."/></i>
</xsl:template>
</xsl:stylesheet>

The result of this template would be

<html> 
 Email: <i>[email protected]</i>
 Email: <i>[email protected]</i>
 Email: <i>[email protected]</i>
</html>
<xsl:template-match="/">
  <xsl:apply-templates/>
</xsl:template-match>


Node type--Rule

Element – Call <xsl:apply-templates> to process the child nodes.
Attributes – Copy the attribute value as text to the result tree
Text – Copy the text to the result tree
Comments – Do nothing
PIs – Do nothing
Namespaces – Do nothing


<?xml version="1.0" encoding="utf-8"?>



    Jane
   Doe

    123 Fake Street
   Springfield
   IL
   49201

   708-555-1212
   708-855-4848
   800-555-1212

   <html>
  Email: <i>[email protected]</i></html>


    John
   Smith
    205 Peaceful Lane
   Bloomington
   IN
   47401
   8192 Busy Street
   Bloomington
   IN
   47408
   812-555-1212
   812-855-4848
   800-333-0999
  <html>
  Email: <i>[email protected]</i></html>
  <html>
  Email: <i>[email protected]</i></html>

<xsl:template match="address_book">
 <html>
 <xsl:apply-templates select="contact/email"/>
 </html>
</xsl:template>

<xsl:template match="email">
  Email: <i><xsl:value-of select="."/></i>
</xsl:template>
</xsl:stylesheet>
						

because this template would first match the address_book element, and then apply the email template to any children of contact elements. When the email template is applied, the resulting value of the email element is inserted into the HTML code in the second template. The first thing that you should notice about the template is that the contents of the template include plain text, HTML tags, and additional XSL tags. Whatever is in the body of the template will be output, so you can directly place text and HTML in the body of the template. The HTML and text are kept separate from the XSL because of the “xsl:” Namespace that is used with XSL elements.

The other piece of the puzzle in the template is the XSL element:

<xsl:value-of select="."/> 

This element is used to specify the value of whichever element or attribute we want to insert into the template body. Because the value of our <email> elements is the actual e-mail address, that is what is inserted into the HTML code.

We could also take the entire contents of our sample file and dump them out as text with one simple template:

<xsl:template match="/"> 
 <xsl:apply-templates />
</xsl:template>

The apply-templates element is used to communicate to the processor that it should apply the template to the current selected node. In the previous example, the node is specified as “/,” or the root element, so it contains the entire document. Therefore, the output would be the text content of the file, stripped of elements and attributes:

Jane 
Doe
office
123 Fake Street
Springfield
Etc...
						

xsl:value-of

The xsl:value-of element is used to insert the value of a component into the template. The value-of element has a select attribute for specifying the element or attribute you want to select the value of, using an XPath expression. As you can see, XPath plays a considerable role in the use of XSLT. Let's look at some value-of elements we might use with our example. If we wanted to insert the value of the name element to display the first and last names, we could use

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml
:space="preserve"> 
<xsl:template match="contact">
 <html>
  <xsl:value-of select="name/first" /> <xsl:value-of select="name/last" />
 </html>
</xsl:template>

</xsl:stylesheet>

We have placed the two value-of elements side-by-side, because that is how they will be replaced in the document. Because XSLT ignores whitespace, in order to preserve the space between the names, we need to set the xml:space attribute to "preserve" in order to preserve the space in the output document.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xml
:space="preserve"> 

You should also note that the template selects the contact node, which then becomes our context for locating the first and last elements, which are children of the name element.

xsl:apply-templates

Apply templates instruct the XSL processor to cycle through child elements of the selected node, which allows you to actually apply templates to more than just the root node of your document. You can use either the apply-templates element with no selector, as we did previously, which will process the current node, or a select attribute to specify a node to apply the template to. For example:

<xsl:template match="/"> 
 <xsl:apply-templates select="//address"/>
</xsl:template>

will first match the root node, and then apply the template to any address children because of the apply-templates element. The result is the output of the content of any address node children, such as street, city, state, and zip:

123 Fake Street
Springfield
IL
49201
205 Peaceful Lane
Bloomington
IN
47401
8192 Busy Street
Bloomington
IN
47408

For example, if we wanted to process the name elements, which are children of the contact element in our example, we could use

<xsl:template match="contact"> 
    <xsl:apply-templates select=".//name"/>
</xsl:template>

The resulting output would be

Jane 
Doe
John
Smith
						

Named Templates

You can also call templates in your documents by name—for example, if you have the following template:

<xsl:template name="get-names" match="contact"> 
    <xsl:apply-templates select=".//name"/>
</xsl:template>

you could actually invoke this template using call-template:

<xsl:call-template name="get-names"/> 

This has basically the same result as using apply-templates; however the biggest difference is that call-template does not have a select attribute that allows you to change the context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.16.23