2011-07-21 130 views
0

如何从现有的xml文件(SimpleXml)获取最高的现有guid值。php simplexml获取最大值

XML的结构例如:

<xml blabla> 
<blog name=""> 
    <post> 
    <guid>3</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
    <post> 
    <guid>1</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
</blog> 

我尝试以下($ XML是一种SimpleXML的对象) MAX($ XML->的xpath(/博客/后/ GUID)); 但这似乎是没有阵列...

有什么建议吗?有没有办法处理每个xpath?我的谷歌搜索不成功。

+1

'MAX()'是你应该使用功能** **内的XPath表达式 – slhck

+1

可能的[基于名称的简单XML排序]重复(http://stackoverflow.com/questions/4338699/simple-xml-sort-based-on-name) – Gordon

+1

@slhck PHP的libxml不支持XPath 2.0 – Gordon

回答

1

你可以使用array_map( 'INTVAL' ......与一些它 “理解” 喂MAX()。

<?php 
$xml = getDoc(); 
$ns = $xml->xpath('//blog/post/guid'); 
$ns = array_map('intval', $ns); 
echo max($ns); 

function getDoc() { 
    return new SimpleXMLElement(<<< eox 
<xml> 
<blog name=""> 
    <post> 
    <guid>3</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
    <post> 
    <guid>1</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
    <post> 
    <guid>99</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
    <post> 
    <guid>47</guid> 
    <author></author> 
    <content>...</content> 
    </post> 
</blog> 
</xml> 
eox 
    ); 
}