1

How can I register PHP function in XPATH? Because XPATH does not allows me to use ends-with()

Here is one solutions given by one member but it does not works with.

The code he has used is:

$xpath = new DOMXPath($document);
$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPHPFunctions("ends_with");
$nodes = $x->query("//tr[/td/a/img[php:function('ends-with',@id,'_imgProductImage')]"

function ends_with($node, $value){
    return substr($node[0]->nodeValue,-strlen($value))==$value;
}

I am using PHP 5.3.9.

6
  • 1
    is it ends_with or ends-with. There's a hyphen in function name Commented Oct 24, 2013 at 11:06
  • Your question title is much misleading. Please leave a comment under the (faulty) answer. And as long as you're having that concrete problem, please use stackoverflow.com/a/5435487/367456 from there. If you insist on asking that question, please create a self-contained and working example code in your question. The code you've posted so far can not be executed in it's current form. Commented Oct 24, 2013 at 12:28
  • What you ask about is also outlined in the PHP manual: php.net/domxpath.registerphpfunctions - So you probably should make more clear what your concrete programming question is here. Or is it only that you stumbled over a non-working answer? If so, just downvote the non-working answer (or correct it). Commented Oct 24, 2013 at 12:29
  • @gwillie, You're right. But an answer poster has created function with _ so it's ends_with Commented Oct 24, 2013 at 12:53
  • Just change the 'ends-with' inside your xpath to 'ends_with' and you should be fine. I think that is what @gwillie wanted to say. At least I'd say that is the first thing one should try out as it seems to be the easy spot to fix this. Commented Oct 24, 2013 at 12:54

1 Answer 1

4

In your question it looks like a typo, there is no function named ends-with therefore I would expect it not to work:

//tr[/td/a/img[php:function('ends-with',@id,'_imgProductImage')]
                             ^^^^^^^^^

Instead use the right syntax, e.g. the correct function name:

//tr[/td/a/img[php:function('ends_with',@id,'_imgProductImage')]
                             ^^^^^^^^^

Or for example like with the following example:

book.xml:

<?xml version="1.0" encoding="UTF-8"?>
<books>
 <book>
  <title>PHP Basics</title>
  <author>Jim Smith</author>
  <author>Jane Smith</author>
 </book>
 <book>
  <title>PHP Secrets</title>
  <author>Jenny Smythe</author>
 </book>
 <book>
  <title>XML basics</title>
  <author>Joe Black</author>
 </book>
</books>

PHP:

<?php
$doc = new DOMDocument;
$doc->load('book.xml');

$xpath = new DOMXPath($doc);

// Register the php: namespace (required)
$xpath->registerNamespace("php", "http://php.net/xpath");

// Register PHP functions (no restrictions)
$xpath->registerPHPFunctions();

// Call substr function on the book title
$nodes = $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]');

echo "Found {$nodes->length} books starting with 'PHP':\n";
foreach ($nodes as $node) {
    $title  = $node->getElementsByTagName("title")->item(0)->nodeValue;
    $author = $node->getElementsByTagName("author")->item(0)->nodeValue;
    echo "$title by $author\n";
}

As you can see, this example registers all PHP functions including the existing substr() function.

See DOMXPath::registerPHPFunctions for more information, that is also where the code example has been taken from.

I hope this is helpful, let me know if you still have a question about this.

See as well:

Sign up to request clarification or add additional context in comments.

8 Comments

I'm appreciating your depth answer :) that you replied it.
There is function name ends-with(string1,string2) in list in w3schools.com
You are mixing two languages here: There is XPath (which has ends-with in version 2.0) and there is PHP (which has end_with in your code example). So better not just copy over code and then wonder why it does not work: You need to understand what it does first. And yes, there is ends-with in Xpath 2.0, but not by w3schools, but by the authorative sepcification documents which are here: w3.org/TR/xquery-operators/#func-ends-with - Take care when multiple langauges are involved you know which one you're using and what is valid.
Yes. You're right. I had tried with ends-with in XPath. But it's supported in version 2.0. I agree. But an example which I seen has registred PHP function with XPath like you also has done like registreing PHP function throught Xpath. So it was not working that's why I asked. Please don't mind it my answer as rude but just letting you know dear friend :)
Yes, no problems taken here, and not rude at all. Just hoping you understand that well as when things mix up it can become a bit complicated.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.