The data and node types of XPath and the basic method of node matching in XPath

Source: Internet
Author: User
Tags define cdata contains functions numeric string xpath xpath contains
XPath data type
XPath can be divided into four types of data:
Node Set (node-set)
A node set is a set of nodes that match the criteria returned by a path. Other types of data cannot be converted to node sets.
Boolean Value (Boolean)
A conditional matching value returned by a function or Boolean expression, the same as a Boolean value in the general language, with true and false two values. Boolean values can be converted to and from numeric types and string types.
Strings (String)
A string is a collection of a series of characters that provides a series of string functions in XPath. Strings can be converted to data from numeric types, Boolean types, and other values.
Value (number)
The values in XPath are floating-point numbers, which can be double-precision 64-bit floating-point numbers. It also includes some special descriptions of numerical values, such as Non-numeric nan (not-a-number), positive infinity infinity, negative infinity-infinity, plus or minus 0, and so on. The integer value of number can be obtained by a function, and numeric values can also be converted to and from Boolean types and string types.
The latter three types of data are similar to the corresponding data types in other programming languages, except that the first data type is a unique product of the XML document tree.

XPath node type
Because XPath contains a series of operations on the document tree, it is also necessary to know the XPath node type. In the logical structure of an XML document, an XML file can contain elements, CDATA, annotations, processing instructions, and other logical elements, in which elements can also contain attributes, and attributes can be used to define namespaces. Accordingly, in XPath, the nodes are divided into seven node types:
Root node
The root node is the topmost of a tree, and the root node is unique. All other element nodes on the tree are its child nodes or descendant nodes. The processing mechanism for the root node is the same as for other nodes. The matching of trees in XSLT always starts with the root node first.
Elements node (element Nodes)
ELEMENT nodes correspond to each element in the document, and the child nodes of an element node can be element nodes, annotation nodes, processing instruction nodes, and text nodes. You can define a unique identity ID for the element node. An element node can have an extension, which is made up of two parts: a namespace URI and a local name.
Text node (Nodes)
A text node contains a set of character data, which is the character contained in CDATA. No text node has an adjacent sibling text node and the text node does not have an extension.
Property node (attribute Nodes)
Each element node has an associated set of attribute nodes, which are the parent of each attribute node, but the attribute node is not a child of its parent element. This means that by looking for the child nodes of an element, you can match the attribute nodes of the element, but in turn it is not, just one-way. Again, the attribute nodes of an element are not shared, meaning that different element nodes do not share the same attribute node.
Handling of default properties is equivalent to a defined property. If a property is declared in a DTD, but is declared as #IMPLIED and the property is not defined in the element, the attribute node set of that element does not contain it.
In addition, there are no namespace declarations for attribute nodes that correspond to attributes. A namespace attribute corresponds to a node of another type.
Namespace node (Namespace Nodes)
Each element node has a set of related namespace nodes. In XML documents, namespaces are declared by retaining attributes, so in XPath, the class nodes are very similar to the property nodes, and their relationship to the parent element is one-way and not shared.
Processing instruction node (processing instruction Nodes)
The processing instruction node corresponds to each processing instruction in the XML document. It also has an extension, the local name of the extension points to the processing object, and the namespace part is empty.
Annotation node (Comment Nodes)
Note nodes correspond to the comments in the document.
an XML document Tree
Let's construct an XML document tree, which is supported by the following examples:
Copy CodeThe code is as follows:
<a id= "A1" >
<b id= "B1" >
<c id= "C1" >
<b name= "B"/>
<d id= "D1"/>
<e id= "E1"/>
<e id= "E2"/>
<b id= "B2"/>
<c id= "C2" >
<d id= "D2"/>

