36036

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

Question:

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 <strong>entry_date</strong> tag, in this case, <strong>2004-12-15</strong>. I'm using SimpleXML but I'm open to other solutions of course. Cheers.

Answer1:

<strong>I. Here is a simple XSLT 1.0 solution that is closest to using a single XPath expression</strong> (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>

<strong>when this transformation is applied on the provided XML document</strong>:

<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>

<strong>the wanted, correct result is produced</strong>:

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

<strong>II. A more efficient XSLT 1.0 solution:</strong>

<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>

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

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

Answer2:

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: <a href="http://www.php.net/manual/en/simplexmlelement.xpath.php" rel="nofollow">http://www.php.net/manual/en/simplexmlelement.xpath.php</a>

$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.

Answer3:

$result = $xml->xpath('//entry_date'); usort($result,'strcmp'); $maxdate = end($result);

Recommend

  • Automating exslt:node-set?
  • finding maximum depth of chapter
  • How to implement xsl
  • Merging two XPathDocuments using XmlCompiledTransform
  • How do I retrieve the text in a table column using Selenium RC?
  • Duplicate Element x number of times with XSLT
  • Failing to get duration of youtube video using xpath
  • Searching an XML file using PHP [closed]
  • Listen to the 'change' even of an element in the shadow DOM
  • Notepad++ - delete all lines with certain text
  • parsing xml and html page with lxml and requests package in python
  • NetBeans doesn't see style.css [duplicate]
  • how to display   in Mozilla using XSL.
  • UIBarButtonItem's action is not called when in a view with a UIGestureRecognizer
  • C++/CLI Thread synchronization including managed and unmanaged code
  • How to add regEx in angular filter
  • change color of jstree node
  • XSLT foreach repeating nodes to flat
  • using System.Speech.Synthesis with Windows10 universal app (XAML-C#)
  • Simple linked list-C
  • NUnit 3.0 TestCase const custom object arguments
  • Plotting line graph with factors in R
  • How to get current document uri in XSLT?
  • CakePHP ACL tutorial initDB function warnings
  • Adjust width of select element according to selected option's width
  • ViewController With Transparent Background Entering Current ViewController With Push Transition
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Alternative to overridePendingTransition() - Android
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • Bad request using file_get_contents for PUT request in PHP
  • Could not find rake using whenever rails
  • Jquery UI tool tip close icon
  • FFmpeg Conversion Error
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • SSO with signing and signature validation doesn't work
  • Deserializing XML into class C#
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • costura.fody for a dll that references another dll
  • How can i traverse a binary tree from right to left in java?