在 XSLT 中排序并获取第一条记录

Posted

技术标签:

【中文标题】在 XSLT 中排序并获取第一条记录【英文标题】:Sort and get first record in XSLT 【发布时间】:2020-05-15 04:43:03 【问题描述】:

我想对 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>

如果“代码”和“日期”相同,我想通过代码升序、日期升序和最后一条记录(最大的 lastModifiedDateTime)对 xml 进行排序

这是我的预期结果

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

如果我理解正确(大 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

【讨论】:

以上是关于在 XSLT 中排序并获取第一条记录的主要内容,如果未能解决你的问题,请参考以下文章

sql中筛选第一条记录分组排序

oracle 查询数据只要排序后的第一条记录(不用rownum=1),sql语句怎么写

oracle 取第一条或者最后一条记录

oracle 查询数据只要排序后的第一条记录(不用rownum=1),sql语句怎么写

如何从 sql 查询中获取第一条和最后一条记录?

mysql中如何查询表的第一条和最后一条记录