Questions tagged [xpath]

The primary purpose of XPath is to address parts of an XML document. It also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax. XPath operates on the abstract, logical structure of an XML document, rather than its surface syntax.

XPath is the result of an effort to provide a common syntax and semantics for functionality shared between XSL Transformations XSLT and XPointer.

The primary purpose of XPath is to address parts of an XML document. In support of this primary purpose, it also provides basic facilities for manipulation of strings, numbers and booleans. XPath uses a compact, non-XML syntax to facilitate use of XPath within URIs and XML attribute values. XPath operates on the abstract, logical structure of an XML document (for example XDM, or DOM), rather than its surface syntax. XPath gets its name from its use of a path notation as in URLs for navigating through the hierarchical structure of an XML document.

Besides XSLT and XPointer, XPath is now used in other host languages such as XQuery, XML Schema, Schematron, and Selenium.


What is XPath?

  • XPath is a syntax for defining parts of an XML document.
  • XPath uses path expressions to navigate in XML documents.
  • XPath contains a library of standard functions.
  • XPath is a major element in XSLT.
  • XPath is a W3C recommendation.

enter image description here

Tools

The XPath Visualizer is a local HTML and JavaScript application for teaching XPath 1.0 by example. Just load your XML file, repeatedly enter any XPath expression and immediately see the result highlighted in the XML document (if node-set) or in a separate dialog box (otherwise).

XPath Online Tools

There are a number of XPath online tools available including

The following online XPath checker can not only be used for testing and evaluation but also for saving and sharing, e.g. sharing samples for Stack Overflow questions:


Browser Addons

  1. xPath Analyzer Google Chrome Addons - Chrome Webstore
  2. XPath Checker Mozilla Firefox Addons - Firefox Addons

Online Tutorial


Online Training

  1. A 70-minutes "Crash Course in XPath" module is available as part of the Pluralsight video-course "XSLT 2.0 and 1.0 Foundations"

  2. "The Evolution of XPath: What’s New in XPath 3.0" -- A Pluralsight video-course (4.5h)


External links

Syntax Highlighting

To add syntax highlighting to a block of XPath code, tag it as lang-xpath

39115 questions
114
votes
6 answers

How to select a node using XPath if sibling node has a specific value?

I have the following document: abc ccc
ddd
zz 1
2
How can I get the value of using XPath if is zz?
HOE SENGKIANG
  • 1,149
  • 2
  • 8
  • 4
112
votes
6 answers

Is there an XSLT name-of element?

In XSLT there is the to get the value of an element, but is there something to select the tag-name of the element? In a situation like this: Robert Can anybody tell me how to express for locating the second child? This doesn't…
Jin Kwon
  • 2,005
  • 3
  • 15
  • 15
106
votes
3 answers

XPath: How to select nodes which have no attributes?

Using XPath, how to select nodes which have no attributes (where attribute count = 0)? For example: <- FIND THIS
Zanoni
  • 30,028
  • 13
  • 53
  • 73
106
votes
3 answers

XPath:: Get following Sibling

I have following HTML Structure: I am trying to build a robust method to extract second color digest element since there will be many of these tag within the DOM. …
add-semi-colons
  • 18,094
  • 55
  • 145
  • 232
106
votes
6 answers

How to concat a string to xsl:value-of select="...?

Is there any way to concat another string to
Hanumanth
  • 1,197
  • 2
  • 10
  • 14
102
votes
3 answers

What is the difference between a CSS and XPath selector? And which is better with respect to performance for cross-browser testing?

I am working with the Selenium WebDriver 2.25.0 on a multilingual web application and mainly test the page content (for different languages, like Arabic, English, Russian, and so on). For my application, which is better according to performance and…
Chetan
  • 2,360
  • 3
  • 18
  • 33
99
votes
7 answers

XPath test if node value is number

How can I check if a node value is a number using XPath? Any ideas?
Harold Sota
  • 7,490
  • 12
  • 58
  • 84
98
votes
4 answers

How to use not contains() in XPath?

I have some XML that is structured like this: Film Business
Jason
  • 1,167
  • 1
  • 9
  • 10
97
votes
3 answers

XPath: difference between dot and text()

My question is about specifics of using dot and text() in XPath. For example, following find_element lines returns same element: driver.get('http://stackoverflow.com/') driver.find_element_by_xpath('//a[text()="Ask…
Andersson
  • 51,635
  • 17
  • 77
  • 129
96
votes
3 answers

Matching a node based on a sibling's value with XPath

Having a XML document like this: svn A 86400 69075
Flavius
  • 13,566
  • 13
  • 80
  • 126
96
votes
5 answers

Xpath: select div that contains class AND whose specific child element contains text

With the help of this SO question I have an almost working xpath: //div[contains(@class, 'measure-tab') and contains(., 'someText')] However this gets two divs: in one it's the child td that has someText, the other it's child span. How do I narrow…
Andrejs
  • 10,803
  • 4
  • 43
  • 48
95
votes
6 answers

How can I find the link URL by link text with XPath?

I have a well formed XHTML page. I want to find the destination URL of a link when I have the text that is linked. Example programming questions site news I want an XPath…
flybywire
  • 261,858
  • 191
  • 397
  • 503