SoFunction
Updated on 2025-04-08

Chapter 3: XSLT Element Syntax

Through the introduction of the previous two chapters, we have gained some understanding of the basic concepts of XSLT and its conversion process. Let’s learn the specific syntax of XSLT together. Speaking of syntax, you can browse it roughly and study them carefully when you really need to use XSLT.

Element syntax

3.1 xsl:template and xsl:apply-templates

3.2 xsl:value-of

3.3 xsl:for-each

3.4 xsl:if

3.5 Xxsl:choose, when, otherwise

3.6 xsl:sort

3.1 xsl:template and xsl:apply-templates


 

Template is one of the most important concepts in XSLT. An XSLT file is composed of templates one by one, and any XSLT file contains at least one template. The concept of templates is like building blocks; if you are a programmer, you can also regard templates as a method, a class, or a module. They can be assembled and combined, or they can be individually blocked, with different templates controlling different output formats.

A template consists of two parts: match pattern and execution. Simply speaking, schema defines which node in the XML source document will be processed by the template, and when executing, it defines what format the output is. The syntax corresponding to the two parts is xsl:template and xsl:apply-templates.


 

The syntax of xsl:template is:


 

<xsl:template

match = pattern

name = qname

priority = number

mode = qname>

<!-- Execution content -->

</xsl:template>


 

The purpose of xsl:template is to define a new template. Name, priority, and mode in the attribute are used to distinguish different templates that match the same node. They are not commonly used properties. The match attribute controls the template's pattern. The matching pattern is used to locate which node in the XML source document is processed by the template. A template matches a node. Let's use an example to help understand:

Suppose we are going to deal with a document containing chapters and paragraphs. We use the para element to define paragraphs and chapter elements to define chapters. Let's take a look at the possible values ​​of the match attribute. The following statements describe the template matching all para elements


 

<xsl:template match="para">

</xsl:template>


 

The following statement is written to illustrate that the template matches all para elements and all chapter elements:


 

<xsl:template match="(chapter|para)">

</xsl:template>


 

The following statement is written to illustrate that the template matches all para elements whose parent node is a chapter element:


 

<xsl:template match="chapter//para">

</xsl:template>


 

The following statements describe how the template matches the root node:


 

<xsl:template match="/">

</xsl:template>


 

Let's look at the apply-templates syntax:


 

<xsl:apply-templates

select = node set-expression

mode = qname>

</xsl:apply-templates>


 

xsl:apply-templates is used to execute which node is processed by the template. You can understand it as calling subfunctions in a program. The select property is used to define the exact node name. xsl:apply-templates is always included in the xsl:template element, like this:


 

<xsl:template match="/">

<xsl:apply-templates select="para"/>

</xsl:template>


 

This code shows that the touchpad matches the entire document (root node), and handles all para elements under the root node during execution.


 

<xsl:template match="para">

<p><xsl:apply-templates/></p>

</xsl:template>


 

This code means that the touchpad matches the para node, and all child elements under para will be processed.

3.2 xsl:value-of


 

XSL:value-of is used to write the text value of elements in the source document to the output document. For example:

There is an XML document for a profile:


 

<?xml version="1.0" encoding="iso-8859-1"?>

<PERSON>

<name>ajie</name>

<age>28</age>

</PERSON>


 

If I want to display the value of the name element in the above XML source document in the output document, I can write the XSLT code like this:


 

<xsl:template match="PERSON">

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

</xsl:template>


 

After execution, you will see "ajie" being displayed separately. Where match="PERSON" defines the touchpad to match the PERSON node, xsl:value-of
Syntax indicates that the value of a node needs to be output, and select="name" defines the element to be output as name. Is this process very similar to querying a person's name in the database? Of course, there are more and more complex syntaxes for xsl:value-of query, because it involves search and positioning, which we will explain in detail in the XPath syntax later.

The same function also has xsl:copy-of, which has the same usage, so I won't explain it repeatedly.

3.3 xsl:for-each


 

xsl:for-each syntax allows you to loop through selected nodes. For example: There is an XML document with multiple personal information:


 

<?xml version="1.0" encoding="iso-8859-1"?>

<PEOPLE>

<PERSON>

<name>ajie</name>

<age>28</age>

</PERSON>

<PERSON>

<name>tom</name>

<age>24</age>

</PERSON>

<PERSON>

<name>miake</name>

<age>30</age>

</PERSON>

</PEOPLE>


 

I need to display everyone's name, then I can write the XSLT code as:


 

<xsl:template match="PEOPLE">

<xsl:for-each select="child::PERSON">

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

</ xsl:for-each>

</xsl:template>


 

3.4 xsl:if


 

xsl:if is similar to the if condition statement in ordinary programming languages, allowing the setting node to be processed by the template when a certain condition is met. The syntax format of xsl:if is:


 

<xsl:if test=boolean expression>

template body

</xsl:if>


 

For example:


 

<xsl:template match="PEOPLE">

<xsl:if test="@name">

<p><xsl:value-of select="@name"/></p>

</xsl:if>

</xsl:template>


 

This code means detecting all elements under the PEOPLE node. If there is a <name> element, the value of the <name> element is output. Where the @ symbol is a general card, indicating all elements under the node.

3.5 xsl:choose, xsl:when and xsl:otherwise


 

xsl:if syntax does not have an else property. If we want to make multiple choices, we need to use the xsl:choose / xsl:when / xsl:otherwise series process control syntax. For specific use, please see the following XSL file example:


 

<xsl:template match="PEOPLE">

<xsl:choose>

<xsl:when test="@name = 'ajie'">

<B><xsl:value-of select="@name"/></B>

</xsl:when>

<xsl:when test="@name">

<I><xsl:value-of select="@name"/></I>

</xsl:when>

<xsl:otherwise>

No name available

</xsl:otherwise>

<xsl:choose>

</xsl:template>


 

Note: First, look for an element with ajie attribute value of ajie under the PEOPLE node. If it is found, output ajie in bold; if no <name> element with a value of ajie, output all the values ​​of all <name> elements in italics; if no <name> elements are found, display "No
name available"。


 

3.6 xsl:sort


 

In XSLT, the elements of the XML source document can be reordered, and the syntax of sorting is xsl:sort. For example: The following code is to sort the document elements by name.


 

<xsl:template match="PEOPLE">

<xsl:apply-templates select="PERSON">

<xsl:sort select="@name"/>

</xsl:apply-templates>

</xsl:template>


 

The above is the main syntax of XSLT elements, and there are many other syntaxes, such as import, include, element, attribute, number, param and other syntaxes, which will not be explained one by one here. Our goal is to give you a basic concept of XSLT's syntax and understand the power of XSLT as a conversion language.