我需要使用datetime排序,我只想要该节点的最新内容

时间:2011-04-08 13:32:38

标签: xslt xslt-2.0 xslt-1.0

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

首先,我想从Datetime属性中找到最新的Datetime,我想使用xslt将这些最新属性传输到另一个属性。

city-C1
Address-A1
Response-R1
Devicename-D1
Datetime-DT

请帮我继续

2 个答案:

答案 0 :(得分:1)

此转化

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

应用于提供的XML文档时:

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

会产生想要的正确结果:

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

答案 1 :(得分:1)

XSLT 1.0样式表:

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

输出:

<test C1="abc" A1="abc" R1="abc" D1="abc" DT="2011-02-10T17:02:53+00:00" />

注意:只要没有不同的时区,它就能正常工作。