排序并获得XSLT中的第一条记录

时间:2020-01-29 16:14:38

标签: xml xslt xsd xslt-1.0 xslt-2.0

我想对XML进行排序,并在排序后获得第一条记录。

我的XML如下

 <book>
    <book>
        <auther>bell</auther>
        <lastModifiedDateTime>2019-07-22T09:51:48.000</lastModifiedDateTime>
        <code>999</code>
        <date>2019-07-30T00:00:00.000</date>
    </book>
    <book>
        <auther>bell</auther>
        <lastModifiedDateTime>2019-01-01T09:51:48.000</lastModifiedDateTime>
        <code>112</code>
        <date>2020-01-30T00:00:00.000</date>
    </book>
    <book>
        <auther>apple</auther>
        <lastModifiedDateTime>2019-02-02T10:09:40.000</lastModifiedDateTime>
        <code>112</code>
        <date>2018-07-10T00:00:00.000</date>
    </book>
    <book>
        <auther>google</auther>
        <lastModifiedDateTime>2020-01-29T09:51:48.000</lastModifiedDateTime>
        <code>999</code>
        <date>2019-07-30T00:00:00.000</date>
    </book>
</book>

我想通过对代码进行升序排序,对日期进行升序对xml进行排序,并在“代码”和“日期”相同的情况下获取最后一条记录(最大的lastModifiedDateTime)

这是我的预期结果

   <book>
        <book>
            <auther>apple</auther>
            <lastModifiedDateTime>2019-02-02T10:09:40.000</lastModifiedDateTime>
            <code>112</code>
            <date>2018-07-10T00:00:00.000</date>
        </book>
        <book>
            <auther>bell</auther>
            <lastModifiedDateTime>2019-01-01T09:51:48.000</lastModifiedDateTime>
            <code>112</code>
            <date>2020-01-30T00:00:00.000</date>
        </book>
        <book>
            <auther>google</auther>
            <lastModifiedDateTime>2020-01-29T09:51:48.000</lastModifiedDateTime>
            <code>999</code>
            <date>2019-07-30T00:00:00.000</date>
        </book>
    </book>

我尝试了此代码

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="xml" indent="yes" />
    <xsl:template match="book">
        <xsl:copy>
            <xsl:for-each select = "book">
                <xsl:sort select='code' order="ascending"  data-type="number"/>
                <xsl:sort select='date' order="ascending" />
                <xsl:sort select='lastModifiedDateTime' order="descending" />
                <xsl:if test="position()=1">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

但是我只能得到一张记录。

这是实际记录。

<book>
   <book>
       <auther>apple</auther>
       <lastModifiedDateTime>2019-02-02T10:09:40.000</lastModifiedDateTime>
        <code>112</code>
        <date>2018-07-10T00:00:00.000</date>
    </book>
</book> 

1 个答案:

答案 0 :(得分:1)

如果我正确理解(大IF!),您想按codedate对记录进行分组,并在每个组。可以通过以下方式实现:

XSLT 2.0

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="/book">
    <xsl:copy>
        <xsl:for-each-group select="book" group-by="string-join((code, date), '|')">
            <xsl:for-each select="current-group()">
                <xsl:sort select='lastModifiedDateTime' order="descending" />
                <xsl:if test="position()=1">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

如果您希望结果按codedate排序,则对组进行排序:

<xsl:template match="/book">
    <xsl:copy>
        <xsl:for-each-group select="book" group-by="string-join((code, date), '|')">
            <xsl:sort select='code' order="ascending"  data-type="number"/>
            <xsl:sort select='date' order="ascending" />
            <xsl:for-each select="current-group()">
                <xsl:sort select='lastModifiedDateTime' order="descending" />
                <xsl:if test="position()=1">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>

演示:https://xsltfiddle.liberty-development.net/ncnu9B9

相关问题