XSL Tutorial: learn how to apply XPath

Source: Internet
Author: User
Tags xml attribute
What is XPathXPath (extensible path) is to provide a shared syntactic and semantic result for the shared functions of XSL Transformation [XSLT] and XPointer [XPointer. An important objective of XPath is to address XML document components. The same as supporting this important goal What is XPath?

XPath (extensible path) is the result of converting the shared functions of [XSLT] and XPointer [XPointer] to XSL to provide a shared syntax and semantics. An important objective of XPath is to address XML document components. While supporting this important goal, it also provides basic means for the control of strings, numbers, and Boolean. XPath applies concise and non-XML syntaxes so that XPath can be applied within the URIs and XML attribute values. XPath controls the abstract and logical structure of XML documents rather than its surface syntaxes, the XPath name is derived from the path tag used in the URL to navigate the hierarchical structure of the XML document.
In addition to addressing, XPath is also designed so that it can be used to match a natural subset (to test whether a node matches a pattern ); XSLT describes the application of XPath in this area.
XPath builds 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 each type of node. Some node types are also famous words. XPath fully supports the XML namespace [XML Names]. In this way, the name of a node is modeled as a pair consisting of a bureau city and a namespace URI that may be empty. this is called an extended name.

Type returned by XPath

1: node aggregation (unordered and non-repeated node aggregation)
2: Boolean (true or false)
3: Number (a floating point number)
4: string (the order of UCS characters)

Address path

The address path is a statement that Xpath uses to locate. the basic syntax is as follows:
/Select the root node of the XML document
/* Select all child nodes of the root node,
* Match any subnode
/X select all x elements of the root node
// Book selects the book element from all descendant nodes of the root node
// @ Id: Select the subnode with the id attribute
Para [1] select the first para child of the high/low file node
// Vendor [@ id = 'id1 _ 2']/book selects all book elements that match "attribute id = 'id1 _ 2 '"
/Bib/vendor/book [year> 2002] Select all book elements that match "element year> '123 '"
Text () select all text nodes of the high and low text nodes
@ Name: select the name attribute of the high/low file node.
@ * Select all the attributes of the high/low file node
*/Para selects all para grandsons of the high and low file nodes.
/Doc/chapter [5]/section [2] select the second section of the fifth chapter of doc
. // Para select the child of the para element of the high and low file nodes
Para [@ type = 'warning'] all para children with the attribute type and the value of warning selected for the high and low file nodes
Chapter [title = 'Introduction '] select the chapter child of the level-and-level node. if it has one or more title children and the string value is Introduction
Employee [@ secretary and @ assistant] select all the employee children with both secretary and assistant attributes on the high and low file nodes.
The chpater [title] node selected contains one or more chpater children with title


1: child is the default axis. For example, the address path div/para is the abbreviation of child: iv/child: para.
2: attributes can also be abbreviated @. For example, the address path para [@ type = 'warning'] is the abbreviation of child: para [attribute: type = 'warning, that is, select the para child with the type attribute and the property value is warning.
3: // is the abbreviation of/descendant-or-self: node. For example, // para is/descendant-or-self: node ()/child: para abbreviation. Therefore, select all para elements in the document (even if para is a document element, it will also be selected by // para, because the document element is the child of the root node); div // para is div/descendant-or-self: node ()/child :: the abbreviation of para. Therefore, all para children of the div will be selected.

Core function library

Node aggregation function

Last ()Returns a number that is used to evaluate the size of a high or low text from an expression.
Position ()The function returns a number equal to the number used to evaluate the position of a high or low text in an expression.
Count (node-set)Returns the number of nodes in the node-set parameter.
Id (object)Select the unique ID of the element
Local-name (node-set ?)Returns the local area of the extended name of a node.
Namespace-uri (node-set ?)Returns the namespace URI of the extended name of the first node in the parameter node aggregation in the document order. the string returned by the namespace-uri function except the element node and attribute node is null.
Name (node-set ?)Returns a string containing a QName, which represents the extended name of the first node in the parameter node aggregation in the document order, except for 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 functions

String (object ?)The function converts an object to a character.
Concat (string, string, string *)Function returns the join 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, false is returned.
Contains (string, string)If the first string parameter contains the second string parameter, the contains function returns true. otherwise, false is returned.
Substring-before (string, string)The function returns the substring of the first string parameter before the second parameter is rendered for the first time. Alternatively, if the first string parameter does not contain the second string parameter, an empty string is returned. For example, substring-before ('2014/1/01', '/') returns 1999/04
Substring-before (string, string)The function returns the substring of the first string parameter before the second parameter is rendered for the first time. Alternatively, if the first string parameter does not contain the second string parameter, an empty string is returned. For example, substring-before ('2014/1/01', '/') returns 1999/04
Substring (string, number, number? )The function returns the substring of the first string parameter starting from the position specified by the second parameter and taking the third parameter as the length. For example, substring ('20140901', 12345) returns '20160901 '. If there is no third parameter, the return value starts from the position specified by the second parameter until the end. For example, substring ('20140901', 2) returns '20160901'
String-length (string? )Returns the number of characters in a string.
Normalize-space (string ?)The function returns the canonicalized parameter string. this canonicalized parameter string is used to clear leading and ending vacant characters and to replace a continuous vacant character with a vacant character.
Translate (string, string, string)The function returns the string of the first parameter, in which all the characters displayed in the second parameter are replaced by the characters corresponding to the third parameter. For example, translate ('bar', 'ABC', 'ABC') returns the string bar. If the character in the second parameter has no relative position in the third parameter (because the character string in the second parameter is longer than the character string in the third parameter), then, the character in the first parameter will be moved. For example, 'AAA' is returned for 'translate ('-- aaa --', 'ABC-', 'ABC '. If the character in the second parameter is displayed more than once, the first representation determines the character to be replaced. If the string in the third parameter is longer than the string in the second parameter, excessive characters will be neglected.

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.