Professional Web Applications Themes

#39593 [NEW]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists" - PHP Bugs

From: dave dot lane at gmx dot net Operating system: Linux PHP version: 5.2.0 PHP Bug Type: DOM XML related Bug description: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists" Description: ------------ The following code does not work. When the nodes in a NodeList that is the result of an XPath query are copied into an array and returned from a function. When the nodes in the array are used a warning results: "Couldn't fetch DOMElement. Node no longer exists..." Reproduce code: --------------- bug.php: <? $DOMMain =& OpenXMLDoc('main.xml'); $aArray =& GetArray($DOMMain); echo 'After return: '.$aArray[0]->localName->localName."<br>\n"; function &OpenXMLDoc($sFileName) { $DOM ...

  1. #1

    Default #39593 [NEW]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists"

    From: dave dot lane at gmx dot net
    Operating system: Linux
    PHP version: 5.2.0
    PHP Bug Type: DOM XML related
    Bug description: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists"

    Description:
    ------------
    The following code does not work. When the nodes in a NodeList that is the
    result of an XPath query are copied into an array and returned from a
    function. When the nodes in the array are used a warning results:
    "Couldn't fetch DOMElement. Node no longer exists..."

    Reproduce code:
    ---------------
    bug.php:
    <?
    $DOMMain =& OpenXMLDoc('main.xml');

    $aArray =& GetArray($DOMMain);
    echo 'After return: '.$aArray[0]->localName->localName."<br>\n";

    function &OpenXMLDoc($sFileName) {
    $DOM = new DOMDoent();
    $DOM->preserveWhiteSpace = false;
    $DOM->load($sFileName);
    return $DOM;
    }

    function &ExecuteXPath($sXPath, DOMDoent &$DOMDoent, DOMNode
    &$DOMNode) {
    $objXPath = new DOMXPath($DOMDoent);
    return $objXPath->query($sXPath, $DOMNode);
    }

    function &GetArray(&$DOMMain) {
    $DOMInclude =& OpenXMLDoc('include.xml');

    $NodeImported = $DOMMain->importNode($DOMInclude->doentElement,
    true);
    $NodeListImported =& ExecuteXPath('./include1', $DOMMain,
    $NodeImported);
    $NodeListImported = $NodeListImported->item(0)->childNodes;

    echo 'In NodeList:
    '.$NodeListImported->item(0)->localName."<br>\n";

    $aArray = array();
    foreach($NodeListImported as $Node) {
    $aArray[] = $Node;
    }
    echo 'In Array: '.$aArray[0]->localName."<br>\n";
    return $aArray;
    }

    ?>

    main.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <root>
    </root>

    include.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <include>
    <include1>
    <div>
    <span>test</span>
    </div>
    </include1>
    </include>


    Expected result:
    ----------------
    In NodeList: div
    In Array: div
    After return: div


    Actual result:
    --------------
    In NodeList: div
    In Array: div

    Warning: Couldn't fetch DOMElement. Node no longer exists in
    /var/www/html/usr/dla/csc/V2/bug/bug.php on line 5
    After return:


    --
    Edit bug report at [url]http://bugs.php.net/?id=39593&edit=1[/url]
    --
    Try a CVS snapshot (PHP 4.4): [url]http://bugs.php.net/fix.php?id=39593&r=trysnapshot44[/url]
    Try a CVS snapshot (PHP 5.2): [url]http://bugs.php.net/fix.php?id=39593&r=trysnapshot52[/url]
    Try a CVS snapshot (PHP 6.0): [url]http://bugs.php.net/fix.php?id=39593&r=trysnapshot60[/url]
    Fixed in CVS: [url]http://bugs.php.net/fix.php?id=39593&r=fixedcvs[/url]
    Fixed in release: [url]http://bugs.php.net/fix.php?id=39593&r=alreadyfixed[/url]
    Need backtrace: [url]http://bugs.php.net/fix.php?id=39593&r=needtrace[/url]
    Need Reproduce Script: [url]http://bugs.php.net/fix.php?id=39593&r=needscript[/url]
    Try newer version: [url]http://bugs.php.net/fix.php?id=39593&r=oldversion[/url]
    Not developer issue: [url]http://bugs.php.net/fix.php?id=39593&r=support[/url]
    Expected behavior: [url]http://bugs.php.net/fix.php?id=39593&r=notwrong[/url]
    Not enough info: [url]http://bugs.php.net/fix.php?id=39593&r=notenoughinfo[/url]
    Submitted twice: [url]http://bugs.php.net/fix.php?id=39593&r=submittedtwice[/url]
    register_globals: [url]http://bugs.php.net/fix.php?id=39593&r=globals[/url]
    PHP 3 support discontinued: [url]http://bugs.php.net/fix.php?id=39593&r=php3[/url]
    Daylight Savings: [url]http://bugs.php.net/fix.php?id=39593&r=dst[/url]
    IIS Stability: [url]http://bugs.php.net/fix.php?id=39593&r=isapi[/url]
    Install GNU Sed: [url]http://bugs.php.net/fix.php?id=39593&r=gnused[/url]
    Floating point limitations: [url]http://bugs.php.net/fix.php?id=39593&r=float[/url]
    No Zend Extensions: [url]http://bugs.php.net/fix.php?id=39593&r=nozend[/url]
    MySQL Configuration Error: [url]http://bugs.php.net/fix.php?id=39593&r=mysqlcfg[/url]
    dave dot lane at gmx dot net Guest

  2. #2

    Default #39593 [Opn->WFx]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists"

    ID: 39593
    Updated by: [email]rrichardsphp.net[/email]
    Reported By: dave dot lane at gmx dot net
    -Status: Open
    +Status: Wont fix
    Bug Type: DOM XML related
    Operating System: Linux
    PHP Version: 5.2.0
    New Comment:

    The problem is that you are working with a fragment that's not attached
    to the doent and only have a reference to a node deep within the
    fragment rather than to the top element of the fragment. When the
    object holding the fragment ($NodeImported) goes out of scope, the
    whole fragment is destroyed - as there isn't a way to track fragments
    without introducing way too much overhead. You still have the
    DOMElement object, just not the underlying xml data structure.

    you either need to return the entire fragment from the function call or
    remove the nodes you want to return from the fragment within the
    array.

    i.e. within the foreach() loop add:
    $Node->parentNode->removeChild($Node);

    This behavior while not optimal is a necessary evil to prevent leaking
    memory while also keeping system memory usage and performance at an
    acceptable level when using the DOM extension.


    Previous Comments:
    ------------------------------------------------------------------------

    [2006-11-22 17:58:31] dave dot lane at gmx dot net

    Sorry description should read:

    Description:
    ------------
    When the nodes in a NodeList that is the result of an XPath query are
    copied into an array and returned from a function the Nodes in the
    array although they exist are not valid. When the nodes in the array
    are used a warning results: "Couldn't fetch DOMElement. Node no longer
    exists..."

    ------------------------------------------------------------------------

    [2006-11-22 17:54:18] dave dot lane at gmx dot net

    Description:
    ------------
    The following code does not work. When the nodes in a NodeList that is
    the result of an XPath query are copied into an array and returned from
    a function. When the nodes in the array are used a warning results:
    "Couldn't fetch DOMElement. Node no longer exists..."

    Reproduce code:
    ---------------
    bug.php:
    <?
    $DOMMain =& OpenXMLDoc('main.xml');

    $aArray =& GetArray($DOMMain);
    echo 'After return: '.$aArray[0]->localName->localName."<br>\n";

    function &OpenXMLDoc($sFileName) {
    $DOM = new DOMDoent();
    $DOM->preserveWhiteSpace = false;
    $DOM->load($sFileName);
    return $DOM;
    }

    function &ExecuteXPath($sXPath, DOMDoent &$DOMDoent, DOMNode
    &$DOMNode) {
    $objXPath = new DOMXPath($DOMDoent);
    return $objXPath->query($sXPath, $DOMNode);
    }

    function &GetArray(&$DOMMain) {
    $DOMInclude =& OpenXMLDoc('include.xml');

    $NodeImported =
    $DOMMain->importNode($DOMInclude->doentElement, true);
    $NodeListImported =& ExecuteXPath('./include1', $DOMMain,
    $NodeImported);
    $NodeListImported = $NodeListImported->item(0)->childNodes;

    echo 'In NodeList:
    '.$NodeListImported->item(0)->localName."<br>\n";

    $aArray = array();
    foreach($NodeListImported as $Node) {
    $aArray[] = $Node;
    }
    echo 'In Array: '.$aArray[0]->localName."<br>\n";
    return $aArray;
    }

    ?>

    main.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <root>
    </root>

    include.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <include>
    <include1>
    <div>
    <span>test</span>
    </div>
    </include1>
    </include>


    Expected result:
    ----------------
    In NodeList: div
    In Array: div
    After return: div


    Actual result:
    --------------
    In NodeList: div
    In Array: div

    Warning: Couldn't fetch DOMElement. Node no longer exists in
    /var/www/html/usr/dla/csc/V2/bug/bug.php on line 5
    After return:



    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=39593&edit=1[/url]
    rrichards@php.net Guest

  3. #3

    Default #39593 [WFx]: XPath NodeList: "Couldn't fetch DOMElement: Node no longer exists"

    ID: 39593
    User updated by: dave dot lane at gmx dot net
    Reported By: dave dot lane at gmx dot net
    Status: Wont fix
    Bug Type: DOM XML related
    Operating System: Linux
    PHP Version: 5.2.0
    New Comment:

    OK excellent, thanks for the tip. It makes sense and I did suspect it
    had something to do with scope, I thought it was the NodeList that was
    causing the problem though.

    I think this should be doented though.


    Previous Comments:
    ------------------------------------------------------------------------

    [2006-11-22 19:06:06] [email]rrichardsphp.net[/email]

    The problem is that you are working with a fragment that's not attached
    to the doent and only have a reference to a node deep within the
    fragment rather than to the top element of the fragment. When the
    object holding the fragment ($NodeImported) goes out of scope, the
    whole fragment is destroyed - as there isn't a way to track fragments
    without introducing way too much overhead. You still have the
    DOMElement object, just not the underlying xml data structure.

    you either need to return the entire fragment from the function call or
    remove the nodes you want to return from the fragment within the
    array.

    i.e. within the foreach() loop add:
    $Node->parentNode->removeChild($Node);

    This behavior while not optimal is a necessary evil to prevent leaking
    memory while also keeping system memory usage and performance at an
    acceptable level when using the DOM extension.

    ------------------------------------------------------------------------

    [2006-11-22 17:58:31] dave dot lane at gmx dot net

    Sorry description should read:

    Description:
    ------------
    When the nodes in a NodeList that is the result of an XPath query are
    copied into an array and returned from a function the Nodes in the
    array although they exist are not valid. When the nodes in the array
    are used a warning results: "Couldn't fetch DOMElement. Node no longer
    exists..."

    ------------------------------------------------------------------------

    [2006-11-22 17:54:18] dave dot lane at gmx dot net

    Description:
    ------------
    The following code does not work. When the nodes in a NodeList that is
    the result of an XPath query are copied into an array and returned from
    a function. When the nodes in the array are used a warning results:
    "Couldn't fetch DOMElement. Node no longer exists..."

    Reproduce code:
    ---------------
    bug.php:
    <?
    $DOMMain =& OpenXMLDoc('main.xml');

    $aArray =& GetArray($DOMMain);
    echo 'After return: '.$aArray[0]->localName->localName."<br>\n";

    function &OpenXMLDoc($sFileName) {
    $DOM = new DOMDoent();
    $DOM->preserveWhiteSpace = false;
    $DOM->load($sFileName);
    return $DOM;
    }

    function &ExecuteXPath($sXPath, DOMDoent &$DOMDoent, DOMNode
    &$DOMNode) {
    $objXPath = new DOMXPath($DOMDoent);
    return $objXPath->query($sXPath, $DOMNode);
    }

    function &GetArray(&$DOMMain) {
    $DOMInclude =& OpenXMLDoc('include.xml');

    $NodeImported =
    $DOMMain->importNode($DOMInclude->doentElement, true);
    $NodeListImported =& ExecuteXPath('./include1', $DOMMain,
    $NodeImported);
    $NodeListImported = $NodeListImported->item(0)->childNodes;

    echo 'In NodeList:
    '.$NodeListImported->item(0)->localName."<br>\n";

    $aArray = array();
    foreach($NodeListImported as $Node) {
    $aArray[] = $Node;
    }
    echo 'In Array: '.$aArray[0]->localName."<br>\n";
    return $aArray;
    }

    ?>

    main.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <root>
    </root>

    include.xml:
    <?xml version = "1.0" encoding="ISO-8859-1"?>
    <include>
    <include1>
    <div>
    <span>test</span>
    </div>
    </include1>
    </include>


    Expected result:
    ----------------
    In NodeList: div
    In Array: div
    After return: div


    Actual result:
    --------------
    In NodeList: div
    In Array: div

    Warning: Couldn't fetch DOMElement. Node no longer exists in
    /var/www/html/usr/dla/csc/V2/bug/bug.php on line 5
    After return:



    ------------------------------------------------------------------------


    --
    Edit this bug report at [url]http://bugs.php.net/?id=39593&edit=1[/url]
    dave dot lane at gmx dot net Guest

Similar Threads

  1. #36795 [Com]: Inappropriate "unterminated entity reference" in DOMElement->setAttribute
    By philippe dot levan_nospam at kitpages dot fr in forum PHP Bugs
    Replies: 0
    Last Post: December 6th, 11:49 AM
  2. Replies: 0
    Last Post: November 22nd, 05:58 PM
  3. "couldn't find files" connecting to subversion repo viaWebDAV
    By whit537 in forum Macromedia Contribute Connection Administrtion
    Replies: 1
    Last Post: September 8th, 08:07 PM
  4. Replies: 0
    Last Post: May 24th, 07:35 PM
  5. SelectSingeNode("xpath")...how to test if null?
    By Kathy Burke in forum ASP.NET General
    Replies: 2
    Last Post: July 23rd, 09:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139