Alex Rivera | Logout

PHP XPath ends-with

Asked 2011-03-25T16:32:26.847
15

Possible Duplicate:
How to use XPath function in a XPathExpression instance programatically?

I'm trying to find all of the rows of a nested table that contain an image with an id that ends with '_imgProductImage'.

I'm using the following query:

"//tr[/td/a/img[ends-with(@id,'_imgProductImage')]"

I'm getting the error: xmlXPathCompOpEval: function ends-with not found

My google searches i believe say this should be a valid query/function. What's the actual function i'm looking for if it's not "ends-with"?

Edit
Report

1 Answer

9

It seems that ends-with() is an XPath 2.0 function.

DOMXPath only supports XPath 1.0


Edit after the comment : In your case, I suppose you'll have to :

  • Find all images, using a simpler XPath query, that will return more images than what you want -- but include those you want to keep.
  • Loops over those, testing in PHP, for each one of them, if the id attribute (see the getAttribute method) matches what you want.

To test if the attribute is OK, you could use something like this, in the loop that iterates over the images :

$id = $currentNode->getAttribute('id');
if (preg_match('/_imgProductImage$/', $id)) {
    // the current node is OK ;-)
}

Note that, in my regex pattern, I used a $ to indicate end of string.

answered 2011-03-25T16:35:45.070

Your Answer