XPath sequence three

xiaoxiao2021-03-06  93

3 Position path and path steps

The main components of XPath are expressions, where the most important expression is a locating path expression, which is why it is named XPath. Location path expression is this type of expression:

/ Pub / Book / Author

Rough looks, positioning path expressions are very like paths in the file system. The idea of ​​the positioning path and the path in the file system are very similar, of course, more complex.

There are two types of positioning paths, which are opposing positioning paths and absolute positioning paths, respectively. Each positioning step is composed of one or more positioning steps, and each positioning step is separated from a forward slash (/). The absolute path begins with a forward slash (/), and the relative path is not.

How to use the context node set context node in XPath to describe how the value of the positioning path is performed. The context node set is defined as the current node set for giving a given point in the expression; and the context is defined as: the current node being processed.

The positioning step evaluates from left to right in order. Each positioning step is obtained with respect to the node in the context node set. If the positioning path is absolute, the initial context node set contains the root node, otherwise it contains the current context node (ie, depending on the expression position). The first positioning step is evaluated as the context node in the context node, and then the result node set is combined into a new node set. This node is integrated into the context node set of the next step, such processing is positioning Each positioning step of the path continues, the final positioning step is the result of this expression.

A positioning path consists of several positioning steps, while a positioning step has three parts:

· ????? A axis, which specifies the tree relationship between the node selected by the positioning step and the context node,

· ????? A node test, it specifies the node type of the node selected by the positioning step and the node extension extension, and

· ????? Zero or more or more judgment words, which uses proprietary expressions to further refine the set of nodes selected by the positioning step.

The syntact of the positioning step is composed of two colonially separated axes and node tests, followed by zero or more than the expression in square cord. For example, in Child :: Para [position () = 1], Child is the axis name, and Para is a node test and [Position () = 1] is a decision word.

The evaluation value of the entire positioning step is the initial node set from the axis and node test, and then filter it sequentially by each decision word to obtain a set of nodes as the result.

The node of the initial node set, these nodes are specified by the context node relationship, and its node type and extension are specified by the node test. For example, positioning step Descendant :: Para Select the PARA element of the context node: Descendant Specifies that each node in the initial node collection must be a descendant node of the context; Para specifies each node in the initial node collection It must be a para element.

The initial node set is filtered by the first determination word to generate a new node set, and the new node set is filtered again by the second decision word, so that it has been down. The final node collection is a collection of nodes selected by the positioning step.

The following is the axis available:

· ?????????? The child contains the child of the context node;

· ???????? Descendant axes contain descendants of the context node; descendants are a child or a child of a child, so that the descendant is never contains attributes or namespace nodes;

· ???? Parent axes contain the parent node of the context node, if any

· ???? Ancestor axis contains ancestors of context nodes; the ancestors of the context node consists of the parent node of the context node and the parent node of the parent node; this, the ancestral axis will always include root nodes unless the context node is root node • ????? FOLLOWING-SIBLING axis contains all the brothers in the context node, if the context node is attribute node or namespace node, the FOLLOWING-SIBLING axis is empty;

· ???? The preceding-sibling axis contains all the brothers in the context node, if the context node is the property node or namespace node, the preceding-sibling is empty;

· ???? FOLLOWING shaft In the same document in the context node, all the nodes after the context node in accordance with the document order, but exclude all descendants, also excludes attribute nodes and namespace nodes;

· ???? The preceding axis is included in the same document in the context node, and all the nodes before the context node in accordance with the document order, but excludes all descendants, and excludes attribute nodes and namespace nodes;

· ???? Attribute shaft contains the properties of the context node, unless the context node is an element, the axis will be empty;

· ???? Namespace axes contain namespace nodes of context nodes unless the context node is an element, the axis will be empty;

· ???? The SELF axis just contains the context node;

· ???? Descendant-or-Self Axis contains descensities of context nodes and context nodes;

· ???? Ancestor-or-Self Axis contains ancestors of context nodes and context nodes; this axis will always include root nodes;

Ancestor, Descendant, Following, Preceding, and Self axes have been divided into a document (ignore attributes and namespace nodes): They do not overlap each other, and they are together with all the nodes of the documentation.

