XML DOMXPath Searching
<?xml version="1.0" encoding="UTF-8"?>
<root>
<channel>
<item>
<category>Cat1</category>
</item>
<item>
<category>Cat1</category>
</item>
<item>
<category>Cat2</category>
</item>
<item>
<category>Cat3</category>
</item>
</channel>
</root>
i have this xml, how i get last category of one item without repeat ? i trying:
<?php
$DOMDocument = new DOMDocument( '1.0', 'utf-8' );
$DOMDocument->preserveWhiteSpace = false;
$DOMDocument->load( 'xml.xml' );
$DOMXPath = new DOMXPath( $DOMDocument );
foreach( $DOMXPath->query('.//channel/item/category[last()]/parent::node()') as $Nodes ){
foreach( $Nodes->childNodes as $Node ){
$RSS[ $Node->nodeName ] = $Node->nodeValue;
}
$RSSContents[] = $RSS;
}
echo '<pre>';
pr开发者_StackOverflowint_r( $RSSContents );
But retorning:
Array
(
[0] => Array
(
[category] => Cat1
)
[1] => Array
(
[category] => Cat1
)
[2] => Array
(
[category] => Cat2
)
[3] => Array
(
[category] => Cat3
)
)
i need to return last of cat 1 + other items
The following XPath should select the last item of each category in the document
/root/channel/item[not(category = following::category)]
精彩评论