Node matching path XPath (transfer)

xiaoxiao2021-03-06  119

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:

Symbolic meaning case matching result / indicator Node path / A / C / D node "a" child node "d" "D", that is, the ID node / root node // all paths of the ID value of D2 Elements // e element ending after "//", the result is the E element for all three E element // c / e all parent nodes C, the result is that the ID value is E1 and E2. Two E-element * paths / A / B / C / * A element → B element → C element under the group B element, the NAME value is b, the D element and ID value of the ID value of D1 is The two E-elements / * / * / * of E1 and E2 have a D element of two-stage nodes. The matching result is a D element of the ID value of D2 // * All elements | Logic or // b | // C B-element and C element position match for each element, each of its sub-elements is orderly. Such as:

Examples Conflict Match Results / A / B / C [1] A element → b / b / c [Last ()] A / B / C [Last ()] a element → The final sub-element ID value of B element → C element is E element E element / A / B / C [position ()> 1] A element → B element → C element is greater than 1 element ID value of 1 D1 D element and two E-element properties and attribute values ​​with ID values ​​can utilize attributes and attribute values ​​to match elements in XPath. It is to be noted that there must be "@" prefix before the element's attribute name. E.g:

Example Configuration Match Result // B [@ID] All B-element ID values ​​with attribute IDs are B1 and B2 // b [@ *] all of the 2 Id properties with id properties B element and a Name property B element // b [NOT (@ *)] all non-attribute B element a elements → B element under C element // b [@ ID = "b1"] ID value is B1 B element 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:

Example Configuration Match Results // E / PARENT:: * All E-Node Parent Node Element ID Values ​​are A1 A1 Elements and ID Values ​​C1 C1 // F / AnceStor :: * All F Elements Ancestral Node The group ID value of the element ID value of the element is A1 is a C element of C2 of C2, the b1, b2 B element, the ID value of C2, and there is no attribute. Element / A / Descendant :: * A of all the descendants of the E-element except for all other elements other than the A element // f / self :: * All F element F element itself // f / Ancestor-or-self: * All F elements and its ancestral node elements F elements, F-element parent C elements and A elements / A / C / Descendant-OR-Self :: * All A elements → C elements and their descendants Id value C2 C element, sub-elements B, D, F element / A / c / following-sibling :: * a element → C-element adjacent back sequence All brothers Node elements do not have any attributes / A / C / preceding-sibling :: * A element → C element adjacent front All brothers Node element ID value of B1 and B2 2 B1 and B2 / A / B / C / FOLLOWING:: * a element → B element → c element All element IDs of the sequence are B2 B2, the C element without the attribute, the B element without the attribute, the ID of D2 D2 D element, the F element without attribute, and an E element without the attribute. / A / c / preceding :: * A element → The front of all element IDs of the C element is B2 B2 B2, the ID of E2 E element, the ID is E element E element, the ID of D1 D1, NAME is B B element, ID of C1 C1, b element of 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 functional action count () function statistics count, return the number of Number () features in the eligible node to convert the text in the value of the value into a value substring () function

Syntax: Substring (Value, Start, Length)

Intercept string SUM () function summary

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.

转载请注明原文地址:https://www.9cbs.com/read-126306.html

New Post(0)