【问题标题】:I need to sort using datetime and i want only the latest contents of that node我需要使用日期时间进行排序,我只想要该节点的最新内容
【发布时间】:2011-04-08 13:32:38
【问题描述】:
<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

请帮我继续

【问题讨论】:

  • 好问题,+1。请参阅我的答案以获得完整、简短且简单的解决方案。
  • 我不完全理解需要什么,下面的两个答案会产生不同的输出,所以我不确定其他人是否真的这样做。

标签: xslt xslt-2.0 xslt-1.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"/>

【讨论】:

    【解决方案2】:

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

    注意:只要没有不同的时区,它就可以工作。

    【讨论】:

      猜你喜欢
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-03
      相关资源
      最近更新 更多