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
230
votes
18 answers

How to execute XPath one-liners from shell?

Is there a package out there, for Ubuntu and/or CentOS, that has a command-line tool that can execute an XPath one-liner like foo //element@attribute filename.xml or foo //element@attribute < filename.xml and return the results line by line? I'm…
clacke
  • 7,688
  • 6
  • 46
  • 48
219
votes
10 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
  • 222,824
  • 274
  • 634
  • 905
219
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
  • 3,223
  • 5
  • 22
  • 35
209
votes
6 answers

XPath find if node exists

Using a XPath query how do you find if a node (tag) exists at all? For example if I needed to make sure a website page has the correct basic structure like /html/body and /html/head/title.
EddyR
  • 6,891
  • 7
  • 42
  • 50
196
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,969
  • 2
  • 12
  • 3
188
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,963
  • 2
  • 11
  • 10
187
votes
5 answers

How to use "not" in XPath?

I want to write something of the sort: //a[not contains(@id, 'xx')] (meaning all the links who's 'id' attribute doesn't contain the string 'xx') I can't find the right syntax.
Guy
  • 14,178
  • 27
  • 67
  • 88
181
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
  • 7,294
  • 14
  • 58
  • 96
181
votes
1 answer

XSLT getting last element

I am trying to find the last element in my xml, which looks like:
Nik
  • 9,063
  • 7
  • 66
  • 81
174
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,654
  • 3
  • 25
  • 33
169
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:
Smern
  • 18,746
  • 21
  • 72
  • 90
165
votes
17 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
164
votes
4 answers

XPath to select multiple tags

nickf
  • 537,072
  • 198
  • 649
  • 721
160
votes
4 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
  • 65,519
  • 58
  • 198
  • 260
158
votes
10 answers

can we use XPath with BeautifulSoup?

I am using BeautifulSoup to scrape an URL and I had the following code, to find the td tag whose class is 'empformbody': import urllib import urllib2 from BeautifulSoup import BeautifulSoup url = …
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313