开发者

How to get the highest value of a specific tag in an XML file using SimpleXML?

My XML file looks like this:

<log>
  <entry entry_id="E200911115777">
    <entry_data>
      <entry_title>Lorem ipsum dolor</entry_title>
      <entry_date>1999-04-15</entry_date>
    </entry_data>
  </entry>
  <entry entry_id="E205011115999">
    <entry_data>
      <entry_title>Lorem ipsum dolor</entry_title>
      <entry_date>2004-12-15</entry_date>
    </entry_data>
  </entry>
  <entry entry_id="E199912119116">
    <entry_data>
      <entry_title>Lorem ipsum dolor</entry_title>
      <entry_date>1990-11-20</entry_date>
    </entry_data>
  </entry开发者_开发百科>
</log>

I'm looking for code that will return the highest value of the entry_date tag, in this case, 2004-12-15. I'm using SimpleXML but I'm open to other solutions of course. Cheers.


I. Here is a simple XSLT 1.0 solution that is closest to using a single XPath expression (it isn't possible to have just a single XPath 1.0 expression selecting the wanted node(s) ):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="entry">
  <xsl:copy-of select=
   "self::node()
      [not((preceding-sibling::entry | following-sibling::entry)
             [translate(*/entry_date,'-','')
             >
             translate(current()/*/entry_date,'-','')
             ]
           )
      ]
   "/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the provided XML document:

<log>
    <entry entry_id="E200911115777">
        <entry_data>
            <entry_title>Lorem ipsum dolor</entry_title>
            <entry_date>1999-04-15</entry_date>
        </entry_data>
    </entry>
    <entry entry_id="E205011115999">
        <entry_data>
            <entry_title>Lorem ipsum dolor</entry_title>
            <entry_date>2004-12-15</entry_date>
        </entry_data>
    </entry>
    <entry entry_id="E199912119116">
        <entry_data>
            <entry_title>Lorem ipsum dolor</entry_title>
            <entry_date>1990-11-20</entry_date>
        </entry_data>
    </entry>
</log>

the wanted, correct result is produced:

<entry entry_id="E205011115999">
   <entry_data>
      <entry_title>Lorem ipsum dolor</entry_title>
      <entry_date>2004-12-15</entry_date>
   </entry_data>
</entry>

II. A more efficient XSLT 1.0 solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:apply-templates>
   <xsl:sort order="descending"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="entry">
  <xsl:if test="position() = 1">
   <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the same XML document (above), again the wanted, correct result is produced:

<entry entry_id="E205011115999">
   <entry_data>
      <entry_title>Lorem ipsum dolor</entry_title>
      <entry_date>2004-12-15</entry_date>
   </entry_data>
</entry>


Yeah, should be quite easy with xpath, that is definately the way to go, and simple xml works well with xpath in php.

Check out the docs here: http://www.php.net/manual/en/simplexmlelement.xpath.php

$xml = new SimpleXMLElement($string);

/* Search for <log><entry><entry_data><entry_date> */
$result = $xml->xpath('/log/entry/entry_data/entry_date');

while(list( , $node) = each($result)) {
    $timestamp = strtotime((string) $node));
    echo '/log/entry/entry_data/entry_date: ' . $timestamp ."\n";
}

I didn't actually test that code, but should be pretty close to what you need, and timestamps of course have their limits but seems ok for your use.


$result = $xml->xpath('//entry_date');

usort($result,'strcmp');

$maxdate = end($result);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