3

this my xpath

//dns:tbody//dns:td[1][@rowspan="1"]/text()

I want to find a solution how can make the attribute rowspan @rowspan=" only number " accept just number

I'm looking for solution regular expression in XPath 1.0

Regexp =^[0-9]*$ my problem I don't know how to integrate the regex with XPath1.0 or any other solution

MokiNex
  • 857
  • 1
  • 8
  • 21
  • With XPath 1.0 you can't, although using regexes would be trivial with XPath 2.0. You could work around it using `number()` – revo May 15 '18 at 14:47
  • my xpath will be like that `//dns:tbody//dns:td[1][@rowspan=number()]/text()`?? – MokiNex May 15 '18 at 14:50

3 Answers3

2

You can use a workaround that tests:

  • if the text is a number (with the number() function)
  • if the text doesn't start with - (with starts-with())
  • if the text doesn't contain the dot (with contains())

like that:

//dns:tbody//dns:td[1][string(number(@rowspan))!='NaN'][not(starts-with(@rowspan, '-'))][not(contains(@rowspan, '.'))]/text()

or

//dns:tbody//dns:td[1][not(string(number(@rowspan))='NaN' or starts-with(@rowspan, '-') or contains(@rowspan, '.'))]/text()
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

Try to use below solution instead of regex:

//dns:tbody//dns:td[1][number(@rowspan)<=0 or number(@rowspan)>0]/text()

This should match @rowspan with int value

Andersson
  • 51,635
  • 17
  • 77
  • 129
0

Some XPath implementations allow you to supply your own functions. You will need to check the documentation on your XPath implementation.

Java: http://xml.apache.org/xalan-j/xpath_apis.html#functionresolver

Not Saying
  • 194
  • 11
  • I'm not allowed to use the function just XPath because I'm using a workflow node accept just XPath query – MokiNex May 15 '18 at 15:02