XPath small knot

xiaoxiao2021-03-06  70

1. XPath Overview

XPath is a language that performs node positioning and selection in an XML file. His core is to select a set of nodes required for a certain path expression by "Path Expression".

2. Tree model in XPath

XPath organizes nodes in the XML file as a tree. XPath defines a 7-point type: root node, element node, text node, attribute node, name spatial node, command processing node, and comment node. The relationship between them can be expressed as follows:

Relationship

Father and child relationship

Root node

Element node

Text node

Property node

Command processing node

Note node

Name spatial node

Note: The father and child relationship refers to the father of the front node, and the post-node is the son of the front node. The reference relationship means that the front node is the father of the post-node, but the post node is not the son of the front node.

3. Path Expression (LocationPath)

LocationPath-> AbsoluteLocationPath | RelativeEvelocationPath

AbsoluteLocationPath -> / RelativeAbsolutionPath? | AbbreviatedAbsoluteLocationPath

RelativeocationPath-> Step | RelativePath / Step | AbbreviatedRelaTiveLocationPath

AbbreviatedAbsoluteELOCATIONPATH -> // RelativePath

AbbreviatedRelaTiveLocationPath-> RelativeEvelocationPath // Step

AbbreviatedStep->..

AbbreviateAxisspecifier -> @?

Step-> Axisspecifier Nodetest Predicate * | AbbreviatedStep

Axisspecifier-> AxisName :: | AbbreviatedAxisspecifier

Axisname-> Ancestor | Annestor-or-self | Attribute | Child |

Descendant | Descendant-OR-Self | FOLLOWING | FOLLOWING-SIBLING |

Namespace | Parent | Preceding | Preceding-Sibling | Self

Nodetest->

Nam

Etest | nodetype () | Processing-instruction (Litral)

Predicate -> [PredicateExpr]

PredicateExpr-> EXPR

EXPR->

Ore

xpr

Ore

Xpr-> Andexpr |

Ore

Xpr or Andexpr

AndExpr-> EqualityExpr | Andexpr and equalityExpr

EqualityExpr-> RelationExpr | EqualityExpr = RelationExpr | EqualityExpr! = RelationExpr

RELATIONEXPR-> AdditiveExpr | RELATIONEXPR

RELATIONEXPR> AdditiveExpr | RELATIONEXPR> = AdditiveExpr

AdditiveExpr-> MultiplicativeExpr | AdditiveExpr MultiplicativeExpr | AdditiveExpr-MultiplicativeExpr

MultiplicativeExpr-> unaryExpr | MultiplicativeExpr * unaryExpr |

MultiplicativeExpr Div UnaryExpr |

MultiplicativeExpr mod unaryexpr

UnaryExpr-> unionexpr | -unaryExpr

UnionExpr-> PATHEXPR |

Union

EXPR | PATHEXPR

PATHEXPR-> LocationPath | FilTeRexpr | FilTeRexpr / RelativePath |

FilTeRexpr // RelativeEvelocationPath

FilteRexpr-> PrimaryExpr | FilteRexpr Predicate

PrimaryExpr-> variablereference | (expr) | Literal | Number | FunctionCall

FunctionCall-> FUNCTIONNAME ((argument (, argument) *)?)

Argument-> EXPR

4. Abbreviated

In order to simplify the representation of the XPath expression and improve the readability of the expression, XPath provides some omitted expression. These include:

Short

effect

(air)

Self

//

Descendant-OR-Self

@

Get a property

.

Self

.

parent

.//

Descendant

*

Child

5. AxesName

Axisname-> Ancestor | Annestor-or-self | Attribute | Child |

Descendant | Descendant-OR-Self | FOLLOWING | FOLLOWING-SIBLING |

Namespace | Parent | Preceding | Preceding-Sibling | Self

The shaft is used to represent a set of nodes in a class. XPath defines a total of 13 shafts, namely:

a) ancestor:

i. Role: Get the parent node of the node, and all ancestors. (Do not include attribute nodes, name spatial nodes).

b) Annestor-OR-Self

i. Role: Get the current node, and all its ancestors.

c) Attribute:

i. Role: Get all attribute nodes of the current node.

d) child:

i. Role: Get all child nodes of the node.

e) Descendant:

