How to get a node with Xpath which has only digits in the text attribute?

29 views Asked by At

I have this kind of DOM:

<a>abc</a>
<a>def</a>
<a>13456</a>
<a>gh564</a>

I want to get the element which contains only digits.

I could do it by getting all the elements and looping them to check if text() is digits, but I would prefer to find it directly with Xpath. Is it possible?

3

There are 3 answers

0
Martin Honnen On

Use //a[number() = number()], but that would work for floating point content like <a>3.14</a> too, so perhaps //a[matches(., '^[0-9]+$')]. Or the old, XPath 1, translate test: //a[not(translate(., '0123456789', ''))].

0
Michael Kay On

In XPath 2.0 you can use the matches() function with a regular expression.

0
Dimitre Novatchev On

Besides the solution using the matches() function, here are two more solutions:

//*[. castable as xs:integer]

//*[normalize-space() and not(translate(., '0123456789', ''))])

The 2nd solution can be used on a pure XPath 1 - (only) supporting platform.


XSLT-based verification:

This transformation:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence select=
      "(//*[. castable as xs:integer],
        //*[normalize-space() and not(translate(., '0123456789', ''))])"/>

  </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>
    <a>abc</a>
    <a>def</a>
    <a>13456</a>
    <a>gh564</a>
</t>

evaluates both XPath expressions and copies to the output the results of these evaluations:

<a>13456</a>
<a>13456</a>