我需要使用日期时间排序,并且我只想要该节点的最新内容

发布于 2024-10-31 11:35:26 字数 916 浏览 0 评论 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

请帮助我继续

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神爱温柔 2024-11-07 11:35:26

此转换

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

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"/>
烟织青萝梦 2024-11-07 11:35:26

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文