I need to sort using datetime and i want only the latest contents of that node
<Note NoteText="TestOrder">
<test City="abc" Address="abc"
Response="abc" Devicename="abc"
DateTime="2011-02-07T07:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder1">
<test City="abc" Address="abc"
Response="abc" Devicename="abc"
DateTime="2011-02-08T07:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder2">
<test City="abc" Address="abc"
Response="abc" Devicename="abc"
DateTime="开发者_JAVA百科2011-02-09T18:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder3">
<test City="abc" Address="abc"
Response="abc" Devicename="abc"
DateTime="2011-02-10T17:02:53+00:00"/>
</Note>
Here first i want to find the latest Datetime from Datetime attribute and i want to transfer those latest attributes to another attribute using xslt.
city-C1
Address-A1
Response-R1
Devicename-D1
Datetime-DT
Please help me to proceed
This transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:variable name="vLatest" select=
"max(/*/*/test/@DateTime/xs:dateTime(.))"/>
<xsl:template match=
"test[xs:dateTime(@DateTime) eq $vLatest]">
<maxTest>
<xsl:copy-of select="@*"/>
</maxTest>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<notes>
<Note NoteText="TestOrder">
<test City="abc" Address="abc" Response="abc" Devicename="abc"
DateTime="2011-02-07T07:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder1">
<test City="abc" Address="abc" Response="abc" Devicename="abc"
DateTime="2011-02-08T07:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder2">
<test City="abc" Address="abc" Response="abc" Devicename="abc"
DateTime="2011-02-09T18:02:53+00:00"/>
</Note>
<Note NoteText="TestOrder3">
<test City="abc" Address="abc" Response="abc" Devicename="abc"
DateTime="2011-02-10T17:02:53+00:00"/>
</Note>
</notes>
produces the wanted, correct result:
<maxTest xmlns:xs="http://www.w3.org/2001/XMLSchema" City="abc" Address="abc"
Response="abc"
Devicename="abc"
DateTime="2011-02-10T17:02:53+00:00"/>
XSLT 1.0 stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="vMax">
<xsl:for-each select="/*/Note/test/@DateTime">
<xsl:sort order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="generate-id(..)"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:template match="test">
<xsl:if test="generate-id()=$vMax">
<test C1="{@City}"
A1="{@Address}"
R1="{@Response}"
D1="{@Devicename}"
DT="{@DateTime}"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<test C1="abc" A1="abc" R1="abc" D1="abc" DT="2011-02-10T17:02:53+00:00" />
Note: It works as long as there is no different time zone.
精彩评论