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
152
votes
8 answers

Select parent element of known element in Selenium

I have a certain element that I can select with Selenium 1. Unfortunately I need to click the parent element to get the desired behaviour. The element I can easily locate has attribute unselectable, making it dead for clicking. How do I navigate…
f l
  • 1,684
  • 2
  • 10
  • 11
151
votes
3 answers

How do I select child elements of any depth using XPath?

Suppose I have this (simplified):
Then I can select the submit button by XPath //form[@id='myform']/input[@type='submit']. Great. However, my…
gertvdijk
  • 24,056
  • 6
  • 41
  • 67
137
votes
5 answers

how to ignore namespaces with XPath

My goal is to extract certain nodes from multiple XML files with multiple namespaces using XPath. Everything works fine as long as I know the namespace URIs. The namespace name itself remains constant, but the Schemas (XSD) are sometimes…
kostja
  • 60,521
  • 48
  • 179
  • 224
132
votes
3 answers

XPath - Selecting elements that equal a value

In Xpath, I am wanting to select elements that equal a specific value. Sample XML data:
developer
  • 7,252
  • 14
  • 49
  • 57
131
votes
12 answers

Select values from XML field in SQL Server 2008

Just looking at my XML field, my rows look like…
Larsenal
  • 49,878
  • 43
  • 152
  • 220
131
votes
6 answers

How can I convert a string to upper- or lower-case with XSLT?

How do you do case conversion in XSL? UPPER CASE
mjs
  • 63,493
  • 27
  • 91
  • 122
131
votes
3 answers

XPath: How to check if an attribute exists?

Given the following XML, how do I write an XPath query to pull nodes where the attribute foo exists?:
Byron Whitlock
  • 52,691
  • 28
  • 123
  • 168
131
votes
2 answers

Why do indexes in XPath start with 1 and not 0?

Some colleagues and I were comparing past languages we had programmed in and were talking about our experience with VBScript with its odd features such as 1-based index instead of 0-based indexes like almost every other language has, the reasoning…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
129
votes
5 answers

Get nodes where child node contains an attribute

Suppose I have the following XML: Purgatorio Dante Alighieri 1308 30.00 Inferno
user176872
127
votes
3 answers

How to select following sibling/XML tag using XPath

I have an HTML file (from Newegg) and their HTML is organized like below. All of the data in their specifications table is 'desc' while the titles of each section are in 'name.' Below are two examples of data from Newegg pages.
Corey Farwell
  • 1,856
  • 3
  • 14
  • 19
127
votes
7 answers

Using XPATH to search text containing  

I use XPather Browser to check my XPATH expressions on an HTML page. My end goal is to use these expressions in Selenium for the testing of my user interfaces. I got an HTML file with a content similar to this: abc …
Bergeroy
  • 2,403
  • 2
  • 16
  • 11
122
votes
6 answers

Case insensitive XPath contains() possible?

I'm running over all textnodes of my DOM and check if the nodeValue contains a certain string. /html/body//text()[contains(.,'test')] This is case sensitive. However, I also want to catch Test, TEST or TesT. Is that possible with XPath (in…
Aron Woost
  • 19,268
  • 13
  • 43
  • 51
121
votes
5 answers

Xml Namespace breaking my xpath!

I have the following XML: This is a slimmed down version of XML being returned from a SharePoint web service. I also have the following…
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
118
votes
4 answers

how to use XPath with XDocument?

There is a similar question, but it seems that the solution didn't work out in my case: Weirdness with XDocument, XPath and namespaces Here is the XML I am working with:
jojo
  • 13,583
  • 35
  • 90
  • 123
114
votes
6 answers

How to select specified node within Xpath node sets by index with Selenium?

I'm writing a Selenium testcase. And here's the xpath expression I use to match all 'Modify' buttons within a data table. //img[@title='Modify'] My question is, how can I visit the matched node sets by index? I've tried with…
Kymair Wu
  • 1,275
  • 2
  • 9
  • 9