Use XPath to accurately locate node elements & selenium use Xpath to locate the complete article

The concept of matching is very important in transforming with XSL. In the template declaration statement xsl:template match = "" and the template application statement xsl:apply-templates select = "", the part enclosed in quotation marks must be able to locate the node exactly. The specific positioning method is given in XPath.

The reason why the concept of XPath is introduced in XSL is to accurately find a certain node element when matching the XML document structure tree. XPath can be compared to the file management path: through the file management path, the required files can be found according to certain rules; similarly, according to the rules formulated by XPath, any node in the XML structure document tree can be easily found , which is obviously one of the most basic functions for XSLT.

XPath data type

XPath can be divided into four data types:

  • A node-set 
    is a set of eligible nodes returned by path matching. Other types of data cannot be converted to node sets.
  • Boolean (boolean) 
    The conditional matching value returned by a function or Boolean expression is the same as the Boolean value in the general language, with two values ​​of true and false. Boolean values ​​can be converted to and from numeric types and string types.
  • String (string) 
    A string is a collection of characters, and XPath provides a series of string functions. Strings can be converted to and from numeric and boolean data.
  • The value (number) 
    in XPath is a floating-point number, which can be a double-precision 64-bit floating-point number. In addition, some special descriptions of values ​​are included, such as non-numeric NaN (Not-a-Number), positive infinity, negative infinity-infinity, positive and negative 0, and so on. The integer value of number can be obtained through the function, in addition, the value can also be converted to and from the Boolean type and the string type.

The latter three data types are similar to the corresponding data types in other programming languages, except that the first data type is a unique product of the XML document tree.

XPath node type

In addition, since XPath contains a series of operations on the document structure tree, it is also necessary to understand the XPath node type. Recall the logical structure of XML documents mentioned in Chapter 2. An XML file can contain logical elements such as elements, CDATA, comments, and processing instructions. Elements can also contain attributes, and attributes can be used to define namespaces. Accordingly, in XPath, nodes are divided into seven node types:

  1. Root node (Root Node) 
    The root node is the top level of a tree, and the root node is unique. All other element nodes on the tree are its children or descendants. The processing mechanism for the root node is the same as for other nodes. Matching against trees in XSLT always starts at the root node first.
  2. Element Nodes 
    Element nodes correspond to each element in the document, and the child nodes of an element node can be element nodes, comment nodes, processing instruction nodes, and text nodes. A unique identifying id can be defined for an element node. Element nodes can have an extension, which consists of two parts: one part is the namespace URI, and the other part is the local name.
  3. Text Nodes 
    Text nodes contain a set of character data, the characters contained in CDATA. No text node has an immediate sibling text node, and text nodes have no extension.
  4. 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. That is to say, the attribute node of the element can be matched by finding the child nodes of the element, but the reverse is not true, it is only one-way. Furthermore, the attribute nodes of elements are not shared, that is to say, different element nodes do not share the same attribute node. 
    Default properties are treated the same as defined properties. If an attribute is declared in a DTD, but declared as #IMPLIED, and the attribute is not defined in an element, the attribute is not included in the element's attribute node set. 
    Additionally, none of the property nodes corresponding to properties have namespace declarations. Namespace attributes correspond to another type of node.
  5. Namespace Nodes 
    Each element node has an associated set of namespace nodes. In XML documents, namespaces are declared through reserved attributes, so in XPath, this class of nodes is very similar to attribute nodes, and the relationship between them and the parent element is one-way and not shared.
  6. Processing Instruction Nodes 
    Processing instruction nodes correspond to each processing instruction in the XML document. It also has extensions, the local naming of the extension points to the processing object, and the namespace part is empty.
  7. Comment Nodes 
    Comment nodes correspond to comments in the document.

an XML document tree

Let's construct an XML document tree as the basis for the following examples:


     <A id="a1">
    <B id="b1">
    <C id="c1">
    <B name="b"/>
    <D id="d1"/>
    <E id="e1"/>
    <F id="e2"/>
    </C>
    </B>
    <B id="b2"/>
    <C id="c2">
    <B/>
    <D id="d2"/>
    <F/>
    </C>
    <E/>
    </A>
            

The following will introduce some basic methods of node matching in XPath.

path matching

Path matching is similar to the representation of file paths, which is easier to understand. There are the following symbols:

(1) Use "/" to indicate the node path 
. For example, "/A/C/D" indicates the child node "D" of the child node "C" of the node "A", that is, the D node with the id value of d2, and "/" indicates root node.

(2) Use "//" to represent all the elements whose paths end with the sub-path specified after "//", 
such as "//E" to represent all E elements, the result is all three E elements, such as "//C/E " means all E elements whose parent node is C, resulting in two E elements with id values ​​e1 and e2.

(3) Use "*" to represent the wildcard of the path. 
For example, "/A/B/C/*" represents all the sub-elements under the A element → B element → C element, that is, the B element whose name value is b and whose id value is d1 The D element and the two E elements with id values ​​e1 and e2 
"/*/*/D" represent the D element with two levels of nodes above, and the matching result is the D element with id value d2, such as "//*" represents all elements.

location match

For each element, its various child elements are ordered.

For example: /A/B/C[1] means A element → B element → the first child element of C element, and get the B element whose name value is b

/A/B/C[last()] represents the last child element of A element → B element → C element, and obtains the E element whose id value is e2

/A/B/C[position()>1] means A element→B element→C element whose position number is greater than 1, and the D element with id value d1 and two E elements with id value are obtained

attributes and attribute values

In XPath, attributes and attribute values ​​can be used to match elements. It should be noted that the attribute name of an element must be prefixed with "@". E.g:

//B[@id] represents all B elements with attribute id, the result is two B elements with id values ​​b1 and b2

//B[@*] represents all B elements with attributes, the result is two B elements with id attributes and one B element with name attributes

//B[not(@*)] means all B elements without attributes, the result is A element → B element under C element

//B[@id="b1"] The B element with the id value of b1, the result is the B element under the A element

kinship match

XML documents can be boiled down to a tree structure, so no node is isolated. Usually we attribute the affiliation between nodes to a kind of kinship, such as father, child, ancestor, descendant, brother and so on. These concepts can also be used when matching elements. E.g:

//E/parent::* represents the parent node element of all E nodes, the result is the A element with id value a1 and the C element with id value c1

//F/ancestor::* represents the ancestor node element of all F elements, the result is the A element with id value a1 and the C element with id value c2

/A/child::* represents the child element of A, the result is the B element with id values ​​b1 and b2, the C element with id value c2, and the E element without any attributes

/A/descendant::* means all descendant elements of A, the result is all other elements except A element

//F/self::* represents all the self elements of F, the result is the F element itself

//F/ancestor-or-self::* Represents all F elements and its ancestor node elements, the result is the F element, the parent node C element of the F element, and the A element

/A/C/descendant-or-self::* Represents all A elements → C elements and their descendant elements, the result is the C element with the id value of c2, the child elements B, D, and F elements of the element

/A/C/following-sibling::* Represents all sibling node elements in the immediate succeeding sequence of element A → element C, and the result is element E without any attributes

/A/C/preceding-sibling::* Represents all the immediately preceding sibling node elements of A element → C element, and the result is two B elements with id values ​​b1 and b2

/A/B/C/following::* Represents all elements in the sequence of A element → B element → C element, the result is B element with id b2, C element without attribute, B element without attribute, id is D element of d2, F element without attribute, / E element without attribute.

/A/C/preceding::* means A element → all elements in front of C element, the result is B element with id b2, E element with id e2, E element with id e1, D element with id d1 , B element with name b, C element with id c1, B element with id b1

condition match

Conditional matching is to use the Boolean value of the operation result of some functions to match the nodes that meet the conditions. There are four types of functions commonly used for conditional matching: node functions, string functions, numerical functions, and Boolean functions. For example, last(), position(), etc., we will not repeat them here.

Among the above matching methods, path matching is the most used. In the style sheet example in the previous chapter, either in the statement <xsl:template match="student roster">, or in the statement <xsl:value-of select="name"/>, it relies on giving A sub-path relative to the current path to locate the node.
_______________________________________________________

4. The syntax 

of XPath We have mentioned earlier that XPath is a language used to help XSLT find positioning information in XML source documents. In actual use, XPath and XSLT are always used together. In the grammar example in the previous chapter, we have already used the grammar of XPath, but we have not pointed it out clearly. But the W3C divides them into two standards, so we also split them into two chapters. 

4.XPath syntax 