i. Role: Get the child node of the node, and all grandson nodes. (Do not include attribute nodes, name spatial nodes).

f) Descendant-OR-Self

i. Role: Get this node, and all its grandson nodes.

g) FOLLOWING

i. Role: Get all subsequent brothers nodes of the node and the children of their brothers nodes. (Do not include attribute nodes, name spatial nodes). h) Following-Sibling

i. Role: Get all subsequent brothers nodes of the node. (Do not include the children and grandchild nodes, attribute nodes, name spatial nodes).

I) Namespace

I. Role: Get the namespace node of the node.

j) parent

i. Role: Get the parent node of the node.

K) preceding

i. Role: Get all front guided brothers nodes of the node and the children of their brothers nodes. (Do not include attribute nodes, name spatial nodes).

l) preceding-sibling

i. Role: Get all front guides of the node. (Do not include the children and grandchild nodes, attribute nodes, name spatial nodes).

m) Self:

I. Role: Get the current node.

6. Nodetest

Nodetest->

Nam

Etest | nodetype () | Processing-instruction (Litral)

NodeTest is used to filter and filter node sets within the range selected by the shaft.

Nametest -> * | ncname * | qname

NODYTYPE-> Comment | text | Processing-instruction | Node

NCNAME: Indicates non-qualified name, namely a general variable name.

QNAME: It is the name of the prefix format. Prefix: Name

7. Predicate

Predicate is used to filter the nodes in the candidate node set. When the return value of the word expression is TRUE, the node will be included in the new node.

8. FunctionCall

FunctionCall-> FUNCTIONNAME ((argument (, argument) *)?)

Argument-> EXPR

XPath provides a function package called a core function for programmers, which is mainly divided into node set functions, string functions, Boolean functions, mathematical functions:

a) Node set function:

i. Number last ():

1. Role: Returns the index value of the last child node.

2. Example: Param [Last ()]: Returns the last param child node.

ii. Number position ()

1. Role: Returns the index value of the node in the child node list of the parent node.

Iii. Number Count (Node-Set)

1. Role: Returns the number of nodes in the node set.

Iv. Node-set ID (Object)

1. Run: Returns the list of nodes for the ID value of Object.

2. Example: ID ("Nick"): Returns the node set of node ID to "Nick".

b) String function:

i. String string (Object?) 1. Run: Transform the contents of Object into string expressions, tested the toString () method in Java.

Ii. String Concat (String, String, String *)

1. Role: Connect all String together.

Iii. Boolean Starts-with (String, String)

1. Role: Check if the first string uses the second string as the starting section

Iv. Boolean Contains (String, String)

1. Role: Check if the first string contains the second string.

v. String Substring-Before (String, String)

1. Role: Returns the character string before the second string appears in the first string.

2. Example: Substring-Before ("Abcde", "D") returns "ABC".

Vi. String Substring -After (String, String)

1. Role: Returns the character string after the second string appears in the first string.

2. Example: Substring -After ("abcde", "D") returns "E"

Vii. String Substring (String, Number, Number)

1. Role: The first digit specifies the beginning of the index, the second number specifies the length. The index of the first character of the string is 0.

Viii. Number string-length (String?)

1. Role: Returns the length of the string.

IX. String Translate (String, String, String)

1. Role: The characters that appear in the second string in the first string will be replaced with characters in the same position in the third string, and if there is no corresponding character in the third string , The character in the first string is not output.

2. Example: Translate ("AbcDef", "ABC", "ABC") Returns Abcdef

Translate ("--- ABC ---", "ABC -", "ABC") Returns ABC

c) Boolean function:

i. Boolean Boolean (Object)

1. Role: Returns the Boolean Expression Value corresponding to Object.

Ii. Boolean Not (Boolean)

1. Role: Reverse.

d) Mathematical functions:

i. Number Number (Object)

1. Role: Returns the value corresponding to the Object.

ii. Number SUM (Node-Set)

1. Role: Seeking the Number value of each node in the node.

Iii. Number floor (Number)

1. Role: Returns the maximum integer value that is not greater than Number.

Iv. Number CEILING (Number) 1. Run: Returns the maximum integer value that is not less than NUMBER.

V. Number Round (Number)

1. Role: Returns the approximate value of Number.

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

New Post(0)