What is XPath?
XPath (Extensible path) is a shared function that converts [XSLT] And XPointer [XPointer] To XSL to provide a shared syntax and semantics. The main objective of XPath is to address XML document components. While supporting this main purpose, it also provides basic means for string, number, and Boolean operations. XPath uses concise and non-XML syntaxes so that XPath can be used within the URIs and XML Attribute values. XPath operates the abstract and logical structure of XML documents rather than its surface syntaxes, the name of XPath is derived from the path flag 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 use of XPath in this regard.
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 also have names. 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 extension.
Type returned by XPath
1: node set (unordered and non-repeated node set)
2: Boolean (true or false)
3: Number (a floating point number)
4: string (the order of the 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 context 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 context node
@ Name: select the name attribute of the context node.
@ * Select all attributes of the context node
*/Para Selects all para grandsons of the context node.
/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 context node
Para [@ type = "warning"] All para children with the property type and value of warning on the selected context node
Chapter [title = "Introduction"] select the chapter child of the context node. If it has one or more title children and the string value is Introduction
Employee [@ secretary and @ assistant] select all employee children with both secretary and assistant attributes on the context node.
The chpater [title] selected context node 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 Set Functions
Last () returns a number, which is equal to the context size in the context evaluated from the expression
The position () function returns a number equal to the value of the context position in the expression.
The count (node-set) function returns the number of nodes in the node-set parameter.
Id (object) selects the unique ID of an element
Local-name (node-set ?) Returns the local area of a node extension.
Namespace-uri (node-set ?) Returns the namespace URI of the extension of the first node in the node set according to the document sequence. 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 extension of the first node in the set of parameters in the document sequence, except for element nodes and attribute nodes, 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.
The 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
Contains (string, string) if the first string parameter contains the second string parameter, the contains function returns true. Otherwise, false
The substring-before (string, string) function returns the substring of the first string parameter before the first occurrence of the second parameter, or, if the first string parameter does not contain the second string parameter, an empty string is returned. For example, substring-before ("", "/") returns 1999
The substring-before (string, string) function returns the substring of the first string parameter before the first occurrence of the second parameter, or, if the first string parameter does not contain the second string parameter, an empty string is returned. For example, substring-before ("", "/") returns 1999
Substring (string, number, number? ) 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 ("12345", 234) returns ". If there is no third parameter, return from the second parameter specified position until the end. For example, substring ("12345", 2) returns "2345"
String-length (string? Returns the number of characters in the string.
Normalize-space (string ?) The function returns the canonicalized parameter string. This canonicalized parameter string is used to clear leading and ending white spaces and replace consecutive white spaces with a blank character.
The translate (string, string, string) function returns the string of the first parameter. The characters that appear in the second parameter are replaced by the 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 does not have a character at the corresponding position of the third parameter (because the character string in the second parameter is longer than the character string of 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, the first occurrence determines the character to be replaced. If the string in the third parameter is longer than the string in the second parameter, extra characters will be ignored.
Boolean Function
Boolean (object)
Not (boolean)
True ()
False ()
Lang (string)
Numeric Functions
Number (object ?) Function parameters are converted to numbers according to the following items:
Sum (node-set) for each node in the parameter node set, the sum function returns the sum
The floor (number) function returns the maximum number (closest to positive infinity) not greater than the integer of a parameter)
The ceiling (number) function returns the minimum number (closest to negative infinity) not small to the integer of the parameter)
The round (number) function returns an integer closest to the parameter. If there are two such numbers, return the one closest to positive infinity. If the parameter is NaN, NaN is returned. If the parameter is positive infinity, positive infinity is returned. If the parameter is negative infinity, negative infinity is returned. If the parameter is positive zero, positive zero is returned. If the parameter is negative zero, negative zero is returned. If the parameter is less than zero, but greater than or equal to-0.5, negative zero is returned.
Reference Specification
XML See http://www.w3.org/TR/1998/REC-xml-19980210
XML Nameshttp: // www.w3.org/TR/REC-xml-names
XSLT See http://www.w3.org/TR/xslt
Refer to XML Path Language (XPath) Version 1.0.