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
94
votes
2 answers

Selecting elements whose attribute begins with something in XPath

As the title says, is it possible to select elements in XPath that only begin with a certain string, but perhaps do not end with the same? For example there are 3 anchor elements:
Allen Gingrich
  • 5,608
  • 10
  • 45
  • 57
94
votes
6 answers

XSLT string replace

I don't really know XSL but I need to fix this code, I have reduced it to make it simpler. I am getting this error Invalid XSLT/XPath function on this line This is the…
Aximili
  • 28,626
  • 56
  • 157
  • 216
93
votes
5 answers

XPath and XSLT 2.0 for .NET?

.NET 3.5 doesn't completely support XPATH 2.0 or XSLT 2.0, which is just too bad. Does anyone know if these two will be included and fully supported in any future .NET versions?
Wim ten Brink
  • 25,901
  • 20
  • 83
  • 149
93
votes
6 answers

Selenium Webdriver finding an element in a sub-element

I am trying to search for an element in a sub-element with Selenium (Version 2.28.0), but selenium des not seem to limit its search to the sub-element. Am I doing this wrong or is there a way to use element.find to search a sub-element? For an…
Dominic Larkin
  • 1,184
  • 3
  • 10
  • 18
92
votes
6 answers

case-insensitive matching in XPath?

For example, for the XML below How to match the first 4 records…
Ethan
  • 18,584
  • 15
  • 51
  • 72
92
votes
2 answers

Get Nth child of a node using xpath

My sample input XML is: item item1 item2 item3 item4 I am suppose to select a node b whose position is the value of a variable. How can I use the value of a variable to…
IordanTanev
  • 6,130
  • 5
  • 40
  • 49
92
votes
8 answers

Find position of a node using XPath

Anyone know how to get the position of a node using XPath? Say I have the following xml: zyx wvu tsr qpo I can use the following xpath query to select the third node…
Wilfred Knievel
  • 3,225
  • 1
  • 26
  • 33
91
votes
6 answers

Selecting a css class with xpath

I want to select just a class on its own called .date For some reason, I cannot get this to work. If anyone knows what is wrong with my code, it would be much appreciated. @$doc = new DOMDocument(); @$doc->loadHTML($html); $xml =…
Teddy13
  • 3,824
  • 11
  • 42
  • 69
89
votes
1 answer

How to select all elements with a specific attribute? with TinyXPath

What is the XPath expression for selecting all elements with attribute A? const char* xpath = "//\*/\*[@A]"
eugene
  • 39,839
  • 68
  • 255
  • 489
88
votes
1 answer

xpath: find a node that has a given attribute whose value contains a string

Is there an xpath way to find a node that has a given attribute whose value contains a given string? For example I have an xml document and want to find a node where the address attribute contains the string Downing, so that I could find the…
flybywire
  • 261,858
  • 191
  • 397
  • 503
87
votes
4 answers

How to find parent elements by python webdriver?

Is there any methods for python+selenium to find parent elements, brother elements, or child elements just like driver.find_element_parent? or driver.find_element_next? or driver.find_element_previous? eg: