O poder do XPath no SimpleXML

PHP
Enviado por Eclesiastes em Seg, 18/06/2007 - 15:57.PHP

Segue abaixo uma série de exemplos de expressões XPath, demonstrando suas funcionalidades no acesso aos elementos de um XML com a lib. SimpleXML.
Os textos dos exemplos foram retirados da referência XPath http://www.zvon.org/xxl/XPathTutorial/General/examples.html.

<?php 

/* XPath referência: 
    http://www.zvon.org/xxl/XPathTutorial/General/examples.html */ 

$source = '<foobar> 
    <foo>
        <bar>test</bar> 
        <bar>test2</bar> 
    </foo> 
    <bar id="1">f</bar> 
    <bar id="2">o</bar> 
    <bar id="3">o</bar> 
    <aaa name=" foo "> 
        <bbb> 
            <ccc> 
                <ddd> 
                    <eee>foobar!</eee> 
                </ddd> 
            </ccc> 
        </bbb> 
    </aaa> 
</foobar>'; 

$foo = simplexml_load_string($source); 

/* Select the first bar child of element foo */ 
// print_r($foo->xpath('foo/bar[1]')); 

/* Select the last bar child of element foo */ 
// print_r($foo->xpath('foo/bar[last()]')); 

/* Select bar elements which have attribute id */ 
// print_r($foo->xpath('//bar[@id]')); 

/* Select bar elements which have any attribute */ 
// print_r($foo->xpath('//bar[@*]')); 

/* Select bar elements without an attribute */ 
// print_r($foo->xpath('//bar[not(@*)]')); 

/* Select all elements with name bar, equivalent with //bar */ 
// print_r($foo->xpath('//*[name()="bar"]')); 

/* Select all elements name of which starts with letter f */ 
// print_r($foo->xpath('//*[starts-with(name(), "f")]')); 

/* Select all elements name of which contain letter f */ 
// print_r($foo->xpath('//*[contains(name(), "f")]')); 

/*  Select aaa elements which have attribute name with value foo, leading and 
    trailing spaces are removed before comparison */ 
// print_r($foo->xpath('//aaa[normalize-space(@name)="foo"]')); 

/* Select elements which have two children bar */ 
// print_r($foo->xpath('//*[count(bar)=2]')); 

/* Select elements which have 2 children */ 
// print_r($foo->xpath('//*[count(*)=2]')); 

/* Select elements with three-letter name */ 
// print_r($foo->xpath('//*[string-length(name())=3]')); 

/* Select all elements bar and eee */ 
// print_r($foo->xpath('//bar | //eee')); 

/* Equivalent of /child::AAA */ 
// print_r($foo->xpath('//child::aaa/bbb')); 

/* Select all elements given in this absolute path */ 
// print_r($foo->xpath('aaa/bbb/ancestor::*')); 

/* Select ancestors of eee element */ 
// print_r($foo->xpath('//ddd/ancestor::*')); 

/* The following-sibling axis contains all the following siblings of the context node. */ 
// print_r($foo->xpath('/foobar/foo/following-sibling::*')); 

/* The preceding-sibling axis contains all the preceding siblings of the context node */ 
// print_r($foo->xpath('/foobar/aaa/preceding-sibling::*')); 

/* The descendant axis contains the descendants of the context node; 
   a descendant is a child or a child of a child and so on; thus the 
   descendant axis never contains attribute or namespace nodes */ 
// print_r($foo->xpath('aaa/bbb/descendant::*')); 

/* Select elements ddd which have ccc among its ancestors */ 
// print_r($foo->xpath('//ccc/descendant::ddd')); 

/* The following axis contains all nodes in the same document as the context node that 
   are after the context node in document order, excluding any descendants and excluding 
   attribute nodes and namespace nodes. */ 
// print_r($foo->xpath('/foobar/foo/following::*')); 

/* The preceding axis contains all nodes in the same document as the context node that 
   are before the context node in document order, excluding any ancestors and 
   excluding attribute nodes and namespace nodes */ 
// print_r($foo->xpath('aaa/bbb/preceding::*')); 

/* Select even bar elements */ 
// print_r($foo->xpath('//bar[position() mod 2 = 0]')); 

/* Select middle bar element(s) */ 
// print_r($foo->xpath('//bar[position() = floor(last() div 2 + 0.5)]')); 

?>