here are some basic ways to match nodes in XPath.
Path matching
Path matching is similar to the representation of the file path, which is better understood. There are several symbols:
(1) using "/" to indicate the node path
"/A/C/D" means the child node "D" of the child node "C" of Node "a", that is, the D node with the ID value of D2, "/" represents the root node.
(2) an element with "//" that indicates the end of all paths to the specified subpath after "//"
If "//e" represents all e elements, the result is all three e elements, such as "//c/e", which represents all the e elements of the parent node C, and the result is two e elements of the ID value E1 and E2.
(3) using "*" to indicate the wildcard character of the path
For example, "/a/b/c/*" represents all child elements under the element →c elements of A →b element, that is, the B element with a name value of B, the D element with an ID value of D1, and the two e elements of E1 and E2 with ID values
"/*/*/d" represents the D element with a level two node above, and the result is a D element with an ID value of D2, such as "//*" that represents all the elements.
Position matching
For each element, its child elements are ordered.
For example:/a/b/c[1] represents the first child element of the →b element →c element of A element, and gets the B element with the name value B
/a/b/c[last ()] represents the last child element of the →b element →c element of A element, and the e element with the ID value of E2
/a/b/c[position () >1] represents the element with a position number greater than 1 under the element →c elements of element a →b, with the D element with the ID value of D1 and the two E element with an ID value
Properties and Property values
You can use attributes and property values in XPath to match an element, and note that the attribute name of an element must be preceded by a "@" prefix. For example:
B[@id] represents all B elements with a property ID, resulting in two B elements with an ID value of B1 and B2
B[@*] represents all B elements with attributes, resulting in two B elements with ID attributes and one with the name attribute B element
B[not (@*)] represents all B elements that do not have attributes, and the result is a B element under the →c element of a element
b[@id = "B1"] ID value is b1 b element, resulting in the B element under a element
Family relationship Matching
XML documents can be grouped into tree structures, so any one node is not isolated. Usually we attribute the relationship between the nodes to a kinship, such as Father, child, ancestor, descendant, brother, etc. These concepts can also be used when matching elements. For example:
E/parent::* represents the parent node element of all E nodes, resulting in a element with an ID value of A1 and a C element with an ID value of C1
F/ancestor::* represents the ancestor node element of all F elements, resulting in a element with an ID value of A1 and a C element with an ID value of C2
/a/child::* represents the child element of a, with the result that the ID value is b1, b2 b element, the ID value is C2 c element, and the e element without any attributes
/a/descendant::* represents all descendant elements of a, resulting in all other elements except a element
F/self::* represents all F's own elements, and the result is the F element itself
F/ancestor-or-self::* represents all f elements and its ancestor node elements, the result is f element, f element's parent node C element and a element
/a/c/descendant-or-self::* represents all the →c elements of the A and their descendant elements, resulting in the C element with the ID value C2, the child elements B, D, f elements of the element
/a/c/following-sibling::* represents the next sequence of all sibling elements of a element →c, resulting in an e element without any attributes
/a/c/preceding-sibling::* represents the immediately preceding all sibling node elements of the →c element of A element, resulting in two B elements with an ID value of B1 and B2
/a/b/c/following::* represents all elements of the following sequence of elements of a →b element of element A, the result is a B element with an ID of B2, a C element with no attributes, a B element with no attributes, a D element with a D2 ID, an F element without attributes, an e element without attributes.
/a/c/preceding::* represents all elements before the →c element of A element, the result is b element with ID B2, e element with ID E2, e element with ID e1, D element with id D1, b element with name B, C element with ID C1, b element with ID B1
Conditional matching
Conditional matching is the use of the Boolean value of the results of some functions to match the nodes with the conditions. The functions commonly used in conditional matching have four classes: node function, string function, numeric function, Boolean function. For example, last (), position (), and so on, here we will not repeat.
Of the above matching methods, the most used number of paths matching. In the example of the previous chapter style sheet, either in the statement <xsl:template match= "student Roster" >, or in the statement <xsl:value-of select= "name"/>, is based on the given subpath relative to the current path to locate the node.
There are some detailed descriptions and usages of XPath: (vs.85). aspx

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: 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.