xslt: select the latest element?

R

ralf321

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
 
R

Richard Tobin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top