SoFunction
Updated on 2025-03-03

Getting started with XPath - XSL Tutorial - 3

What is XPath
XPath (extended path) is a shared syntax and semantic result for the sharing function of XSL conversion [XSLT] and XPointer [XPointer]. The main purpose of XPath is to address XML document components. While supporting this main purpose, it also provides basic means for the operation of strings, numbers and booleans. XPath uses concise, non-XML syntax to facilitate the use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of the XML document rather than its surface syntax. The name of XPath comes from its use in URL as a path flag to navigate the hierarchical structure of the XML document.
In addition to being used for addressing, XPath is also designed so that it has a natural subset that can be used to match (testing whether a node matches a pattern); the use of XPath in this regard is described in XSLT.
XPath Models an XML document into a node tree with different types of nodes, including element nodes, attribute nodes and body nodes. XPath defines a method to calculate the string value of nodes of each class. Some node types also have names. XPath fully supports XML namespace [XML Names]. In this way, the name of the node is modeled as a pair consisting of a city part and a potentially empty namespace URI; this is called an extension.

The type returned by XPath
1: Node collection (unordered, repetitive node collection)
2: Boolean (true or false)
3: Number (a floating point number)
4: String (order of UCS characters)
Address path
The address path is a statement used by Xpath for positioning. The basic syntax is as follows:
/ Select the root node of the XML document
/* Select all child nodes of the root node,
*Match any child node
/x Select all x elements of the root node
//book Select book elements in all descendant nodes of the root node
//@id Select child nodes containing id attribute
para[1] The first para child who selects the context node
//vendor[@id='id1_2']/book Select all book elements that meet "attribute id='id1_2'"
/bib/vendor/book[year>2002] Select all book elements that match "element year>'2002'"
text() Select all text nodes of the context node child
@name Select the name attribute of the context node
@* Select all properties of the context node
*/para Select all para grandchildren of the context node
/doc/chapter[5]/section[2] Select the fifth chapter of doc and the second section of the second section of the fifth chapter of doc
.//para Select the para element descendants of the context node
para[@type="warning"] All para children with attributes and value warning of the selected context node
chapter[title="Introduction"] Child of the context node. If it has one or more titles, the child and the string value is Introduction
employee[@secretary and @assistant] Select all employees children with both the secretary  attribute and the assistant  attribute of the context node.
chapter[title] Chpater child with one or more title in the context node selected
1: child is the default axis. For example, the address path div/para is the abbreviation of child:iv/child::para.
2: Attribute also has abbreviation: attribute:: can be abbreviated as @. For example, the address path para[@type="warning"] is the abbreviation of child::para[attribute::type="warning"], that is, select a para child with a type attribute and the attribute value is warning.
3:// is the abbreviation of /descendant-or-self::node()/. For example, //para is the abbreviation of /descendant-or-self::node()/child::para, so all para elements in the document are selected (even if the para element is a document element, it will be selected by //para because the document element is the child of the root node); div//para is the abbreviation of div/descendant-or-self::node()/child::para, so all para descendants of div children will be selected
Core function library
Node collection function
last() returns a number equal to the context size in the evaluation context from the expression
The position() function returns a number that is equal to the context position in the context of the evaluation from the expression.
count(node-set) function returns the number of nodes in the parameter node-set
id(object) Select them by the unique ID of the element
local-name(node-set?) returns the local part of the extension of a node
namespace-uri(node-set?) returns the namespace URI of the extension of the first node in the node collection according to the document order. In addition to element nodes and attribute nodes, the string returned by the function namespace-uri will be empty.
name(node-set?) returns a string, the string contains a QName, which represents the extension of the first node in the node collection in the document order. In addition to the element node and attribute node, the string returned by the name function will be the same as the string returned by the local-name function.
String function
The string(object?) function converts an object into a character.
concat(string, string, string*) function returns the connection of its parameters
starts-with(string, string) If the first string parameter starts with the second string parameter, the starts-with function returns true, otherwise, return false
contains(string, string) If the first string parameter contains the second string parameter, the contains function returns true, otherwise, return false
The substring-before(string, string) function returns the substring before the first string parameter appears, or returns an empty string if the first string parameter does not contain the second string parameter. For example, substring-before("1999/04/01","/") return 1999
The substring-before(string, string) function returns the substring before the first string parameter appears, or returns an empty string if the first string parameter does not contain the second string parameter. For example, substring-before("1999/04/01","/") return 1999
The substring( string , number , number? ) function returns the first string parameter starting from the position specified by the second parameter and taking the third parameter as the length substring. For example, substring("12345",2,3) returns "234". If there is no third parameter, it returns to start from the position specified by the second parameter until the end. For example, substring("12345",2) returns "2345"
string-length( string? ) Returns the number of characters in the string
Normalize-space(string?) function returns the parameter string after the normalization of the blank character, which clears the leading and ending blank characters and replaces continuous blank characters with a blank character.
The translate(string, string, string) function returns the string of the first parameter, where all characters appear in the second parameter are replaced by characters at the corresponding position in the third parameter. For example, translate("bar","abc","ABC") returns the string BAr. If the character in the second parameter has no characters at the corresponding position of the third parameter (because the string in the second parameter is longer than the string in the third parameter), then the character in the first parameter will be removed. For example, translate("--aaa--","abc-","ABC") returns "AAA". If the character in the second parameter appears more than once, then the first occurrence determines the replaced character. If the string in the third parameter is longer than the string in the second parameter, then the extra characters will be ignored
Boolean functions
boolean(object)
not(boolean)
true()
false()
lang(string)
Number functions
Number(object?) function parameters are converted into numbers according to the following items
sum(node-set) For each node in the parameter node set, the sum function returns the sum after the node string value is converted into a number
The floor(number) function returns the maximum number of integers not larger than the parameter (closest to positive infinity)
ceiling(number) function returns the smallest number of integers not smaller than the parameter (closest to negative infinity)
The round(number) function returns the integer closest to the parameter. If there are two such numbers, then return the one closest to the positive infinity. If the parameter is NaN, then NaN is returned. If the parameter is positive infinity, then return positive infinity. If the parameter is negative infinity, then negative infinity is returned. If the parameter is positive zero, then positive zero is returned. If the parameter is negative zero, negative zero is returned. If the parameter is less than zero, but is greater than or equal to -0.5, then negative zero is returned.
Refer to the specifications
XML See See http:///TR/1998/REC-xml-19980210
XML Nameshttp:///TR/REC-xml-names
XSLT See http:///TR/xslt
Partial reference XML Path Language (XPath) Version 1.0