Easy to start with XSLT Chapter fourth: XPath syntax _xml/rss

Source: Internet
Author: User
Tags numeric value xpath xsl xsl file xslt

4.XPath of syntax

As we mentioned earlier, XPath is the language used to help XSLT find positioning information in an XML source document. In actual use, XPath and XSLT are always mixed together, and in the syntax examples in the previous chapter we already have the syntax to use XPath, just not explicitly. But the consortium divides them into two standards, so we also split them into two chapters for explanation.

4.XPath of syntax

4.1 Current Position
4.2 Addressing operations
4.3 operator
4.4 Functional functions

4.1 Current Position

When we use XSLT to process an XML source document, we use the context to represent the node location that is currently being processed by the template. For example, the xsl:template match= "/" statement indicates the context in the document's root node. I don't know how to translate the word context exactly, it's similar to a pointer in C that indicates where the program is currently running. Understanding the context is important for handling XSL templates correctly, and when your XSL template outputs documents that are different from what you want, the first thing you should analyze is where the context is.
Location paths is used to set the context node location you want to find. Directory commands similar to DOS. Let's look at an example.

<xsl:for-each select= "Child::P eople/descendant::P Erson" >

Where Child::P eople/descendant::P Erson is the XPath syntax, which is a location Paths that shows the child elements of all people elements and all the person elements. In general, we will use a simpler formulation:

<xsl:for-each select= "People//person" >

Let's explain two ways to represent path: "/" and "//".
"/" is a node that represents the current document, similar to a DOS directory separator. For example,/people represents the selection of the people element under the root node, and People/person represents all Peson child elements under people element selection.
"//" represents all nodes in the current document. Similar to view the entire directory. For example,//people represents the selection of all people elements in a document, regardless of its level, and People//person represents all the person elements under the people element, regardless of how deep it is.

4.2 Addressing operations

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

Axis Grammar Table
--------------------------------------------------------
Short description of expression
--------------------------------------------------------
Self. Select the current node ...
Example:
<td><xsl:value-of select= "." /></td>
The code represents inserting the text value of the current node in 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 the attributes of the person element.
--------------------------------------------------------
The child selects all children of the current node.
--------------------------------------------------------
Ancestor selects all the parent elements of the current node (including parent elements, and so on)
--------------------------------------------------------

Axis helps us select all the nodes around the current node, and predicate is used to locate the elements within the current node. The representation method is a bracket [] with an expression: [Expression]. Specific examples are as follows:

Person[position () =2]
This code means looking for a second "person" element

Person[starts-with (name, "B")]
This code represents finding a person element with all names beginning with "B".

4.3 operator

This section describes the XPath operator (Expressions), which is listed below:
--------------------------------------------------------
Operator description
--------------------------------------------------------
And, or is a common meaning and, or
--------------------------------------------------------
= equals
--------------------------------------------------------
!= is not equal to
--------------------------------------------------------
>= is greater than or equal to
--------------------------------------------------------
<= is less than, less than or equal to. Note: The,< symbol in the XSL file should be < represented
--------------------------------------------------------
+,-, *, div subtraction
--------------------------------------------------------
MoD Take Mold
--------------------------------------------------------
| Two nodes to compute together
--------------------------------------------------------

The

4.4 functional Function (functions)

has a number of functional functions in XPath that can help us pinpoint the nodes we need. The

count () function
Function: Statistics count, which returns the number of nodes that meet the criteria.
Example: <p><xsl:value-of select= "count (Person[name=tom])"/></P>
Description: The purpose of the code is to display the name attribute value for the person element as Tom has several. The

Number () function
Action: Converts the text in a property's value 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 () feature
Syntax: substring (value, start, length)
Action: intercepts a 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. Display from the first letter to the third.

sum () function
function: Sum.
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.

These features are just a subset of the XPath syntax, and a large number of functional functions are not covered, and XPath syntax is still evolving. These functions allow us to implement more complex queries and operations.

When we see this, our introductory tutorials are almost over. By quick Learning, I hope you have a basic idea of XSLT: XSLT is a language that transforms XML documents, and it contains two processes: transformation and formatting. XSLT is much more powerful than CSS, and it has syntax for similar data queries. If you are interested in XSLT, then the above knowledge is far from enough, you need to query more information. Atzie in the last chapter 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.