XPath test if node value is number

Test the value against NaN: <xsl:if test=”string(number(myNode)) != ‘NaN'”> <!– myNode is a number –> </xsl:if> This is a shorter version (thanks @Alejandro): <xsl:if test=”number(myNode) = myNode”> <!– myNode is a number –> </xsl:if>

Getting element’s name in XPATH

Use name(). (Find docs for newer versions of the XPath language here.) Here are modified versions of your example: Works in XPath 2.0+ only: //element/*[@id=’elid’]/name() Works in XPath 1.0 and 2.0+*: name(//element/*[@id=’elid’]) *If using 2.0+, the expression //element/*[@id=’elid’] must only return one element. Otherwise you’ll get an error like A sequence of more than one … Read more

What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing?

Absolute Xpath: It uses Complete path from the Root Element to the desire element. Relative Xpath: You can simply start by referencing the element you want and go from there. Relative Xpaths are always preferred as they are not the complete paths from the root element. (//html//body). Because in future, if any webelement is added/removed, … Read more

XPath : select all following siblings until another sibling

You could do it this way: ../node[not(text()) and preceding-sibling::node[@id][1][@id=’1′]] where ‘1’ is the id of the current node (generate the expression dynamically). The expression says: from the current context go to the parent select those child nodes that have no text and from all “preceding sibling nodes that have an id” the first one must … Read more

Xpath “ends-with” does not work

The ends-with function is part of xpath 2.0 but browsers (you indicate you’re testing with chrome) generally only support 1.0. So you’ll have to implement it yourself with a combination of string-length, substring and equals substring(@id, string-length(@id) – string-length(‘register’) +1) = ‘register’

XPath OR operator for different nodes

All title nodes with zipcode or book node as parent: Version 1: //title[parent::zipcode|parent::book] Version 2: //bookstore/book/title|//bookstore/city/zipcode/title Version 3: (results are sorted based on source data rather than the order of book then zipcode) //title[../../../*[book] or ../../../../*[city/zipcode]] or – used within true/false – a Boolean operator in xpath | – a Union operator in xpath that … Read more