Each axis has a direction. The direction of the axis can be moved in the node in the direction of forward, forward axis, while the reverse axis moves between nodes in the reverse document order. In these shafts, Child, Descendant, Descendant-OR-Self, FOLLOWING, FOLLOWING-SIBLING belongs to the forward axis, while parent, ancestor, ancestor-or-self, preceding, and preceding-sibling belong to the reverse axis, other axes The direction is not available.

Each axis has a basic node type. If the shaft can contain an element, the basic node type is an element; otherwise, its type is the type of node that the axis can be included. Therefore, for the attribute shaft, the basic node type is attribute. For named spatial axes, basic node types are named space. For other axes, the basic node type is an element.

The node car uses the node in the determination axis. If the node test value of the given node is True, it remains in the node set, otherwise it is removed from the node. Node tests can be performed using names or types.

When using the name of the Name, all non-specified axes basic types of nodes are automatically discarded, and then the name of the remaining node is compared to the qualified name (QNAME) specified in the positioning step, QNAME is expanded as a namespace name ( Local name named space URI), then compare with the namespace name of each node in the question. If the namespace name is matched, the node remains in the node set, otherwise the node is discarded. For example, Child :: Para Selects the PARA element of the context node. If the context node does not have a PARA child, it will select an empty collection of the node; attribute :: href Select the HREF property of the context node, if the context node does not have the href property, It will select an empty collection of nodes. XPath handler In order to extend the QNAME into a namespace name, it needs to access the namespace binding. Therefore, the XPath handler needs to provide a mechanism for creating a namespace binding, which will be used when the expression is evaluated. In XSLT, this can be done through standard XML 1.0 namespace life, and in the DOM, you need to use other methods to complete.

Node test * is true for any node of the basic node type. For example, Child :: * will select all of the element subtots of the context, and attribute :: * will select all the properties of the context node. Node tests can be used in format ncname: *. In this case, the prefix is ​​expanded in the same way as QNAME, ie the context namespace declaration. This node test can determine all nodes from a given namespace.

If the node has a specific type, you can use the type of node test, and multiple node type identifiers for node testing are defined in XPath. For any body node, node test text () is true. For example, Child :: text () will select the text subtitle of the context node. Similarly, for the comment node, the node test comment () is true; for the processing instruction, the node test processing-instruction () is true; Processing-Instruction () test can have a literal parameter, in which case all Processing instructions, such as the instruction name and parameters, etc., its value is true. For any node, no matter the type, node test node () is true.

For this purpose, it is placed in square brackets at the end of the positioning step. Predicates Screen a node set to generate new nodes set. For each of the screened nodes in the node, the predicate expression will evaluate this node as the context node, and the result is forced to convert to the Boolean value. If the result is True, this node remains in the node set, otherwise it will be removed from the node.

Many path expressions can use a short queue syntax. In fact, the syndrome is more common than the full syntax because they are more concise. The most important simplification is Child :: can be omitted from the positioning step. In the actual effect, Child is the default axis. For example, address path DIV / Para is the abbreviation of Child :: div / child :: Para. Attributes also have abbreviations: attribute :: can be abbreviated into @. For example, the full form of positioning path Para [@ type = "warning"] is Child :: Para [attribute :: type = "warning"]. // is an abbreviation for / descendant-or-self :: node () /. For example, // para is / descendant-or-self :: node () / child :: Para's abbreviation, so select the PARA element in the document (even if the PARA element is a document element, it will be selected by // Para, Because the document element is a child's child); Div // Para is the abbreviation of div / descendant-or-self :: node () / child :: Para, so you will choose all PARA descensicles of the Div child. It should be noted that the address path // Para [1] is different from address path / descendant :: Para [1], the latter chooses the first descendant PARA element, the former chooses such descendants PARA elements: they are The first Para child in his parent node. In addition, the positioning step is the abbreviation of Self :: node (). This is especially useful with //. For example, the positioning path .//para is the abbreviation of Self :: Node () / descendant-or-self :: node () / child :: Para, so you will select all the PARA descendants of the context node. Similarly, the address path .. is a abbreviation for Parent :: node (). For example, .. / title is the abbreviation of the Parent :: Node () / Child :: title, so this will select the Title child of the parent node of the context node.

In addition, the position () = number in the predicate () = digital is often short-handed as a number, such as Author [Position = 1] often is simply written as Author [1], which is used to select the first Author child element of the context node.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -----------------

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

New Post(0)