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

Source: Internet
Author: User
Tags cdata processing instruction types of functions xsl xpath contains

XPath Data Type
XPath can be divided into four data types:
Node-set)
A node set is a set of nodes that meet the conditions returned by path matching. Other types of data cannot be converted to a node set.
Boolean)
The condition matching value returned by a function or Boolean expression is the same as the Boolean value in a general language and has two values: true and false. Boolean values can be converted to numeric and string types.
String)
A string is a collection of characters. XPath provides a series of string functions. A string can be converted to data of the numeric or boolean type.
Number)
In XPath, the value is a floating point number, which can be a 64-bit double-precision floating point number. In addition, it includes some special descriptions of numerical values, such as non-numerical NaN (Not-a-Number), positive infinity, negative infinity-infinity, and positive and negative 0. The integer value of number can be obtained through the function. In addition, the value can also be converted to boolean and string types.
The last three data types are similar to the corresponding data types in other programming languages, but 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 structure tree, it is also necessary to understand the XPath node type. In the logical structure of an XML document, an XML file can contain elements, CDATA, comments, processing instructions, and other logical elements. The elements can also contain attributes and define namespaces using attributes. Correspondingly, in XPath, nodes are divided into seven node types:
Root Node)
The root node is the top layer of a tree, and the root node is unique. All other element nodes on the tree are their child nodes or descendant nodes. The root node is processed in the same way as other nodes. In XSLT, tree matching always starts from the root node.
Element Node)
An element node corresponds to every element in the document. A child node of an element node can be an element node, a comment node, a processing command node, and a text node. You can define a unique id for an element node. Each element node can have an extension. It consists of two parts: one is the namespace URI and the other is the local name.
Text Node)
A text node contains a set of character data, that is, the characters contained in CDATA. No text node is adjacent to any sibling text node, and the text node has no extension.
Attribute Nodes)
Each element node has an associated set of attribute nodes. The element is the parent node of each attribute node, but the attribute node is not a child node of its parent element. This means that the child node of the element can match the attribute node of the element, but in turn it is not true, only one-way. Furthermore, attribute nodes of elements are not shared, that is, different element nodes do not have the same attribute node.
Processing of default properties is equivalent to defining properties. If an attribute is declared in DTD but declared as # IMPLIED, and this attribute is not defined in the element, the attribute node of the element does not contain this attribute.
In addition, the attribute nodes corresponding to the attribute do not have namespace declarations. The namespace attribute corresponds to another type of node.
Namespace Node)
Each element node has a related namespace node set. In XML documents, namespaces are declared by retaining attributes. Therefore, in XPath, such nodes are very similar to attribute nodes, and their relationships with parent elements are unidirectional, it is not shared.
Processing Instruction Nodes)
The processing command node corresponds to each processing command 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.
Comment Nodes)
The comment node corresponds to the comment in the document.
An XML Document Tree
Let's construct an XML document tree based on the following example: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"/>
</C>
</B>
<B id = "b2"/>
<C id = "c2">
<B/>
<D id = "d2"/>
<F/>
</C>
<E/>
</A>

The following describes some basic methods for node matching in XPath.
Path Matching
Path Matching is similar to the expression of the file path, which is easy to understand. There are several symbols:
(1) Use "/" to indicate the Node path
For example, "/A/C/D" indicates the subnode "D" of "A" subnode "C", that is, the D node with the id value of d2, "/" indicates the root node.
(2) "//" indicates all elements whose paths end with the specified sub-path after "//"
For example, "// E" indicates all Eelements, and the result is all three Eelements. For example, "// C/E" indicates all Eelements whose parent node is C, the result is an Eelement with an id of e1 and e2.
(3) Use "*" to indicate path wildcards
For example, "/A/B/C/*" indicates all sub-elements under Element A → Element B → element C, that is, elements B whose name is B, D whose id is d1, and ewhose id is e1 and e2
"/*/D" indicates the D element of the above two nodes. The matching result is the D element with the id value of d2. For example, "// *" indicates all elements.
Location match
For each element, its child elements are ordered.
For example,/A/B/C [1] indicates the first child element of Element A → Element B → element C, and obtains the B element whose name is B.
/A/B/C [last ()] indicates the last child element of Element A → Element B → element C. The Eelement with the id of e2 is obtained.
/A/B/C [position ()> 1] indicates Element A → Element B → element with A position number greater than 1 under Element C, get the D element with the id value of d1 and two Eelements with the id value.
Attributes and attribute values
In XPath, attributes and attribute values can be used to match elements. Note that the attribute names of elements must have a "@" prefix before them. For example:
// B [@ id] indicates all B elements with the property id. The result is two B elements whose id values are b1 and b2.
// B [@ *] indicates all B elements with attributes. The result is two B elements with the id attribute and one B element with the name attribute.
// B [not (@ *)] indicates all B elements without attributes. The result is element A → Element B under Element C.
// B [@ id = "b1"] Element B whose id is b1 and the result is Element B under Element
Kinship match
XML documents can be categorized into tree structures, so no node is isolated. Generally, we define the attribution relationship between nodes as a kinship, such as parent, child, ancestor, descendant, and brother. These concepts can also be used for element matching. For example:
// E/parent: * indicates the parent node element of all E nodes. The result is the element whose id value is a1 and the C element whose id value is c1.
// F/ancestor: * indicates the ancestor node elements of all F elements. The result is the element whose id is a1 and the C element whose id is c2.
/A/child: * indicates the child element of A. The result is the B element whose id value is b1 and b2, the C element whose id value is c2, And the Eelement without any attribute.
/A/descendant: * indicates all descendant elements of A. The result is all elements except element.
// F/self: * indicates all the elements of F, and the result is the F element.
// F/ancestor-or-self: * indicates all F elements and their ancestor node elements. The result is F, and.
/A/C/descendant-or-self: * indicates all A elements → C elements and their descendant elements, the result is the C element with the id value c2, the child elements B, D, and F of the element.
/A/C/following-sibling: * indicates that element A → all sibling node elements in the back sequence of Element C, and the result is an Eelement without any attribute.
/A/C/preceding-sibling: * indicates all the sibling node elements adjacent to element A → element C. The result is two B elements whose id value is b1 and b2.
/A/B/C/following: * indicates all elements in the descending order of Element A → Element B → element C, the result is B element with id b2, C element without attributes, B element without attributes, D element with id d2, F element without attributes, and Eelement without attributes.
/A/C/preceding: * indicates all elements before Element A → element C, result: B element with id b2, Eelement with id e2, Eelement with id e1, D element with id d1, B element with name B, C element with id c1 element B whose id is b1
Condition match
Conditional matching is to use the Boolean values of some functions to match nodes that meet the conditions. The following types of functions are commonly used for condition matching: node functions, string functions, numeric functions, and Boolean functions. For example, last () and position (). We will not repeat them here.
Among the above matching methods, the maximum number of Path Matching is also used. In the example of the style sheet in the previous chapter, whether in the statement <xsl: template match = "student roster"> or in the statement <xsl: in value-of select = "name"/>, nodes are located based on the given subpaths relative to the current path.
Here are some detailed introductions and usage of xpath:
The http://msdn.microsoft.com/en-us/library/ms256115 (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: 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.