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

XSLT按line数量分组拆分Message节点并保留父元素实现问题

解决方法

你的现有代码仅输出了分组后的line元素,没有复制原Message下的Control节点、以及Body节点下的非line头部元素,调整后的完整XSLT如下:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="Message">
        <!-- 提取公共复用的Control和Body头部内容 -->
        <xsl:variable name="control-node" select="./Control"/>
        <xsl:variable name="body-headers" select="./Body/*[not(self::line)]"/>
        <xsl:for-each-group select="Body/line" group-adjacent="(position() - 1) idiv 2">
            <Message>
                <xsl:copy-of select="$control-node"/>
                <Body>
                    <xsl:copy-of select="$body-headers"/>
                    <xsl:copy-of select="current-group()"/>
                </Body>
            </Message>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

如果需要调整每个Message包含的line数量,只需要修改group-adjacent="(position() - 1) idiv 2"中的数值2为目标数值即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.02 02:45:02