web analytics

xsl:call-template vs xsl:apply-templates in XSLT

Options

codeling 1599 - 6654
@2015-12-16 18:52:48

In XSLT, the xsl:call-template element takes a template name as a parameter whereas the xsl:apply-templates element takes an xpath expression.

<xsl:call-template>

An xsl:call-template element invokes a template by name. Unlike xsl:apply-templates, xsl:call-template does not change the current node or the current node list.

Only templates with a name attribute can be invoked by name. If an xsl:template element has a name attribute, it may, but need not, also have a match attribute.

<xsl:apply-templates>

The xsl:apply-templates element applies a template to the current element or to the current element's child nodes. If we add a select attribute to the xsl:apply-templates element it will process only the child element that matches the value of the attribute.

The <xsl:apply-templates> element takes a sequence of nodes and goes through them one by one. For each, it locates the template with the highest priority that matches the node, and invokes it. So <xsl:apply-templates> is like a <xsl:for-each> with an <xsl:choose> inside, but more modular.

Summary

Some recommendations about when to use matching templates and when to use named templates are:

  • use <xsl:apply-templates> and matching templates if you're processing individual nodes to create a result; use modes if a particular node needs to be processed in several different ways (such as in the table of contents vs the body of a document)
  • use <xsl:call-template> and a named template if you're processing something other than an individual node, such as strings or numbers or sets of nodes
  • In XSLT 2.0, use <xsl:function> if you're returning an atomic value or an existing node
@2016-03-03 23:13:06

Unnamed (Match) Templates

Unnamed templates, also known as match templates, include a match attribute that contains an XPath expression to specify the criteria for nodes upon which the template should be invoked. In the following example, the template applies to the element named route that is a child of the current context and that has a child element named next-hop whose value starts with the string 10.10..

<xsl:template match="route[starts-with(next-hop, '10.10.')]">
    <!-- ... body of the template ... -->
</xsl:template>

By default, when XSLT processes a document, it recursively traverses the entire document hierarchy, inspecting each node, looking for a template that matches the current node. When a matching template is found, the contents of that template are evaluated.

The <xsl:apply-templates> element can be used inside an unnamed template to limit and control XSLT’s default, hierarchical traversal of nodes. If the <xsl:apply-templates> element has a select attribute, only nodes matching the XPath expression defined by the attribute are traversed. Otherwise all children of the context node are traversed. If the select attribute is included, but does not match any nodes, nothing is traversed and nothing happens.

In the following example, the template rule matches the <route> element in the XML hierarchy. All the nodes containing a changed attribute are processed. All <route> elements containing a changed attribute are replaced with a <new> element.

<xsl:template match="route">
    <new>
        <xsl:apply-templates select="*[@changed]"/>
    </new>
</xsl:template>

Using unnamed templates allows the script to ignore the location of a tag in the XML hierarchy. For example, if you want to convert all <author> tags into <div class="author"> tags, using templates enables you to write a single rule that converts all <author> tags, regardless of their location in the input XML document.

@2016-03-03 23:13:49

Named Templates

Named templates operate like functions in traditional programming languages, although with a verbose syntax. When the complexity of a script increases or a code segment appears in multiple places, you can modularize the code and create named templates. Like functions, named templates accept arguments and run only when explicitly called.

You create a named template by using the <xsl:template> element and defining the name attribute, which is similar to a function name in traditional programming languages. Use the <xsl:param> tag and its name attribute to define parameters for the named template, and optionally include the select attribute to declare default values for each parameter. The select attribute can contain XPath expressions. If the select attribute is not defined, the parameter defaults to an empty string.

The following example creates a template named my-template and defines three parameters, one of which defaults to the string false, and one of which defaults to the contents of the element node named name that is a child of the current context node. If the script calls the template and does not pass in a parameter, the default value is used.

<xsl:template name="my-template">
    <xsl:param name="a"/>
    <xsl:param name="b" select="'false'"/>
    <xsl:param name="c" select="name"/>
     <!-- ... body of the template ... -->
</xsl:template>

To invoke a named template in a script, use the <xsl:call-template> element. The name attribute is required and defines the name of the template being called. When processed, the <xsl:call-template> element is replaced by the contents of the <xsl:template> element it names.

When you invoke a named template, you can pass arguments into the template by including the <xsl:with-param> child element and specifying the name attribute. The value of the <xsl:with-param> name attribute must match a parameter defined in the actual template; otherwise the parameter is ignored. Optionally, you can set a value for each parameter with either the select attribute or the content of the <xsl:with-param> element. If you do not define a value for the parameter in the calling environment, the script passes in the current value of the parameter if it was previously initialized, or it generates an error if the parameter was never declared. For more information about passing parameters.

In the following example, the template my-template is called with the parameter c containing the contents of the element node named other-name that is a child of the current context node.

<xsl:call-template name="my-template">
    <xsl:with-param name="c" select="other-name"/>
</xsl:call-template>
@2016-12-18 10:45:59

This example creates a block for a chapter element and then processes its immediate children.

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

In the absence of a select attribute, the xsl:apply-templates instruction processes all of the children of the current node, including text nodes.

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com