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

35289 questions
203
votes
5 answers

How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug?

How can I verify my XPath? I am using Chrome Developers tool to inspect the elements and form my XPath. I verify it using the Chrome plugin XPath Checker, however it does not always give me the result. What is a better way to verify my XPath. I have…
user3448242
  • 2,035
  • 2
  • 10
  • 6
196
votes
5 answers

XPath: Get parent node from child node

I need get the parent node for child node title 50 At the moment I am using only //*[title="50"] How could I get its parent? Result should be the store node.
GibboK
  • 64,078
  • 128
  • 380
  • 620
189
votes
5 answers

XPath with multiple conditions

What XPath can I use to select any category with a name attribute specified and any child node author with the value specified. I've tried different variations of the path below with no success: //quotes/category[@name='Sport' and author="James…
mjroodt
  • 2,833
  • 5
  • 19
  • 35
188
votes
9 answers

What is the correct XPath for choosing attributes that contain "foo"?

Given this XML, what XPath returns all elements whose prop attribute contains Foo (the first three nodes):
ripper234
  • 202,011
  • 255
  • 600
  • 878
175
votes
4 answers

How to use "not" in XPath?

I want to write something of the sort: //a[not contains(@id, 'xx')] (meaning all the links that there 'id' attribute doesn't contain the string 'xx') I can't find the right syntax.
Guy
  • 12,478
  • 25
  • 61
  • 86
164
votes
4 answers

How to write an XPath query to match two attributes?

Following Question:
Given above, If I want a XPath expression with checks both id and class, can we do it w/ 'and' condition LIKE: //div[@id='id-74385'] and div[@class='guest…
shola
  • 1,649
  • 2
  • 11
  • 3
163
votes
3 answers

XPath OR operator for different nodes

How can I do with XPath: //bookstore/book/title or //bookstore/city/zipcode/title Just //title won't work because I also have //bookstore/magazine/title p.s. I saw a lot of or examples but mainly with attributes or single node structure.
user569008
  • 1,713
  • 2
  • 10
  • 10
157
votes
2 answers

XPath: select text node

Having the following XML: Text1text2 How do I select either the first or the second text node via XPath? Something like this: /node/text()[2] of course doesn't work because it's the merged result of every text inside the…
kernel
  • 3,434
  • 2
  • 23
  • 31
156
votes
1 answer

Getting the value of an attribute in XML

How would one get the value of attribute1 (blah) in the following xml using xslt:
Dallas
  • 17,186
  • 21
  • 64
  • 82
151
votes
1 answer

XSLT getting last element

I am trying to find the last element in my xml, which looks like:
Nik
  • 7,782
  • 6
  • 54
  • 79
151
votes
5 answers

How to use XPath contains() here?

I'm trying to learn XPath. I looked at the other contains() examples around here, but nothing that uses an AND operator. I can't get this to work: //ul[@class='featureList' and contains(li, 'Model')] On: ...
ryeguy
  • 60,742
  • 51
  • 186
  • 256
146
votes
15 answers

How to parse XML in Bash?

Ideally, what I would like to be able to do is: cat xhtmlfile.xhtml | getElementViaXPath --path='/html/head/title' | sed -e 's%(^|$)%%g' > titleOfXHTMLPage.txt
asdfasdfasdf
146
votes
2 answers

XPath query to get nth instance of an element

There is an HTML file (whose contents I do not control) that has several input elements all with the same fixed id attribute of "search_query". The contents of the file can change, but I know that I always want to get the second input element with…
rlandster
  • 6,310
  • 12
  • 50
  • 76
141
votes
4 answers

XPath to select multiple tags

nickf
  • 499,078
  • 194
  • 614
  • 709
126
votes
7 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,374
  • 2
  • 8
  • 9