php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #52751 XPath processing-instruction() function is not supported.
Submitted: 2010-08-31 11:24 UTC Modified: 2010-08-31 11:53 UTC
Votes:3
Avg. Score:4.3 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: ivan dot enderlin at hoa-project dot net Assigned:
Status: Closed Package: SimpleXML related
PHP Version: Irrelevant OS: All
Private report: No CVE-ID: None
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
48 + 42 = ?
Subscribe to this entry?

 
 [2010-08-31 11:24 UTC] ivan dot enderlin at hoa-project dot net
Description:
------------
SimpleXML misses a XPath function which is processing-instruction(). This last one enables to reach/select PIs along the XML document.

Test script:
---------------
<?php

$xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n\n" .
       '<foo>' . "\n" .
       '  <bar>Gordon</bar>' . "\n" .
       '  <bar><![CDATA[Gordon]]></bar>' . "\n" .
       '  <bar><?baz href="ftw" ?></bar>' . "\n" .
       '</foo>';

$sxe = simplexml_load_string($xml);

var_dump(
    $sxe->xpath('//bar')
);

var_dump(
    $sxe->xpath('//processing-instruction(\'baz\')')
);

Expected result:
----------------
//bar

array(3) {
  [0]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(6) "Gordon"
  }
  [1]=>
  object(SimpleXMLElement)#3 (0) {
  }
  [2]=>
  object(SimpleXMLElement)#4 (1) {
    ["baz"]=>
    object(SimpleXMLElement)#5 (0) {
    }
  }
}


//processing-instruction('baz')
array(1) {
  [2]=>
  object(SimpleXMLElement)#6 (1) {
    …
  }
}

Actual result:
--------------
//bar

array(3) {
  [0]=>
  object(SimpleXMLElement)#2 (1) {
    [0]=>
    string(6) "Gordon"
  }
  [1]=>
  object(SimpleXMLElement)#3 (0) {
  }
  [2]=>
  object(SimpleXMLElement)#4 (1) {
    ["baz"]=>
    object(SimpleXMLElement)#5 (0) {
    }
  }
}


//processing-instruction('baz')
array(0) {
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-31 11:52 UTC] ivan dot enderlin at hoa-project dot net
I forget to notice that DOMXPath support this function.
If we take the first code example:

    $doc   = dom_import_simplexml($sxe)->ownerDocument;
    $xpath = new DOMXPath($doc);
    $e     = $xpath->query('//processing-instruction(\'baz\')');
    var_dump($e, $e->length, $e->item(0)->data);

It will output:

    object(DOMNodeList)#8 (0) {
    }
    int(1)
    string(11) "href="ftw" "

That's what we expected!


But I think that SimpleXML does not simply support PI.
Maybe you noticed that when outputing //bar, in the 2 offset, we have “baz”. Well, try to use it:

    $handle = $sxe->xpath('//bar');
    var_dump(
        dom_import_simplexml($handle[2])
        ->childNodes
        ->item(0)
        ->data
    );

It will output:

    string(11) "href="ftw" "

We must use the DOM API to reach PI data.


Conclusion:
  • processing-instruction() works with DOMXpath, not with SimpleXML.
  • PIs appear in the DOM and the SimpleXML tree.
  • PIs are usable only with the DOM API.
 [2010-08-31 11:53 UTC] ivan dot enderlin at hoa-project dot net
-Package: *XML functions +Package: SimpleXML related
 [2010-08-31 11:53 UTC] ivan dot enderlin at hoa-project dot net
Change bug's package.
 [2014-09-25 08:18 UTC] ivan dot enderlin at hoa-project dot net
ping?
 [2023-09-10 22:16 UTC] git@php.net
Automatic comment on behalf of nielsdos
Revision: https://github.com/php/php-src/commit/107443b311a7fef97ae0815203a956d4c35fdcfe
Log: Fix #52751: XPath processing-instruction() function is not supported.
 [2023-09-10 22:16 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 14 01:01:28 2024 UTC