XSLT - Templates

Card Puncher Data Processing

About

A stylesheet contains a number of templates, defined with the <xsl:template> tag.

An XSLT template is a set of formatting instructions that apply to the nodes selected by an XPath expression.

Syntax

In a stylesheet, an XSLT template would look something like this:

Without parameter

<xsl:template match="/ARTICLE/SECT">
 <h2> 
     <xsl:apply-templates select="text()|B|I|U|DEF|LINK"/> 
 </h2>
 <xsl:apply-templates select="SECT|PARA|LIST|NOTE"/> 
</xsl:template>

where:

  • xsl:template defines a template
  • the match attribute select the (current|set of) node(s) by using an XPath addressing mechanisms. In this case, the (selected|current) node is the set of node /ARTICLE/SECT. It will select the set of nodes from the input stream.
  • The instruction <xsl:apply-templates>:
    • processes any templates that apply to children of the current node
    • causes the text of the current node to be processed.
  • The select attribute of the apply-template instruction select the tags that must be processed in a regular expression style (Note: text() is an Xpath function). Here a set of tag are processed first and the others afterwards. Using the select clause lets you apply all templates to a subset of the information available in the current context.

With parameter

The definition

The parameterized template gets executed only when you invoke it.

<xsl:template name="htmLink">
 <xsl:param name="dest" select="UNDEFINED"/> 
 <xsl:element name="a">
    <xsl:attribute name="href">
       <xsl:value-of select="$dest"/>
    </xsl:attribute>
    <xsl:apply-templates/> 
 </xsl:element> 
</xsl:template>

where:

  • name is the name of the template (There is no match clause)
  • the xsl:param tag defines a parameter with the default value (UNDEFINED)
  • the <xsl:value-of> tag extract the value of a node
  • xsl:apply-templates inserts the text from the text node

The call

<xsl:call-template name="htmLink">
  <xsl:with-param name="dest" select="@target"/> 
</xsl:call-template>

The template definition must be below the call.

Processing

A processor will process the stylesheet.

Tag

XSLT works by transforming the source tree into the result tree. To visualize the result of XSLT operations, it is helpful to understand the structure of those trees, and their contents.

Within the template, tags that do not start with the xsl: namespace prefix are simply copied. The newlines and whitespace that follow them are also copied.

An XSLT stylesheet expects to process tags. So everything it sees needs to be either:

In the example above case, the non-XSL tags are HTML tags. So when the root tag is matched, XSLT:

  • outputs the HTML start tags,
  • processes any templates that apply to children of the root,
  • and then outputs the HTML end tags.

Order

Templates are independent of one another, so it does not generally matter where they occur in a file.

Order does make a difference when two templates can apply to the same node. In that case, the one that is defined last is the one that is found and processed.





Discover More
Card Puncher Data Processing
XPATH - (Node) Attribute (@)

In a template, the expression @type refers to the type attribute of the current node. Similarly, the expression @ refers to all the attributes for the current element. To refer to an attribute, you prefix...
Card Puncher Data Processing
XSLT - (Current) Context

The context in XSL (ie the node where you are) The context can be considered as: Xsl File System Tips a set of nodes a directory a single member of the set a file The context can also be considered...
Card Puncher Data Processing
XSLT - Variable

There are two elements that can be used to bind variables: xsl:variable and xsl:param. The difference is that the value specified on the xsl:param variable is only a default value for the binding;...
Card Puncher Data Processing
XSLT/XPATH - Special Character

Curly braces cause the text inside the quotes to be processed as an XPath expression instead of being interpreted as a literal string. Curly braces are recognized anywhere that an attribute value template...
Card Puncher Data Processing
Xml - XSL Transformations (XSLT)

XSLT is the most important part of XSL. Extensible Stylesheet Language Transformation (XSLT): is a language for transforming XML documents into other XML documents (and particularly into (X)HTML documents)....



Share this page:
Follow us:
Task Runner