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.

Community
  • 1
  • 1
Smile
  • 2,770
  • 4
  • 35
  • 57
  • 1
    is it `ends_with` or `ends-with`. There's a hyphen in function name – gwillie Oct 24 '13 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 http://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. – hakre Oct 24 '13 at 12:28
  • What you ask about is also outlined in the PHP manual: http://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). – hakre Oct 24 '13 at 12:29
  • @gwillie, You're right. But an answer poster has created function with `_` so it's `ends_with` – Smile Oct 24 '13 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. – hakre Oct 24 '13 at 12:54
  • @hakre, I also checked with it. but I will check your below answer – Smile Oct 24 '13 at 12:55

1 Answers1

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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I'm appreciating your depth answer :) that you replied it. – Smile Oct 24 '13 at 12:56
  • There is function name `ends-with(string1,string2)` in list in [w3schools.com](http://www.w3schools.com/xpath/xpath_functions.asp) – Smile Oct 24 '13 at 13:08
  • 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: http://www.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. – hakre Oct 24 '13 at 13:15
  • 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 :) – Smile Oct 24 '13 at 13:21
  • 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. – hakre Oct 24 '13 at 13:31
  • Perfect ! :) hakre. Working Great. Accepted your answer friend ;) – Smile Oct 25 '13 at 06:20
  • And yes. you can change question title and even question to make it more informative so users can clearly get the Idea of question asked and an answer you suggested. – Smile Oct 25 '13 at 06:26
  • Using $xpath->registerPHPFunctions(); without restrictions is evil! Someone that has access to your xsl templates can make your system do anything they want!! – Ton Snoei Dec 08 '14 at 07:34