如何使用 XSLT 1.0 过滤和获取具有最新日期的 XML 中的元素 [英] How to filter and fetch elements in XML which has latest date using XSLT 1.0

查看:20
本文介绍了如何使用 XSLT 1.0 过滤和获取具有最新日期的 XML 中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XML 负载,如下所示,其中包含帐户列表.我想选择活动帐户,如果有任何帐户处于活动状态并且正在重复,那么我需要根据该帐户的日期选择最新的帐户.

I have a XML payload as below, which contains list of accounts. I want to select the Active accounts and if any account is active and is repeating, then i need to select the latest account based on the date of that account.

输入有效载荷:

<accounts>
    <account1>
        <name>abc</name>
        <account_no>123</account_no>
        <status>Active</status>
        <date>20-05-2018</date>
    </account1>
    <account2>
        <name>def</name>
        <account_no>123</account_no>
        <status>Active</status>
        <date>20-06-2018</date>
    </account2>
    <account3>
        <name>ghi</name>
        <account_no>1234</account_no>
        <status>Active</status>
        <date>20-05-2018</date>
    </account3>
    <account4>
        <name>jkl</name>
        <account_no>1234</account_no>
        <status>In Active</status>
        <date>20-05-2018</date>
    </account4>
</accounts>

预期结果:

<accounts>
    <account2>
        <name>def</name>
        <account_no>123</account_no>
        <status>Active</status>
        <date>20-06-2018</date>
    </account2>
    <account3>
        <name>ghi</name>
        <account_no>1234</account_no>
        <status>Active</status>
        <date>20-05-2018</date>
    </account3>
</accounts>

请帮我解决这个问题,我尝试使用 for-each 循环,使用它我可以选择选择的活动计划,但在重复节点时无法根据日期选择最新帐户.

Please help me with this, i tried using for-each loop using which im able to select the select active plans but not getting how to select the latest account based on date when there is repeating nodes.

提前致谢.

推荐答案

这需要大量的工作.而且您的数据提供者肯定不会让事情变得更容易,他们选择在帐户元素名称中添加序列号,并选择 DD-MM-YYYY 作为日期格式.

This requires a considerable amount of work. And your data provider is certainly not making it any easier, with their choice to add a serial number to the account element names, as well as selecting DD-MM-YYYY as the date format.

试试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:key name='acc' match="*[starts-with(name(), 'account') and status='Active']" use="account_no"/>

<xsl:template match="/accounts">
    <xsl:copy>
        <!-- GROUP ACTIVE ACCOUNTS BY account_no, USING MUENCHIAN GROUPING -->
        <xsl:for-each select="*[starts-with(name(), 'account') and status='Active'][count(. | key('acc', account_no)[1]) = 1]">
            <!-- SORT CURRENT GROUP BY DATE, DESCENDING -->
            <xsl:for-each select="key('acc', account_no)">
                <!-- SORT BY YEAR -->
                <xsl:sort select="substring(date, 7, 4)" data-type="number" order="descending"/>
                <!-- SORT BY MONTH -->
                <xsl:sort select="substring(date, 4, 2)" data-type="number" order="descending"/>
                <!-- SORT BY DAY -->
                <xsl:sort select="substring(date, 1, 2)" data-type="number" order="descending"/>
                <!-- RETURN THE FIRST RECORD IN THE SORTED GROUP -->
                <xsl:if test="position()=1">
                    <xsl:copy-of select="."/>
                </xsl:if>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet> 

<小时>

要了解 Muenchian 分组,请阅读:http://www.jenitennison.com/xslt/grouping/muenchian.html

这篇关于如何使用 XSLT 1.0 过滤和获取具有最新日期的 XML 中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