XSLT 3.0 转型

问题描述 投票:0回答:1

尝试在 xslt 3.0 中转换 xml 并按值和元素对节点进行分组

嘿伙计们,我有一个输入 xml 如下

<?xml version='1.0' encoding='UTF-8'?>
<Applications>
    <Application>
        <applicationId>280</applicationId>
        <cust_IDCheckIDCollation>
            <Option>
                <id>197249</id>
            </Option>
        </cust_IDCheckIDCollation>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>292</applicationId>
        <cust_IDCheckIDCollation>
            <Option>
                <id>197249</id>
            </Option>
        </cust_IDCheckIDCollation>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>280</applicationId>
        <cust_OnlineReferenceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_OnlineReferenceCheck>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>292</applicationId>
        <cust_OnlineReferenceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_OnlineReferenceCheck>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>280</applicationId>
        <cust_AustralianWorkRights>
            <Option>
                <id>197250</id>
            </Option>
        </cust_AustralianWorkRights>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>292</applicationId>
        <cust_AustralianWorkRights>
            <Option>
                <id>197250</id>
            </Option>
        </cust_AustralianWorkRights>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>280</applicationId>
        <cust_NationalPoliceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_NationalPoliceCheck>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
    <Application>
        <applicationId>292</applicationId>
        <cust_NationalPoliceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_NationalPoliceCheck>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
    </Application>
</Applications>

我需要按 applicationId 将其分组,然后按元素名称对其进行子分组以删除重复元素。

预期产出

<Applications>
    <Application>
        <applicationId>280</applicationId>
        <cust_IDCheckIDCollation>
            <Option>
                <id>197249</id>
            </Option>
        </cust_IDCheckIDCollation>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
        <cust_OnlineReferenceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_OnlineReferenceCheck>
        <cust_AustralianWorkRights>
            <Option>
                <id>197250</id>
            </Option>
        </cust_AustralianWorkRights>
        <cust_NationalPoliceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_NationalPoliceCheck>
    </Application>
    <Application>
        <applicationId>292</applicationId>
        <cust_IDCheckIDCollation>
            <Option>
                <id>197249</id>
            </Option>
        </cust_IDCheckIDCollation>
        <cust_overallduedilligencestatus>
            <Option>
                <id>197276</id>
            </Option>
        </cust_overallduedilligencestatus>
        <cust_OnlineReferenceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_OnlineReferenceCheck>
        <cust_AustralianWorkRights>
            <Option>
                <id>197250</id>
            </Option>
        </cust_AustralianWorkRights>
        <cust_NationalPoliceCheck>
            <Option>
                <id>197249</id>
            </Option>
        </cust_NationalPoliceCheck>
    </Application>
    <Applications>

在 xslt 3.0 中是否有更简单的方法来做到这一点。

我在 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="/Applications">
        <xsl:copy>
            <xsl:for-each-group select="Application" group-by="applicationId">
                <xsl:copy>
                    <xsl:for-each-group select="current-group()/*" group-by="name()">
                        <xsl:element name="{current-grouping-key()}">
                            <xsl:value-of select="(current-group())[1]"/>
                        </xsl:element>
                    </xsl:for-each-group>
                </xsl:copy>
            </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>```
xslt xslt-2.0 xslt-3.0 xslt-grouping
1个回答
0
投票

我想你想做:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/Applications">
    <xsl:copy>
        <xsl:for-each-group select="Application" group-by="applicationId">
            <xsl:copy>
                <xsl:copy-of select="applicationId"/>
                <xsl:for-each-group select="current-group()/(* except applicationId)" group-by="name()">
                    <xsl:copy-of select="current-group()[1]"/>
                </xsl:for-each-group>
            </xsl:copy>
        </xsl:for-each-group>
    </xsl:copy>
</xsl:template>
    
</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.