Php XML域XPath搜索
我需要返回cat 1+其他项的最后一项以下XPath应选择文档中每个类别的最后一项Php XML域XPath搜索,php,xml,rss,domdocument,domxpath,Php,Xml,Rss,Domdocument,Domxpath,我需要返回cat 1+其他项的最后一项以下XPath应选择文档中每个类别的最后一项 /root/channel/item[not(category = following::category)] 你说“我需要返回最后一类物品+其他物品”是什么意思?获取最后一类:我有{cat1,cat1(last),cat2,cat3}需要返回{cat1(last),cat2,cat3}不客气。如果这解决了您的问题,如果您可以单击旁边的复选标记图标,将答案勾选为已接受,我将不胜感激。还向其他人表明,这一问题已经
/root/channel/item[not(category = following::category)]
你说“我需要返回最后一类物品+其他物品”是什么意思?获取最后一类:我有{cat1,cat1(last),cat2,cat3}需要返回{cat1(last),cat2,cat3}不客气。如果这解决了您的问题,如果您可以单击旁边的复选标记图标,将答案勾选为已接受,我将不胜感激。还向其他人表明,这一问题已经得到解决。谢谢
<?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>';
print_r( $RSSContents );
Array
(
[0] => Array
(
[category] => Cat1
)
[1] => Array
(
[category] => Cat1
)
[2] => Array
(
[category] => Cat2
)
[3] => Array
(
[category] => Cat3
)
)
/root/channel/item[not(category = following::category)]