XPath in .NET Select the XPath in the blog keyword in Uncommon. NPATH Keywords: XPath XML Select Blog from Guoyan19811021 About XPath Node Match Path XPath
The concept of matching is very important during the conversion of XSL. In Template Declaration Statements XSL: Template Match = "" and Template Application Statement XSL: Apply-Templates Select = ", the part of the quotation must be able to accurately locate the node. The specific positioning method is given in XPath. Alternatively, XPath can be used to search for the XML document. The concept of introducing XPath is to accurately find a certain node element in order to match the XML document tree. You can use the XPath to be used as a file management path: through the file management path, you can find the required files in accordance with certain rules; .
Before introducing the match rules of XPath, let's take some basic concepts about XPath. The first thing to say is the XPath data type. XPath can be divided into four data types:
The Node-SET node set is a collection of a set of nodes that returned by the path match. Other types of data cannot be converted to a node set.
Boolean (Boolean) is matched by the conditional or Boolean expression, which is the same as the Boolean value in the general language, two values of True and FALSE. Boolean values can be converted with numeric types, string types.
String strings that contain a collection of characters, providing a range of string functions in XPath. The string can be converted to the data of the numeric type, the Boolean value type.
NUMBER value is a floating point number in XPath, which can be a double precision 64-bit floating point number. Alternatively, specific descriptions of some values, such as non-numerical nan (not-a-number), infinite infinity, negative infinity -Infinity, positive and negative 0, etc.. Number's integer value can be obtained by a function, in addition, numerical values can be converted to both Boolean types, string types. The corresponding data types of the three data types and other programming languages are similar, but the first data type is the unique product of the XML document tree. In addition, since XPath is included in a series of operations of the document structure, it is also necessary to understand the XPath node type. Due to the logical structure of the XML document, an XML file can contain logic elements such as elements, cdata, annotations, process instructions, where elements can also contain attributes and can be used to define namespaces. Accordingly, in XPath, the node is 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 elements nodes on the tree are its child nodes or descendants. The processing mechanism for root nodes is the same as other nodes. The matching of the tree in XSLT is always starting from the root node.
Element nodes element node corresponds to each element in the document, the child node of an element node can be an element node, an annotation node, a processing command node, and a text node. A unique identifier ID can be defined for the element node. Element nodes can have an extension, which consists of two parts: part is named space URI, and the other is naming locally.
Text Nodes text nodes contain a set of character data, that is, characters contained in CDATA. No text node has a neighboring brother text node, and the text node has no extension.
Attribute Nodes Each element node has a set of associated property nodes, the element is the parent node of each attribute node, but the property node is not the child node of its parent element. That is to say, by finding the child node of the element, the properties node of the element can be matched, but it is not established, but only one-way. Moreover, the attribute node of the element has no sharing, that is, different element nodes do not share the same attribute node. The processing of defined attributes is equivalent to the defined properties. If an attribute is declared in the DTD, the declaration is #implied, and the property is not defined in the element, the attribute node set of the element does not contain this property. In addition, there is no declaration with the attribute node corresponding to the property. Namespace properties correspond to another type of node. Name Space Node (Namespace Nodes) Each element node has a related namespace node set. In the XML document, the namespace is declared by reserving attributes, so in XPath, the class node is extremely similar to the attribute node, and the relationship between them and the parent element is unidirectional, and does not have sharedness.
Processing Instruction Nodes Processing Instruction Node corresponds to each processing instruction in an XML document. It also has an extension, and the local naming of the extension points to the processing object, and the namespace part is empty.
Comment Nodes comment nodes correspond to the comments in the document. Below, let's construct an XML document tree:
Now, some basic methods that use XPath to match nodes in XML.
The path matching path match is similar to the file path representation, which is better understood. There are several symbols:
Symbol
Meaninglessness
Give an example
Match results
/
Indicates node path
/ A / C / D
The child node "D" of the child node "C" of the node "a", that is, the ID node of the ID value is D2
/
Root node
//
Elements ending with the subpath specified after "//"
// e
All E elements, the result is all three E elements
// c / e
All parent nodes are E elements of C, the result is two E elements for the ID value E1 and E2
*
Path wildcard
/ A / b / c / *
A element → B element → all sub-elements under the C element, the B element of the NAME value is B, the ID value of the D1 D1 is two E elements of E1 and E2.
/ * / * / D
There are two D elements of the two-stage nodes above, the matching result is a D element with the ID value D2
// *
All the elements
|
Logic or
// b | // c
All B elements and C elements
Location matching For each element, its individual sub-elements is orderly. Such as:
Give an example
Meaninglessness
Match results
/ A / b / c [1]
A element → B element → c element first child element
Name value B element B element / A / B / C [Last ()]
A element → B element → the last child element of C element
ID value E element E element
/ A / b / c [position ()> 1]
A element → B element → the position below the C element is greater than 1 element
The ID value of D1 is D1 and two E elements with ID value.
Properties and attribute values can utilize attributes and attribute values in XPath to match elements. It is to be noted that there must be "@" prefix before the attribute name of the element. E.g:
Give an example
Meaninglessness
Match results
// b [@ID]
All B elements with properties ID
Two B elements of the ID value of B1 and B2
// b [@ *]
All B elements with properties
Two B elements with ID attributes and a NAME attribute B element
// b [not (@ *)]
All B elements do not have attributes
A element → B element under C element
// b [@ ID = "b1"]
B element of the ID value of B1
B element under a element
The relative relationship matching the XML document can be attributed to the tree structure, so any of the nodes is not isolated. Usually, we will break the property relationship between nodes into a relative relationship, such as father, child, ancestors, offspring, brothers, etc. These concepts can also be used when matching elements. E.g:
Give an example
Meaninglessness
Match results
// E / PARENT :: *
Parent node elements in all E nodes
C1 of C1 of the a element and ID value of the ID value of A1
// f / ancestor :: *
Ancestor node elements for all F elements
C element of A1 and ID value of the ID value of A1 is C2
/ A / child :: *
A child element
The ID value of the ID value is B1, B2, the C element of the ID value of C2, and E elements without any attributes
/ A / descendant :: *
All descendants of A
All other elements other than A elements
// f / self :: *
All F of your own elements
F element itself
// f / Annestor-or-self :: *
All F elements and its ancestors
F element, F element parent C element and a element
/ A / c / descendant-or-self :: *
All A elements → C elements and their descendants
C element of C2 is C2, sub-element B, D, F element of this element
/ A / c / following-sibling :: *
A element → C element close proximity of all brothers node elements
E element without any properties
/ A / c / preceding-sibling :: *
A element → c element close to all brothers node elements
Two B elements of the ID value of B1 and B2
/ A / B / C / FOLLOWING :: *
A element → B element → all elements of the sequence of C element
The b element of the ID is B2, the C element without the attribute, the B element without attribute, the ID of the ID D2 D2, the F element without attribute, and an E element without the attribute.
/ A / c / preceding :: *
A element → all elements in front of C element
The B element of the ID is B2, the ID E element E element, the ID is E element E element, the ID of the D1 D1, the NAME B, the B element of the NAME B, the C1 of C1, the C1 of C1, the b element of the ID B1)
The conditional matching condition match is the Boolean value of the calculation result of some functions to match the eligible node. Some functions that are commonly used for conditions match have four categories: node functions, string functions, numeric functions, and Boolean functions. For example, Last (), position (), etc. mentioned earlier. These functional functions can help us find the needs of the nodes.
Function and function
effect
COUNT () function
Counting statistics, returning the number of qualified nodes
Number () function
Convert text in the value of the attribute to a value
Substring () function
Syntax: Substring (Value, Start, Length)
Intercept string
SUM () function
Sum up
These features are only part of the XPath syntax, and there are a lot of functional functions that are not introduced, and the syntax of XPath is still growing. Through these functions we can achieve more complex queries and operations.
In these matching methods, the most important path matching is used. Relying on the position of the node to give a subpath relative to the current path.
Author Blog:
http://blog.9cbs.net/uncommon/
related articles