SoFunction
Updated on 2025-04-07

Chapter 4: XPath Syntax

Syntax of

As we mentioned earlier, XPath is a language used to help XSLT find location information in XML source documents. In actual use, XPath and XSLT are always used together. In the syntax example in the previous chapter, we already have the syntax to use XPath, but we didn't point it out clearly. But W3C divides them into two standards, so we also break them into two chapters to explain them.

Syntax of

4.1 Current location
4.2 Addressing Operation
4.3 Operators
4.4 Functions

4.1 Current location

When we use XSLT to process XML source documents, we use Context to represent the location of the node that is currently being processed by the template. For example, the xsl:template match="/" statement indicates that the Context is at the root node of the document. I don't know how to accurately translate the word Context, it is similar to a pointer in C language, indicating the current running position of the program. Understanding Context is very important for correctly processing XSL templates. When the document output of your XSL template is different from what you want, the first thing you should analyze is where the Context is.
Location Paths is used to set the location of the Context node you want to find. It's similar to the directory command of DOS. Let's look at an example

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

Where child::PEOPLE/descendant::PERSON is XPath syntax, and this expression is a Location Paths. The code indicates that all child elements of PEOPLE elements and all child elements of PERSON elements are displayed. Usually we use a simpler writing method:

<xsl:for-each select="PEOPLE//PERSON">

Let's explain two ways of representing path: "/" and "//".
"/" is the node that represents the current document, similar to the DOS directory splitter. For example: /PEOPLE means selecting PEOPLE elements under the root node; PEOPLE/PERSON means selecting all PESON sub-elements under the PEOPLE element.
"//" means all nodes in the current document. Similar to viewing the entire directory. For example: //PEOPLE means selecting all PEOPLE elements in the document, no matter what level it is; PEOPLE//PERSON means all PERSON elements under the PEOPLE element, no matter how deep it is.

4.2 Addressing Operation

Axis and Predicate are syntaxes for positioning Location Paths in XPath syntax. The specific usage list is as follows

Axis syntax table
--------------------------------------------------------
Expression Abbreviation Description
--------------------------------------------------------
self . Select the current node.
example :
<TD><xsl:value-of select="."/></TD>
The code indicates the text value contained in the current node is inserted at the current location.
--------------------------------------------------------
parent .. Select the parent node of the current node.
--------------------------------------------------------
attribute @ Select all attributes of an element.
example:
<TD><xsl:value-of select="@PERSONID"/></TD>
Select all properties of the PERSON element.
--------------------------------------------------------
child Selects all child elements of the current node.
--------------------------------------------------------
historytor Selects all parent elements of the current node (including the parent element of the parent element, and so on)
--------------------------------------------------------

Axis helps us select all nodes around the current node, while Predicate is used to locate elements inside the current node. The expression is to add expression in square brackets []: [Expression]. Specific examples are as follows:

PERSON[position()=2]
This code indicates the search for the second "PERSON" element

PERSON[starts-with(name, "B")]
This code indicates that all PERSON elements whose names start with "B".

4.3 Operators

This section introduces the operators (Expressions) of XPath, and the list is as follows:
--------------------------------------------------------
Operator Description
--------------------------------------------------------
and, or is the ordinary meaning and, or
--------------------------------------------------------
= equal to
--------------------------------------------------------
!= does not equal
--------------------------------------------------------
>, >= greater than, greater than or equal to
--------------------------------------------------------
<, <= less than, less than or equal to. Note: In XSL files, the < symbol should be represented by <
--------------------------------------------------------
+, -, *, div Addition, subtraction, multiplication, and division
--------------------------------------------------------
mod
--------------------------------------------------------
| Two nodes compute together
--------------------------------------------------------

4.4 Functions

There are many functional functions in XPath that can help us find the nodes we need accurately.

count() function
Function: count counting and return the number of nodes that meet the conditions.
Example: <p><xsl:value-of select="count(PERSON[name=tom])"/></p>
Description: The purpose of the code is to display how many names attribute values ​​are tom in the PERSON element.

number() function
Function: Convert text in the value of the attribute to a numeric value.
Example: <p>The number is: <xsl:value-of select="number(book/price)"/></p>
Description: The purpose of the code is to display the price of the book.

substring() function
Syntax: substring(value, start, length)
Function: Intercept the string.
Example: <p><xsl:value-of select="substring(name, 1, 3)"/></p>
Description: The purpose of the code is to intercept the value of the name element and display it from the first letter to the third one.

sum() function
Function: seek sum.
For example: <p>Total Price = <xsl:value-of select="sum(//price)"/></p>
Description: The purpose of the code is to calculate the sum of all prices.

The above functions are only part of the XPath syntax, and there are still a large number of functional functions that have not been introduced, and the current syntax of XPath is still developing. Through these functions, we can implement more complex queries and operations.

After seeing this, our introductory tutorial is almost over. Through quick and easy learning, I hope everyone should have a basic concept of XSLT: XSLT is a language for converting XML documents, which includes two processes: conversion and formatting. XSLT is much more powerful than CSS, and it has a syntax similar to data query. If you are interested in XSLT, the above knowledge is far from enough and you need to query more information. Ajie provides you with the main XSLT resources in the appendix of the last chapter.