4.1 Current position 
4.2 Addressing operation 
4.3 Operator 
4.4 Function 

4.1 Current position 

When we use XSLT to process the XML source document, we use Context to represent the node position currently being processed by the template. For example, the xsl:template match="/" statement indicates that the Context is at the root node of the document. I don't know how to accurately translate the word Context, which is similar to a pointer in C language, indicating the current position of the program. Understanding Context is very important for correct processing of XSL templates. When your XSL template output document is not what you want, the first thing you should analyze is where is Context.
Location Paths is used to set the location of the Context node you want to find. Similar to the DOS directory command. Let's look at an example

<xsl:for-each select="child::PEOPLE/descendant::PERSON"> 

where child::PEOPLE/descendant::PERSON is the XPath syntax, this expression is a Location Paths, and the code description should be displayed Child elements of all PEOPLE elements and child elements of all PERSON elements. Usually we will use a simpler way of writing:

<xsl:for-each select=" 

Let's explain two representations of paths: "/" and "//".
"/" is the node representing the current document, similar to the DOS directory separator. For example: /PEOPLE means to select the PEOPLE element under the root node; PEOPLE/PERSON means to select all PESON child elements under the PEOPLE element.
"//" means all nodes in the current document. Similar to viewing the entire directory. For example: //PEOPLE means to select all PEOPLE elements in the document, no matter what level it is; PEOPLE//PERSON means all PERSON elements under the PEOPLE element, no matter how deep it is.

4.2 Addressing operations 

Axis and Predicate are the syntax for locating Location Paths in XPath syntax. The specific usage list is as follows:

Axis syntax table
------------ ----------------------------------
Expression abbreviations
------------ --------------------------------------------
self . selects the current node ..
Example:
<TD><xsl:value-of select="."/></TD>
code means to insert the text value contained in the current node at the current position,
----------- ----------------------------------------------
parent .. select current The parent node of the node. 
-------------------------------------------------- ------
attribute @ selects all attributes of an element. 
Example:
<TD><xsl:value-of select="@PERSONID"/></TD>
selects all attributes of the PERSON element.
-------------------- ------------------------------------
child Selects all child elements of the current node.
-------------------------------------------------- ------
ancestor selects all parent elements of the current node (including the parent element of the parent element, and so on)
---------------------------- ------------------------------

Axis helps us select all nodes around the current node, and Predicate is used to locate the interior of the current node Elements. The expression method is to add expressions in square brackets []: [ Expression ]. Specific examples are as follows:

PERSON[position()=2] 
This code means to find the second "PERSON" element

PERSON[starts-with(name, "B")] 
This code means to find all names starting with "B" PERSON element. 

4.3 Operators 

This section introduces the XPath operators (Expressions), the list is as follows:
-------------------------------- ------------------------
Operator Description
----------------------- ---------------------------------
and, or is the ordinary meaning of and, or 
------- -------------------------------------------------
= Equal to
--------------------------------- -------
!= does not equal
--------------------------------------------- ----------
>, >= greater than, greater than or equal to
-------------------------------- ------------------------
<, <= Less than, less than or equal to. Note: In the XSL file, the < symbol should be represented by <
----------------------- -------------------
+, -, *, div Addition, subtraction, multiplication and division 
--------------------- -----------------------------------
mod modulo
------------ --------------------------------------------
| Two nodes are calculated together
-------------------------------------------------- ------

________________________________________________________________

The complete article of selenium using Xpath positioning

One of the articles mentioned xpath element positioning, but some of the articles are not suitable for some special and personalized scenarios. Provide the ultimate article on positioning xpath elements in text, you will definitely find the solution you need here.

The first method: positioning by absolute path (I believe everyone will not use this method)

By.xpath("html/body/div/form/input")
By.xpath("//input")
The third method: locate by element index
By.xpath("//input[4]")
The fourth method: use the xpath attribute to locate (can be used in combination with the second and third methods)
By.xpath("//input[@id='kw1']")
By.xpath("//input[@type='name' and @name='kw1']")
Fifth method: use partial attribute value matching (the most powerful method)
By.xpath("//input[start-with(@id,'nice')
By.xpath("//input[ends-with(@id,'very beautiful')
By.xpath("//input[contains(@id,'so beautiful')]")

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324515926&siteId=291194637