Finally, let’s find out how XSLT can transform an XML document to another document.
1. Processing Model
A list of source nodes is processed to create a result tree fragment.
- A node is processed by finding all the template rules with patterns that match the node, and choosing the best amongst them.
- The chosen rule’s template is then instantiated with the node as the current node and with the list of source nodes as the current node list.
- A template typically contains instructions that select an additional list of source nodes for processing. The process of matching, instantiation and selection is continued recursively until no new source nodes are selected for processing.
- When a template is instantiated, it is always instantiated with respect to a current node and a current node list. The current node is always a member of the current node list. The operations are relative to the current node.
Template rules identify the nodes to which they apply by using a pattern.
A pattern specifies a set of conditions on a node. A node that satisfies the conditions matches the pattern. The syntax for patterns is a subset of the syntax for expressions. In particular, location paths that meet certain restrictions can be used as patterns.
3. Defining Template Rules
A template rule is specified with the xsl:template element.
<xsl:template match="pattern" name="qname" priority="number" mode="qname"> ... </xsl:template>
- match : a pattern that identifies the source node or nodes to which the rule applies
4. Applying Template Rules
The <xsl:apply-templates /> instruction processes all of the children of the current node, including text nodes.
A “select” attribute can be used to process nodes selected by an expression instead of processing all children. The value of the “select” attribute is an expression. The expression must evaluate to a node-set. The selected set of nodes is processed in document order, unless a sorting specification is present.
<xsl:template match="book"> <xsl:apply-templates select="author"/> </xsl:template>
It is possible for a source node to match more than one template rule. A template with a high priority is chosen over a template with a lower priority. You can assign a template a priority using the priority attribute. The priority attribute can be set to any number.
<xsl:template match="para" priority="-1">
If you don’t specify a priority attribute, the XSLT processor assign the template the default priority.
- Patterns that match a class of nodes which matches all elements(such as ‘ * ‘) are assigned as -0.5.
- Patterns that match nodes according their name(such as ‘author’) are assigned as 0
- Patterns that match nodes according their context(such as ‘book/author’) are assigned as 0.5
Modes allow an element to be processed multiple times, each time producing a different result.
Both <xsl:template> and <xsl:apply-templates> have the optional “mode” attribute.
- If an <xsl:apply-templates> element has a mode attribute, then it applies only to those template rules fromelements that have a mode attribute with the same value
- If an <xsl:apply-templates> element does not have a mode attribute, then it applies only to those template rules fromelements that do not have a mode attribute
<xsl:template match="book"> <xsl:apply-templates select="title" mode="content"/> </xsl:template> <xsl:template match="title"> <h1><xsl:apply-templates/></h1> </xsl:template> <xsl:template match="title"> <h3><xsl:apply-templates mode="content"/></h3> </xsl:template>
7. Built-in Templates
If a processor can’t find a template that matches a node, it uses the built-in template for that node type.
- The built-in template for the elements :
<xsl:template match="*"> <xsl:apply-templates /> </xsl:template>
- the built-in template for text nodes :
<xsl:template match="text()"> <xsl:apply-templates select="."/> </xsl:template>
8. <xsl:if> Element
The <xsl:if> element takes a single attribute, “test“, which holds an XPath expression. If the XPath expression evaluates to true, then the content of <xsl:if> element is processed, otherwise nothing happens.
<xsl:if test="..."> ... </xsl:if>
<xsl:if test="name/@gender='female'"> <xsl:value-of select="name" /> is a woman. </xsl:if>
XSLT doesn’t have if-else-if statement, but you can use <xsl:choose> element. The <xsl:choose> element contains a number of <xsl:when> elements, which can be followed by an <xsl:otherwise> element. Each of the <xsl:when> elements tests for a particular condition with a test attribute that works in exactly the same way as the test attribute on <xsl:if>
<xsl:choose> <xsl:when test="..."> ... </xsl:when> <xsl:when test="..."> ... </xsl:when> ... <xsl:otherwise> ... </xsl:otherwise> </xsl:choose>
<xsl:choose> <xsl:when test="name/@gender='female'"> Woman </xsl:when> <xsl:when test="name/@gender='male'"> Man </xsl:when> <xsl:otherwise> What? </xsl:otherwise> </xsl:choose>
10. “test” Attribute
The “test” attribute of elements and elements holds the XPath expression. You can test elements and attributes using Xpath operators and functions.