xslt: select the latest element?

Discussion in 'XML' started by ralf321@gmx.de, Dec 9, 2005.

  1. Guest

    hello!

    i have this xml:
    <object>
    <node meta="27" >
    <data meta="13" changed="2005-09-05 16:16:43">a</data>
    <data meta="14" changed="2005-11-06 19:26:49">b</data>
    <data meta="15" changed="2005-09-12 13:58:44">c</data>
    <data meta="16" changed="2005-09-07 16:56:48">d</data>
    </node>

    <node meta="27" >
    <data meta="13" changed="2005-04-05 16:56:45">a</data>
    <data meta="14" changed="2005-11-05 13:44:44">b</data>
    <data meta="15" changed="2005-12-05 12:13:49">c</data>
    <data meta="16" changed="2005-09-05 11:52:43">d</data>
    </node>
    </object>

    I want to select the data which has changed at last from now.
    i have no idea to get this data.

    thanks
    , Dec 9, 2005
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >I want to select the data which has changed at last from now.


    The only way to sort things in XSLT 1 is by using <xsl:sort> in
    <xsl:apply-templates> of <xsl:for-each>. And XSLT 1 has no knowledge
    of date formats.

    Luckily, your date format

    <data meta="13" changed="2005-09-05 16:16:43">a</data>

    has the property that sorting it lexicographically is the same as
    sorting it as a date.

    So you can call apply-templates with the dates in ascending order
    quite easily:

    <xsl:apply-templates select="//data">
    <xsl:sort select="@changed"/>
    </xsl:apply-templates>

    and in a template with match="data" you can see if the element is
    the last one in the (sorted) node list:

    <xsl:if test="position() = last()">
    ...
    </xsl:if>

    -- Richard
    Richard Tobin, Dec 9, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    652
    Stylus Studio
    Aug 3, 2004
  2. Benjamin Hillsley
    Replies:
    3
    Views:
    1,676
    Dimitre Novatchev
    Sep 25, 2003
  3. Gerald Aichholzer
    Replies:
    2
    Views:
    972
    Gerald Aichholzer
    May 29, 2005
  4. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  5. palmiere
    Replies:
    1
    Views:
    396
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page