tags:

views:

47

answers:

2

I think this might be more accurately called "paging"... not sure. I am getting a chunk of XML (lets say 100 nodes) I then want to display only a set number of them at a time on a page. How do you do this?

For reference, in .NET it would be something like this:

    // Get some results. These are an XPathExpression
    XPathNodeIterator iterate = nav.Select(results);
    int index = 0;
    // Iterate over them deleting excess results
    foreach (XPathNavigator node in iterate) {
        if ((index < beginIndex) || (index > finishIndex)) {
            node.DeleteSelf();
        }
        index++;
    }
    // Set iterate to be this new set of results
    iterate = nav.Select(results);
    // Write out my new result set
    foreach (XPathNavigator node in iterate) {
        Response.Write(node.OuterXml);
    }

and that would grab a subset of XML from an XML document, run through it deleting all nodes greater or less than my beginIndex and my finishIndex (so I would grab, say the first 20 results by setting beginIndex = 0 and finishIndex = 19 and next pass through I could grab nodes 20-29 and so on).

In a nutshell, in PHP, how do you delete the unwanted nodes like that bit that says node.DeleteSelf? Everything else I can do... just not sure about that delete bit.

A: 

You could use a PHP iterator class: http://php.net/manual/en/class.iterator.php

easement
+1  A: 

PHP's SimpleXMLElement documentation: http://php.net/manual/en/simplexmlelement.xpath.php

Shows that SimpleXmlElement::xpath returns an array of SimpleXMLElement objects, so you could do something like:

$result = $xml->xpath('string');
for ($i = $startIndex; $i < $startIndex + $pageSize; $i++){
    // also make sure that $result[$i] exists
    // Then handle printing $result[$i]
}
Johrn