4.XPath syntax
We have already mentioned earlier, XPath is the language used to help XSLT to find location information in the XML source document. During the actual use, XPath and XSLT are always mixed together. In the above chapter, we already have the syntax used to use XPath, just not clearly. But W3C divides them into two standards, so we also dismantled them into two chapters to explain.
4.XPath syntax
4.1 Current location
4.2 Addressing
4.3 operator
4.4 Function Function
4.1 Current location
When we use the XSLT to process the XML source document, we use context to represent the node location that is currently being processed by the template. For example, the XSL: Template match = "/" statement indicates the root node of the document in the document. I don't know how to accurately translate the word word, which is similar to the pointer in the C language, indicating the location where the program is currently running. Understanding Context is very important to correctly handle the XSL template, when your XSL template outputs, the first thing you want, the first should be analyzed where CONTEXT is.
Location Paths is used to set the CONTEXT node location you want to find. Similar to the DOS directory command. Let's look at an example
Where Child :: People / Descendant :: Person is the XPath syntax, this expression is a location paths, code description to display the child elements of all people and the child elements of all Person elements. Usually we will use simpler ways:
Let's explain two representations of PATH: "/" and "//".
"/" Is a node that represents the current document, similar to the DOS directory split. For example: / people represents the people elements under the root node; People / Person represents all the Peson sub-elements under the people.
"//" means all nodes of the current document. Similar to view the entire directory. For example: // people indicate what the people have all in the document, whether it is what level; people // person represents all the Person elements under the people of the People, no matter how deep it.
4.2 Addressing
AXIS and PREDICATE are syntax for locating the Location Paths in XPath syntax, and the specific usage list is as follows
AXIS syntax table
-------------------------------------------------- --------
Expression
-------------------------------------------------- --------
Self. Select the current node..
Example:
The code represents the text (Text) value contained in the current node in the current location,
-------------------------------------------------- --------
Parent .. Select the parent node of the current node.
-------------------------------------------------- --------
Attribute @ Select all properties of an element.
example:
Select all properties of the Person element.
-------------------------------------------------- --------
Child Selects all child elements of the current node.
-------------------------------------------------- --------
Ancestor Select all parent elements of the current node (including the parent element of the parent element, push)
-------------------------------------------------- --------
AXIS helps us select all nodes around the current node, while Predicate is used to locate the elements within the current node. Representation is square bracket [] plus expression: [expression]. Specific examples are as follows:
Person [Position () = 2]
This code means looking for the second "person" element
Person [Starts-with (Name, "B")]]]
This code represents all the Person elements that start with "B".
4.3 operator
This section describes the EXPRESSIONS, the list is as follows:
-------------------------------------------------- --------
Operator description
-------------------------------------------------- --------
And, or is the ordinary meaning and OR
-------------------------------------------------- --------
= Equal to
-------------------------------------------------- --------
! = Not equal
-------------------------------------------------- --------
>,> = Greater than or equal to
-------------------------------------------------- --------
<, <= Less than, less than or equal. Note: In the XSL file, -------------------------------------------------- -------- , -, *, DIV reduction -------------------------------------------------- -------- MOD molding -------------------------------------------------- -------- | Two nodes together -------------------------------------------------- -------- 4.4 Functional Function (Functions) There are a lot of function functions in XPath to help us find the need for nodes. COUNT () function Role: Statistical count, returning the number of qualified nodes. Example: Explanation: The use of code is to display a few name attribute values in the Person element. Number () function Role: Convert text in the value of the property to a value. Example: The number is: Note: The use of code is the price of the display. Substring () Function Syntax: Substring (Value, Start, Length) Role: Intercept string. Example: Explanation: The use of code is to intercept the value of the NAME element, from the first letter to the third. SUM () function Role: sum find. Example: Total price = Note: The use of code is to calculate all the price and. The above features are only part of the XPath syntax, and there are a lot of functional functions that are not introduced, and the syntax of XPath is still growing. Through these functions we can achieve more complex queries and operations.