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
478
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,864
  • 3
  • 16
  • 7
433
votes
8 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
  • 66,024
  • 56
  • 149
  • 197
431
votes
11 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
  • 8,808
  • 11
  • 32
  • 35
415
votes
8 answers

Getting attribute using XPath

Given an XML structure like so: Harry Potter 29.99 Learning XML
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
393
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
  • 222,824
  • 274
  • 634
  • 905
389
votes
7 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 ... Let's say my XML is ABC 5 BLAH BLAH BLAH

ABC
Let's say I want to…
Mike Milkin
  • 3,961
  • 4
  • 17
  • 10
322
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,908
  • 6
  • 28
  • 29
295
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…
Adil Bhatty
  • 17,190
  • 34
  • 81
  • 118
265
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
  • 71,848
  • 143
  • 435
  • 658
257
votes
11 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
253
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,533
  • 2
  • 14
  • 4
252
votes
19 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
  • 87,710
  • 93
  • 269
  • 411
247
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,894
  • 2
  • 23
  • 23
245
votes
6 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,455
  • 2
  • 12
  • 6
243
votes
3 answers

XPath to select Element by attribute value

I have following XML. 40 Tom Male Manager
Pankaj
  • 5,132
  • 3
  • 28
  • 37
1
2 3
99 100