ZF-3950: Zend_Dom_Query should remember its internal DOMDocument object

Description

Currently every call to Zend_Dom_Query::queryXpath() results in a completely new DOMDocument object being created, regardless of whether or not the underlying document string has changed. This means that the same node retrieved by two separate queries is not actually the same node, making it difficult to do comparisons.


$string = '
first itemsecond item'; $queryObject = new Zend_Dom_Query($string); // Retrieve the first list item once. $firstQueryResult = $queryObject->query('li'); $firstItemFromFirstQuery = current($firstQueryResult); // Retrieve the first list item again. $secondQueryResult = $queryObject->query('li'); $firstItemFromSecondQuery = current($secondQueryResult); echo (integer)($firstItemFromFirstQuery->isSameNode($firstItemFromSecondQuery));

The result I'd like to see here is 1 (true), but the actual result is 0 (false).

In order to make this work the way I was hoping, Zend_Dom_Query would need to avoid re-creating the DOMDocument object with each query, instead storing it internally and using it again later.

One downside of this approach is that the contents of $_document might not necessarily reflect the contents of the internally-stored DOMDocument object, if that object is modified by outside code.

Comments

I attached a sample script of a DOMDocument implementation. This enables you to solve this issue's problem and similarly ZF-3939.And this class enables us to handle a DOMDocument with a pointer to CSS-Queried objects. The usage is an aggregation of DOMDocument and Zend_Dom_Query. However I clipped part of the Zend_Dom_Query. If this approach make a sense,please review an attached scripts and add a comment.


<?php
require_once('Zend/Dom/DOMDocument.php');
$string = '
first itemsecond item'; $dom = new Zend_Dom_DOMDocument(); $dom->loadXML($string); // Retrieve the first list item once. $firstQueryResult = $dom->query('li'); $firstItemFromFirstQuery = $firstQueryResult->current(); // Retrieve the first list item again. $secondQueryResult = $dom->query('li'); $firstItemFromSecondQuery = $secondQueryResult->current(); echo (integer)($firstItemFromFirstQuery->isSameNode($firstItemFromSecondQuery)); // output: 1

Hi guys,

I know this is not the proper forum to ask. but just one question. and maybe you can kick me off from this site. I was wondering how to use Zend_Dom. I tried my best searching for a good resource on the internet which gives tutorial on how to use it but aside from the brief introduction on Zend online manual I can't find anything more.

Anyway, i'm just particularly confused on how to use the DomDocument/DomElement objects. What are its member functions? I tried searching for the file which holds these classes in my installation but I can't figure where it's located.

Can you just help me understand and probably give me a sample usage of this plugin? Or maybe just redirect me to the proper forum where I can learn this thing.

Looking forward for you reply.

Thanks!

Bulk change of all issues last updated before 1st January 2010 as "Won't Fix".

Feel free to re-open and provide a patch if you want to fix this issue.