Search code examples
phpdomxpathdomxpath

Use DOMXpath query to retrieve all nodes with a class=integer


I currently have this portion of html with spans whose classes are integers:

<span class="1">
    .. some other stuff
</span>
<span class="2">
    .. some other stuff
</span>
<span class="3">
    .. some other stuff
</span>

I need to set a query to get these span child nodes whose classes are integers. Somehting like this:

$oDomObject = $oDomXpath->query('//span[contains(@class, number(something))]');

What would "something" be in order to get these span child nodes whose classes are integers? Is there such operator? The span tags only have class attribute with no id, or other. If I set "something" to "1"

$oDomObject = $oDomXpath->query('//span[contains(@class, number(1))]');

It return span tags with ids 1, 12, 21, any number containing 1. If I use "*"

$oDomObject = $oDomXpath->query('//span[contains(@class, number(*))]');

it returns nothing.

I hope I made sense.

Thank you!


Solution

  • The following will select all of the span elements who's class attribute value evaluates to a number using the number() function and is equal to the same value.

    //span[number(@class)=number(@class)]
    

    A non-numeric value will evaluate to NaN, which is unequal to any value (including NaN) and returns false for the predicate test.