You need to enable JavaScript to run this app. XSLT中创建与使用属性:实现发票XML拆分客户与关联引用
优惠活动
大模型
产品
解决方案
定价
更多

XSLT中创建与使用属性:实现发票XML拆分客户与关联引用

XSLT客户信息拆分与关联实现方案

实现思路

  • 首先定义全局变量存储所有输入的Invoice/Customer节点集合,保证遍历顺序固定
  • 生成<Customers>节点时,直接用遍历客户时的position()作为ref属性值,该值为数字类型,符合schema对long类型的要求
  • 生成<Invoices>节点时,对每个发票对应的Customer节点,查找它在全局客户集合中的索引位置作为Recipient的值,即可完成关联,不需要额外缓存映射关系

完整代码实现

XSLT 1.0 兼容版本

适用于所有支持XSLT 1.0的处理器:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- 全局客户集合,遍历顺序固定 -->
    <xsl:variable name="allCustomers" select="/Invoices/Invoice/Customer" />
    
    <xsl:template match="/">
        <!-- XML要求仅能有一个根节点,若输出规范允许多顶级节点可自行调整 -->
        <root>
            <Customers>
                <xsl:for-each select="$allCustomers">
                    <Customer ref="{position()}">
                        <xsl:attribute name="type">
                            <xsl:choose>
                                <xsl:when test="Type='P'">Person</xsl:when>
                                <xsl:when test="Type='C'">Company</xsl:when>
                            </xsl:choose>
                        </xsl:attribute>
                        <xsl:choose>
                            <xsl:when test="Type='P'">
                                <FirstName><xsl:value-of select="First" /></FirstName>
                                <LastName><xsl:value-of select="Last" /></LastName>
                            </xsl:when>
                            <xsl:when test="Type='C'">
                                <Name><xsl:value-of select="Name" /></Name>
                            </xsl:when>
                        </xsl:choose>
                        <Id><xsl:value-of select="Id" /></Id>
                    </Customer>
                </xsl:for-each>
            </Customers>
            <Invoices>
                <xsl:for-each select="Invoices/Invoice">
                    <Invoice>
                        <!-- 通过generate-id匹配节点,查找对应索引 -->
                        <Recipient>
                            <xsl:value-of select="count($allCustomers[generate-id(.) = generate-id(current()/Customer)][1]/preceding-sibling::Customer) + 1" />
                        </Recipient>
                        <Amount><xsl:value-of select="Amount" /></Amount>
                    </Invoice>
                </xsl:for-each>
            </Invoices>
        </root>
    </xsl:template>
</xsl:stylesheet>

XSLT 2.0+ 精简版本

语法更简洁,执行效率更高:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:variable name="allCustomers" select="/Invoices/Invoice/Customer" />
    
    <xsl:template match="/">
        <root>
            <Customers>
                <xsl:for-each select="$allCustomers">
                    <Customer ref="{position()}" type="{if (Type='P') then 'Person' else 'Company'}">
                        <xsl:choose>
                            <xsl:when test="Type='P'">
                                <FirstName>{First}</FirstName>
                                <LastName>{Last}</LastName>
                            </xsl:when>
                            <xsl:otherwise>
                                <Name>{Name}</Name>
                            </xsl:otherwise>
                        </xsl:choose>
                        <Id>{Id}</Id>
                    </Customer>
                </xsl:for-each>
            </Customers>
            <Invoices>
                <xsl:for-each select="Invoices/Invoice">
                    <Invoice>
                        <!-- 直接用index-of查找节点位置 -->
                        <Recipient>{index-of($allCustomers, Customer)}</Recipient>
                        <Amount>{Amount}</Amount>
                    </Invoice>
                </xsl:for-each>
            </Invoices>
        </root>
    </xsl:template>
</xsl:stylesheet>

注意事项

  • 若输入中存在重复客户(同一个Id出现多次),需要先对客户集合做去重处理:XSLT 1.0使用Muenchian分组,XSLT 2.0+直接用group-by即可,避免生成重复的ref值
  • position()基于当前遍历的节点集合生成,只要全局客户集合的顺序固定,关联的Recipient值就会完全匹配,不需要额外维护映射表

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 09:00:02