<?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>我有这个xml,我如何在不重复的情况下获得一个项目的最后一个类别?我试着:
<?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
)
)我需要退还cat 1+其他物品中的最后一个
发布于 2011-05-16 23:24:11
下面的XPath应该选择文档中每个类别的最后一项
/root/channel/item[not(category = following::category)]https://stackoverflow.com/questions/6019306
复制相似问题