2011-07-21 132 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 
    ); 
}