Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of XPath

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain the example analysis of XPath for you in detail. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Example 1

The basic XPath syntax is similar to locating files in a file system, and if the path begins with a slash /, then the path represents the absolute path to an element.

/ AAA

Select the root element AAA

/ AAA/CCC

Select all CCC child elements of AAA

/ AAA/DDD/BBB

Select all child elements of AAA DDD

Example 2

If the path begins with a double slash / /, it means that all elements in the document that meet the rules after the double slash / / are selected (regardless of hierarchy) / / BBB

Select all BBB elements

/ / DDD/BBB

Select all BBB elements whose parent element is DDD

Example 3

An asterisk * indicates the selection of all elements located by the path before the asterisk

/ AAA/CCC/DDD/*

Select all elements whose paths are attached to / AAA/CCC/DDD

/ * / BBB

Select all BBB elements with 3 ancestor elements

/ / *

Select all elements

Example 4

The expression in the square number can further specify the element, where the number indicates the position of the element in the selection set, and the last () function represents the last element in the selection set.

/ AAA/BBB [1]

Select the first BBB child element of AAA

/ AAA/BBB [last ()]

Select the last BBB child element of AAA

Example 5

/ / @ id

Select all id properties

/ / BBB [@ id]

Select the BBB element with the id attribute

/ / BBB [@ name]

Select the BBB element with the name attribute

/ / BBB [@ *]

Select a BBB element with any attribute

/ / BBB [not (@ *)]

Select a BBB element with no attributes

Example 6

The value of the attribute can be used as a criterion for selection. The normalize-space function removes the leading and trailing spaces and replaces the consecutive space string with a single space.

/ / BBB [@ id=\'b1\']

Select the BBB element that contains the attribute id and has a value of\'b1\'

/ / BBB [@ name=\ 'bbb\']

Select the BBB element that contains the attribute name and whose value is\ 'bbb\'

/ / BBB [normalize-space (@ name) =\ 'bbb\']

Select the BBB element that contains the attribute name and whose value (after removing spaces before and after using the normalize-space function) is\ 'bbb\'.

Example 7

The count () function can count the number of selected elements

/ / * [count (BBB) = 2]

Select the element that contains 2 BBB child elements

/ / * [count (*) = 2]

Select an element with 2 child elements

/ / * [count (*) = 3]

Select an element with 3 child elements

Example 8

The name () function returns the name of the element, the start-with () function returns true if the function's first argument string starts with the second argument character, and the contains () function returns true if its first string parameter contains the second string argument.

/ / * [name () =\ 'BBB\']

Select all elements named BBB (here equivalent to / / BBB)

/ / * [starts-with (name (),\'B\')]

Select all elements whose names start with\ "B\"

/ / * [contains (name (),\'C\')]

Select all elements whose names contain\ "C\"

Example 9

Multiple paths can be merged with delimiters |

/ / CCC | / / BBB

Select all CCC and BBB elements

/ AAA/EEE | / / BBB

Select all BBB elements and all EEE elements that are children of AAA

/ AAA/EEE | / / DDD/CCC | / AAA | / / BBB

There is no limit to the number of paths that can be merged

Example 10

The child axis (axis) contains child elements of the context node. As the default axis, it can be ignored or unwritten.

/ AAA

Equivalent to / child::AAA

/ child::AAA

Equivalent to / AAA

/ AAA/BBB

Equivalent to / child::AAA/child::BBB

/ child::AAA/child::BBB

Equivalent to / AAA/BBB

/ child::AAA/BBB

Both can be merged.

Example 11

The descendant axis contains the descendants of context nodes, a descendant is a child node or a child node, and so on, so the descendant axis does not contain attribute and namespace nodes.

/ descendant::*

Select all descendants of the document root element. That is, all elements are selected.

/ AAA/BBB/descendant::*

Select all descendant elements of / AAA/BBB

/ / CCC/descendant::*

Select all elements that have CCC in the ancestor element

This is the end of this article on "sample Analysis of XPath". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report