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
6
votes
1 answer

Convert HTML text to Leet (1337) Speak with XPath

I want to transform a webpage into leet (1337) speak with XPath and PHP. It can be done with only PHP but then the HTML nodes are also replaced with leet speak. Example ($html is the webpage): $find =…
user1480019
6
votes
2 answers

Test XPath expressions with namespaces in Chrome

I have a XML document opened in Chrome and I would like to test some XPath expressions. The XML document involves different namespaces. How do I define the prefix URIs? For testing XPath expressions in Chrome I have used $x("...") so far.
Mahoni
  • 7,088
  • 17
  • 58
  • 115
6
votes
6 answers

XSLT to sum product of two attributes

I have the following XML source structure:
staterium
  • 1,980
  • 2
  • 20
  • 32
6
votes
1 answer

Remove non-breakable whitespaces using xpath

I have the following xml document:  Well, some spaces and nbsps     some more   or whatever a…
user1800825
  • 203
  • 1
  • 4
  • 17
6
votes
2 answers

HtmlAgilityPack SelectNodes expression to ignore an element with a certain attribute

I am trying to select nodes except from script nodes and a ul that has a class called 'relativeNav'. Can someone please direct me to the right path? I have been searching for this for a week and I can't find it anywhere. Currently I have this but it…
thaky
  • 63
  • 1
  • 4
6
votes
1 answer

xpath 1 and xpath 2 return different results when determining min and max value

I have following question relating to XPATH1 and XPATH2: 3 11 3 2 12 5 0 7 xpath1: max //val[not(. <…
Fl0R1D3R
  • 862
  • 2
  • 11
  • 22
6
votes
3 answers

Get Xpath dynamically using ElementTree getpath()

I need to write a dynamic function that finds elements on a subtree of an ATOM xml by building dynamically the XPath to the element. To do so, I've written something like this: tree = etree.parse(xmlFileUrl) e = etree.XPathEvaluator(tree,…
puntofisso
  • 383
  • 1
  • 3
  • 17
6
votes
2 answers

Python Selenium Webdriver to check if element does NOT exist takes time

Trying to verify after few GUI operations some button does not exist (expected not to be present). I am using find_element_by_xpath() but its very slow. Any solution of timeout?
Abhishek Kulkarni
  • 3,693
  • 8
  • 35
  • 42
6
votes
3 answers

Selenium WebDriver - Unable to close select drop down menu in Chrome on Mac OS X

I have been Working with Selenium WebDriver for a few months now and I have a problem with a drop down menu within a web app that I am working on. What is happening is that the test is opening the page, verifying several elements on the page by…
6
votes
1 answer

Find text nodes that contain a word with xpath in dom

I need to extract the text from the nodes in an html file and I'm trying to use XPath and Javascript. The required condition is that the text must contain an specific word. Let's take by example the next html file:

dysfuntcional
  • 109
  • 1
  • 1
  • 7
6
votes
1 answer

Getting exactly one element or raising exception with lxml xpath

The xpath() function in lxml normally returns a list of elements. If I have an XPath which I expect to return exactly one element, what's the nicest way to: Check that one element is returned or else raise an exception, and: Get that element (as…
mskel
  • 842
  • 9
  • 16
6
votes
1 answer

php xpath get contents of div with class

What is the right syntax to use xpath to get the contents of all divs with a certain class? i seem to be getting the divs but i don't know how to get their innerHTML. $url =…
David
  • 10,418
  • 17
  • 72
  • 122
6
votes
1 answer

Extracting a term from SOAP header

I would like to extract an element called ServiceGroupID from the SOAP header, which specifies the session of the transaction. I would need this so that I could direct the request to the same server using SOAP session. My XML is as follow:
Spaniard89
  • 2,359
  • 4
  • 34
  • 55
6
votes
1 answer

primefaces selectOneMenu value using Selenium Webdriver+Java

I need to set value to primefaces or JSF selectOneMenu using webdriver. I am able to achieve this using index but could not set value directly. The following code is working with…
Naresh Kavala
  • 183
  • 1
  • 6
  • 17
6
votes
3 answers

How can I use the Xpath function 'contains()' to return nothing if it's search param is blank or missing/false?

I'm trying to write an Xpath Statement (1.0) that can read info from a 'search' node and perform a search using it. I was making some nice progress, but stumbled across an issue where if an attribute (used for a value in the search) is empty or…
Futile32
  • 834
  • 2
  • 8
  • 15
1 2 3
99
100