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

XSL 3.0同时使用两种编号集插入不同类型脚注的问题

解决XSLT中同时添加两种不同编号脚注的问题

看起来你遇到的核心问题是重复生成整个文档以及第二种脚注未生效,这是因为你之前的代码把整个文档用两个独立模式分别处理,然后将两个完整的文档副本拼接输出了。下面我来拆解问题并给出修复方案:

问题根源分析

  1. 你创建了$footnotes和$appnotes两个变量,每个变量都通过应用不同模式生成了一份完整的XML文档副本。最后在根模板里把这两个副本拼接输出,自然会导致整个文档重复。
  2. 两种脚注的处理是完全独立的,没有在同一个文档流程中同时添加,所以看起来第二种脚注没生效——实际上它生成在了另一份文档副本里,只是和第一份重复了。

修复后的XSLT代码

我们可以直接在默认处理流程中同时处理两种脚注,不需要分开生成文档副本:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="3.0">
    <!-- 默认模式:浅拷贝所有未匹配的元素,保留原结构 -->
    <xsl:mode on-no-match="shallow-copy"/>

    <!-- 给seg[@type='foo']添加数字脚注 -->
    <xsl:template match="seg[@type='foo']">
        <xsl:next-match/> <!-- 保留原seg元素的内容 -->
        <footnote/>       <!-- 插入数字脚注占位符 -->
    </xsl:template>

    <!-- 给note元素添加数字脚注 -->
    <xsl:template match="note">
        <xsl:next-match/>
        <footnote/>
    </xsl:template>

    <!-- 给note2元素添加字母脚注 -->
    <xsl:template match="note2">
        <xsl:next-match/>
        <appnote/>        <!-- 插入字母脚注占位符 -->
    </xsl:template>

    <!-- 为footnote生成数字编号:每个deposition内从1开始计数 -->
    <xsl:template match="footnote">
        <xsl:copy>
            <xsl:number level="any" format="1" from="deposition" count="footnote"/>
        </xsl:copy>
    </xsl:template>

    <!-- 为appnote生成字母编号:每个deposition内从a开始计数 -->
    <xsl:template match="appnote">
        <xsl:copy>
            <xsl:number level="any" format="a" from="deposition" count="appnote"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

关键修改说明

  • 移除独立模式和重复文档变量:直接在默认模式下处理所有需要加脚注的元素,确保整个文档只被处理一次,避免重复。
  • 明确脚注计数规则:使用count属性指定只对对应类型的脚注元素计数(count="footnote"和count="appnote"),避免两种脚注的编号互相干扰。
  • 可选调整编号范围:如果不需要每个<deposition>内重新编号,而是整个<corpus>连续编号,只需要去掉from="deposition"属性即可。

预期输出效果

以你提供的示例XML为例,第一个<deposition>中的输出片段会变成:

<seg type="not_foo">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus<note2>another note 2</note2><appnote>a</appnote> ultrices consequat facilisis. Suspendisse a odio<note>foo note</note><footnote>1</footnote> in lobortis. Aenean non dui scelerisque, rutrum est at, cursus sem.</seg>
<seg type="foo">Ut pharetra bibendum ipsum, portitor velit pharetra quis. Aeneano<note>foo note</note><footnote>2</footnote> purus. Praesent aliquam viverra tellus<note>another note</note><footnote>3</footnote> in condimentum.</seg><footnote>4</footnote>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:30:49