0

I want to selecting an XML node based on whether it has a child with a URL attribute set to a certain value.

I think this code shows phpQuery doesn't parse URL correctly... but I may have missed something regarding escaping the =...

Any ideas?

<?php
include '../libs/phpQuery.php';
phpQuery::newDocumentXML('
   <doc><item>item 1<link url="http://example.com" /></item>
        <item>item 2<link url="http://example.com?abc" /></item>
        <item>item 3<link url="http://example.com?abc=" /></item>
        <item>item 4<link url="http://example.com?abc=21" /></item>
   </doc>');

echo "<pre>
".
pq("link[url='http://example.com']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc=']:first")->parents('item')->xml()
."
".
pq("link[url='http://example.com?abc=21']:first")->parents('item')->xml()
."
</pre>";
?>

This is returning

<pre>
item 1<link url="http://example.com"/>
item 2<link url="http://example.com?abc"/>
item 2<link url="http://example.com?abc"/>
item 2<link url="http://example.com?abc"/>
</pre>
7
  • I agree that it shows that phpQuery isn't working properly but you also might be confused about what :first is supposed to do Commented Apr 11, 2015 at 22:29
  • The intention of 'first' was to get the first link tha matche (in case there are duplicates). I had changed the order of the links to see if phpQuery was getting the original parsing wrong (and all links were the same internally) or failing in the later search (it seems to be the search that fails). Is this not what first will achieve? Commented Apr 12, 2015 at 8:00
  • It's actually not too clear in the css spec but I believe that :first should match the first li, whether it matches or not. So if the first li doesn't match it should probably return nothing. There's better ways to get the first match. Commented Apr 12, 2015 at 8:20
  • Hi - I did more testing - with with duplicate URLs, :first and :last works as I expected (just let to right). Its not clear which branch of phpquery is current - so think I'll have to debug it myself... Commented Apr 12, 2015 at 12:22
  • I just tested it myself, my phpQuery doesn't match these selectors properly. In your example all four link's are :first and :last as well as :only-child, do you see why? Commented Apr 12, 2015 at 22:21

1 Answer 1

0

In phpQuery/phpQueryObject.html around line 749

The attribute and its required value are parsed using

                    $value = null;
                    list($attr, $value) = explode('=', $attr);
                    $value = trim($value, "'\"");

So the second '=' and everything after are lost (exploded into a third value that isn't captured). I changed this to

                // attr with specifed value
                if (mb_strpos($s, '=')) {
                    $value = null;
                    $raw = $attr;
                    parse_str($raw,$ary);
                    $attr = current(array_keys($ary));
                    $value = substr($raw,strlen($attr)+1);
                    $value = trim($value, "'\"");

Its a bit (very) naff, but it works for my app!

Note the key is extracted from the 'parse' value, then the value is the remainder of the string.

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

Comments

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.