Click to See Complete Forum and Search --> : [RESOLVED] domxpath query select a .......


whisher06
02-11-2007, 06:24 PM
Hi.
I've got this simple xml file:

<?xml version="1.0" encoding="utf-8"?>
<items>
<item>
<time>1169838286</time>
<title>Tre</title>
<author>Tre</author>
<content>TRE his module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.
This module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.TRE
</content>
</item>
<item><time>1169838</time>
<title>Tre</title>
<author>Tre</author>
<content>TRE his module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.
This module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.TRE
</content>
</item>
<item>
<time>11698286</time>
<title>Tre</title>
<author>Tre</author>
<content>TRE his module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.
This module defines a number of classes, which are explained in the following tables. Classes with an equivalent in the DOM standard are named DOMxxx.TRE
</content>
</item>
</items>


To get all the item nodes I've
got this code:

$xp = new domxpath($dom);
$titles = $xp->query("/items/item");
foreach ($titles as $node) {
print $node->firstChild->data . "\n";
}


Is there a way
to get only the item with
time=1169838 for instance ?

I'm looking for good tutorials
to using domxpath ;)

Thanks in advance.

Bye.

Weedpacket
02-11-2007, 07:49 PM
Use a predicate that says that the item node's time node's text node has that value (Heh, the XPath is easier to read). /items/item[time/text()='1169838']

Oh, right, tutorials. I don't know any tutorials: I got everything I know about XPath from the source (http://www.w3.org/TR/xpath), but I used XPath Explorer (http://www.purpletech.com/xpe/index.jsp) to check the above code.

dream.scape
02-11-2007, 08:53 PM
W3 Schools has some XPath tutorials:

http://www.w3schools.com/xpath/default.asp

whisher06
02-12-2007, 05:20 AM
Thanks a lot buddies :D

I used XPath Explorer to check the above code

It's very useful !


Bye.