You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XSLT按Channel顺序提取XML Rate值并补全缺失项为NA的实现需求

问题与解决方案

输入XML

<Rates>
    <Rate>
        <Channel>1</Channel>
        <Type>Life</Type>
        <GRates>
            <GRate>
                <band>&lt;25</band>
                <amount>0.04</amount>
            </GRate>
        </GRates>
    </Rate>
    <Rate>
        <Channel>2</Channel>
        <Type>Life</Type>
        <GRates>
            <GRate>
                <band>&lt;25</band>
                <amount>0.12</amount>
            </GRate>
        </GRates>
    </Rate>
    <Rate>
        <Channel>1</Channel>
        <Type>Add</Type>
        <GRates>
            <GRate>
                <band>&lt;25</band>
                <amount>0.08</amount>
            </GRate>
        </GRates>
    </Rate>
</Rates>

提取要求

  • 按指定顺序提取Rate的amount值:先Channel 1的Life类型、Channel 1的Add类型,再Channel 2的Life类型、Channel 2的Add类型
  • 若某Channel下对应Type缺失,输出NA

用户现有XSLT代码

<xsl:template match="/Rates">
    <xsl:for-each-group select="Rate[Channel=('1', '2')][Type=('Life', 'Add')]/GRates/GRate" group-by="band">
        <row>
            <entry align="center" colsep="1">
                <xsl:value-of select="band"/>
            </entry>
            <xsl:for-each select="current-group()">
                <entry>
                    <p>
                        <xsl:value-of select="format-number(amount, '$0.00')"/>
                    </p>
                </entry>
            </xsl:for-each>
        </row>
    </xsl:for-each-group>
</xsl:template>

当前输出

<row>
    <entry align="center" colsep="1">&lt;25</entry>
    <entry>
        <p>$0.04</p>
    </entry>
    <entry>
        <p>$0.12</p>
    </entry>
    <entry>
        <p>$0.08</p>
    </entry>
</row>

预期输出

<row>
    <entry align="center" colsep="1">&lt;25</entry>
    <entry>
        <p>$0.04</p>
    </entry>
    <entry>
        <p>$0.08</p>
    </entry>
    <entry>
        <p>$0.12</p>
    </entry>
    <entry>
        <p>NA</p>
    </entry>
</row>

优化后的XSLT代码

<xsl:template match="/Rates">
    <!-- 定义固定的输出顺序:Channel+Type的组合 -->
    <xsl:variable name="requiredCombinations" as="element()*">
        <combo channel="1" type="Life"/>
        <combo channel="1" type="Add"/>
        <combo channel="2" type="Life"/>
        <combo channel="2" type="Add"/>
    </xsl:variable>

    <xsl:for-each-group select="Rate/GRates/GRate" group-by="band">
        <row>
            <entry align="center" colsep="1">
                <xsl:value-of select="band"/>
            </entry>
            
            <!-- 遍历每个需要的组合,按顺序输出 -->
            <xsl:for-each select="$requiredCombinations">
                <xsl:variable name="currentChannel" select="@channel"/>
                <xsl:variable name="currentType" select="@type"/>
                <!-- 找到对应Channel和Type的Rate下的当前band的amount -->
                <xsl:variable name="matchingAmount" select="
                    current-group()[../../Channel = $currentChannel and ../../Type = $currentType]/amount
                "/>
                
                <entry>
                    <p>
                        <xsl:choose>
                            <xsl:when test="$matchingAmount">
                                <xsl:value-of select="format-number($matchingAmount, '$0.00')"/>
                            </xsl:when>
                            <xsl:otherwise>NA</xsl:otherwise>
                        </xsl:choose>
                    </p>
                </entry>
            </xsl:for-each>
        </row>
    </xsl:for-each-group>
</xsl:template>

优化说明

  1. 固定输出顺序:通过定义requiredCombinations变量明确指定需要输出的Channel+Type组合顺序,确保输出严格符合要求。
  2. 补全缺失项:遍历每个预设组合时,检查是否存在对应的数据,不存在则输出NA。
  3. 关联数据匹配:通过../../Channel和../../Type向上遍历到Rate节点,匹配当前组合的Channel和Type,确保取到对应band的正确amount值。

内容的提问来源于stack exchange,提问作者User501

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 10:20:59