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
458
votes
10 answers

How can I match on an attribute that contains a certain string?

I am having a problem selecting nodes by attribute when the attributes contains more than one word. For example:
This is my xpath expression: //*[@class='atag'] The expression works with
but not…
crazyrails
  • 4,664
  • 3
  • 14
  • 7
366
votes
8 answers

Getting attribute using XPath

Given an XML structure like so: Harry Potter 29.99 Learning XML
GurdeepS
  • 58,809
  • 95
  • 236
  • 371
347
votes
7 answers

How can I find an element by CSS class with XPath?

In my webpage, there's a div with a class named Test. How can I find it with XPath?
Strawberry
  • 58,930
  • 53
  • 138
  • 190
332
votes
9 answers

How to select the first element with a specific attribute using XPath

The XPath bookstore/book[1] selects the first book node under bookstore. How can I select the first node that matches a more complicated condition, e.g. the first node that matches /bookstore/book[@location='US']
ripper234
  • 202,011
  • 255
  • 600
  • 878
314
votes
10 answers

Is there a way to get element by XPath using JavaScript in Selenium WebDriver?

I am looking for something like: getElementByXpath(//html[1]/body[1]/div[1]).innerHTML I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can't find it itself), but how? I could use ID…
pMan
  • 7,118
  • 10
  • 29
  • 34
294
votes
8 answers

Extract value of attribute node via XPath

How can I extract the value of an attribute node via XPath? A sample XML file is: child2_Parent_1
Rehman
  • 3,590
  • 6
  • 26
  • 28
290
votes
6 answers

XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

I have a small problem with Xpath contains with dom4j ... Lets say my XML is ABC 5 BLAH BLAH BLAH

ABC
Lets say I want to…
Mike Milkin
  • 2,971
  • 4
  • 15
  • 10
283
votes
8 answers

How to read XML using XPath in Java

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement. here is what I want to do: Get XML file from online via its URL, then use XPath to parse it, I want to create…
kaibuki
  • 16,208
  • 34
  • 75
  • 111
236
votes
12 answers

How to use XPath in Python?

What are the libraries that support XPath? Is there a full implementation? How is the library used? Where is its website?
yeruham
236
votes
18 answers

Is there a JSON equivalent of XQuery/XPath?

When searching for items in complex JSON arrays and hashes, like: [ { "id": 1, "name": "One", "objects": [ { "id": 1, "name": "Response 1", "objects": [ // etc. }] } ] Is there some kind of query language I can…
Naftuli Kay
  • 75,812
  • 80
  • 244
  • 374
230
votes
1 answer

XPath to select element based on childs child value

Trying to select an element based on the value of one of it's childrens childrens Thinking the following but not working, appreciate any help, thanks ./book[/author/name = 'John'] or ./book[/author/name text() = 'John'] Want all books where the…
James Walsh
  • 2,303
  • 2
  • 12
  • 4
230
votes
2 answers

XPath: How to select elements based on their value?

I am new to using XPath and this may be a basic question. Kindly bear with me and help me in resolving the issue. I have an XML file like this: Data
vcosk
  • 2,714
  • 2
  • 20
  • 23
212
votes
17 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
  • 6,908
  • 5
  • 41
  • 48
209
votes
3 answers

XPath to select Element by attribute value

I have following XML. 40 Tom Male Manager
Pankaj
  • 4,738
  • 3
  • 25
  • 36
206
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,561
  • 7
  • 40
  • 50
1
2 3
99 100