[xsl] Re: Date Parsing (Was: Document() question)

Subject: [xsl] Re: Date Parsing (Was: Document() question)
From: Peter Davis <pdavis152@xxxxxxxxx> (by way of Peter Davis <pdavis152@xxxxxxxxx>)
Date: Tue, 11 Jun 2002 23:58:50 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 11 June 2002 22:33, Jarkko.Moilanen@xxxxxx wrote:
> I need to find the latest document from file TopicIndex.xml and insert the
> values to 000000001001.xml file. Values that I need are: SignStatus,
> DocType, Time, Sender, Subject. The result is shown in html format.

So the hard part is really just finding the "latest" <Information> element. 
Fortunately, you're date and time formats are easily parseable, and more
importantly, the parts of the date and time (1) begin with the most
significant digits, and (2) are padded with 0's for numbers less than 10. 
Those qualities mean that you can use <xsl:sort> to sort the date and time
alphabetically, and then select the latest one from the sorted list.

Once you sort the list of <Information> elements, you have to choose the last 
one from the list.  The easiest way to do that is with the position() and 
last() functions, which will let you test the position of each <Information> 
element in the sorted list.  Finally, once you get the desired <Information> 
element, you just copy the necessary child elements to the result tree.

<xsl:template match="Document">
  <Result>
    <xsl:apply-templates select="Information">
      <!-- this stage is simple because your date and time formats are already 
        sortable; other formats might require additional processing, since
        XPath does not have a build in date parser/sorter -->
      <xsl:sort select="Time"/>
      <!-- sort by Time (date) first, and then by the clock if two dates are 
        the same -->
      <xsl:sort select="Clock"/>
    </xsl:apply-templates>
  </Result>
</xsl:template>

<xsl:template match="Information">
  <!-- if the current element is the last element in the sorted list -->
  <xsl:if test="position() = last()">
    <xsl:copy-of select="SignStatus"/>
    <xsl:copy-of select="DocType"/>
    <xsl:copy-of select="Time"/>
    <xsl:copy-of select="Sender"/>
    <xsl:copy-of select="Subject"/>
  </xsl:if>
</xsl:template>

HTH

- --
Peter Davis
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9BvEqNSZCJx7tYycRAkVRAKCzbd8iuy1XyLDspP5azvyUTUg8fgCdGJGB
m1MKktmxDH86UycTCk2hnpk=
=PPzz
-----END PGP SIGNATURE-----


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread