Chapter 4: XPath syntax

Source: Internet
Author: User
Tags xsl xsl file xslt

4. XPath syntax

As we have mentioned earlier, XPath is a language used to help XSLT search for location information in XML source documents. In actual use, XPath and XSLT are always used together. In the above chapter, we have used the XPath syntax, but it is not clear. However, W3C divides them into two standards, so we also split them into two chapters for explanation.

4. XPath syntax

4.1 current location
4.2 addressing operations
4.3 Operator
4.4 Function

4.1 current location

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

<XSL: For-each select = "Child: People/descendant: Person">

Child: People/descendant: person is the XPath syntax, and this expression is a location paths,CodeDisplays the child elements of all people elements and the child elements of all person elements. We usually adopt a simpler method:

<XSL: For-each select = "People/person">

We will explain two methods of path representation: "/" and "//".
"/" Indicates the node of the current document, similar to the DOS directory delimiter. For example,/People indicates selecting the people element under the root node; people/person indicates Selecting All the peson sub-elements under the People element.
"//" Indicates all nodes in the current document. Similar to viewing the entire directory. For example, // People indicates to select all the people elements in the document, no matter what level it is. People // person indicates all the person elements under the People element, no matter how deep it is.

4.2 addressing operations

Axis and predicate are the syntax for locating location paths in XPath syntax. The specific usage list is as follows:

Axis syntax table
--------------------------------------------------------
Abbreviated expression description
--------------------------------------------------------
Self. Select the current node ..
Example:
<TD> <XSL: value-of select = "."/> </TD>
The Code inserts the text value of the current node at the current position,
--------------------------------------------------------
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 attributes of the person element.
--------------------------------------------------------
Child Selects all child elements of the current node.
--------------------------------------------------------
Ancestor Selects all the 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 the elements inside the current node. The expression in [] is [expression] in square brackets. For example:

Person [position () = 2]
This code indicates searching for the second "person" element

Person [starts-with (name, "B")]
This code searches for all the person elements whose names start with "B.

4.3 Operator

This section describes the XPath operators (expressions). The list is as follows:
--------------------------------------------------------
Operator description
--------------------------------------------------------
And, or is the common meaning and, or
--------------------------------------------------------
= Equal
--------------------------------------------------------
! = Not equal
--------------------------------------------------------
>,>= Greater than, greater than or equal
--------------------------------------------------------
<, <= Less than, less than or equal. Note: In the XSL file, the <symbol must be expressed as <
--------------------------------------------------------
+,-, *, Div addition, subtraction, multiplication, division
--------------------------------------------------------
MoD modulo
--------------------------------------------------------
| Two nodes are computed together.
--------------------------------------------------------

4.4 Functions)

Many functions in XPath can help us find the desired node precisely.

Count ()
Purpose: count the number of nodes that meet the condition.
Example: <p> <XSL: value-of select = "count (person [name = Tom])"/> </P>
Note: The purpose of the code is to display the number of Tom attributes in the person element.

Number () function
Purpose: Convert the text in the attribute value to a value.
Example: <p> The number is: <XSL: value-of select = "number (book/price)"/> </P>
Note: The purpose of the code is to display the book price.

Substring ()
Syntax: substring (value, start, length)
Purpose: intercept a string.
Example: <p> <XSL: value-of select = "substring (name, 1, 3)"/> </P>
Note: The purpose of the code is to take the value of the name element and display it from the first letter to the third.

Sum () function
Purpose: sum.
Example: <p> total price = <XSL: value-of select = "sum (// price)"/> </P>
Note: The purpose of the code is to calculate the sum of all prices.

The above functions are only part of the XPath syntax. A large number of functional functions are not introduced yet, and the XPath syntax is still evolving. Through these functions, we can implement more complex queries and operations.

here, our getting started tutorial is almost over. Through rapid learning, we hope that you will have a basic concept of XSLT: XSLT is a language for converting XML documents. It contains two processes: conversion and formatting. XSLT is much more powerful than CSS. 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. In the appendix of the last chapter, Alibaba Cloud provides you with the main XSLT resources.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.