Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

XSLT Element

XSLT xsl:template Element


An XSL stylesheet consists of one or more sets of rules called templates.

Each template contains the rules to apply when a specified node is matched.


xsl:template Element

xsl:template The element is used to build a template.

match The attribute is used to associate an XML element with a template. The match attribute can also be used to define a template for the whole XML document. The value of the match attribute is an XPath expression (for example, match="/" defines the whole document).

Now, let's look at a simplified version of the XSL file from the previous chapter:

Example

<

xsl:stylesheet

version

=

"

1.0

"

xmlns:xsl

=

"

http://www.w3.org/1999/XSL/Transform

"

<

xsl:template

match

=

"

/

"

<

html

<

body

<

h2

My CD Collection

h2

<table border="1">
<tr bgcolor="#9acd32">
<th Title th>
<th Artist th tr>
<tr>
<td>

.

td

<

td

.

td

tr

table

body

html

xsl:template

xsl:stylesheet

Try it »

Example Explanation

Since an XSL stylesheet is itself an XML document, it always starts with an XML declaration:.

The next element,xsl:stylesheet, ** **, defines this document as an XSLT stylesheet document (along with the version number and XSLT namespace attribute).

xsl:template The element defines a template, and the match="/" attribute associates this template with the root of the XML source document.

xsl:template The content inside the element defines the HTML code written to the output result.

The last two lines define the end of the template and the end of the stylesheet.

The result of this example has a small flaw, because the data is not copied from the XML document to the output. In the next chapter, you will learn how to use the xsl:value-of element to select values from XML elements.