You need to enable JavaScript to run this app. DITA-OT PDF转换:如何将指定主题页脚页码替换为标题缩写
优惠活动
大模型
产品
解决方案
定价
更多

DITA-OT PDF转换:如何将指定主题页脚页码替换为标题缩写

实现方案

步骤1:修改commons.xsl的topic匹配模板,标记特殊主题

先判断当前根级topic的title是否带CD_Section_Heading的outputclass标识,同时计算标题缩写,作为参数传给后续页脚渲染逻辑,修改后代码如下:

<xsl:template match="*[contains(@class, ' topic/topic ')]">
  <xsl:when test="not(ancestor::*[contains(@class, ' topic/topic ')]) and not(ancestor::ot-placeholder:glossarylist)">
    <fo:page-sequence master-reference="{$page-sequence-reference}" xsl:use-attribute-sets="__force__page__count" initial-page-number="1">
      <!-- 新增特殊主题判断逻辑 -->
      <xsl:variable name="current-title" select="*[contains(@class, ' topic/title ')]/normalize-space(text())" />
      <xsl:variable name="is-special-topic" select="exists(*[contains(@class, ' topic/title ')]/@outputclass[. = 'CD_Section_Heading'])" />
      <!-- 缩写规则可自定义,若缩写不规律可提前配置映射表 -->
      <xsl:variable name="title-abbr">
        <xsl:choose>
          <!-- 固定映射规则,可按需新增条目 -->
          <xsl:when test="$current-title = 'DEFINITIONS'">DEF</xsl:when>
          <xsl:when test="$current-title = 'TERMS OF USE'">TOU</xsl:when>
          <!-- 也可默认取前3位大写字母,无匹配时生效 -->
          <xsl:otherwise><xsl:value-of select="upper-case(substring($current-title,1,3))" /></xsl:otherwise>
        </xsl:choose>
      </xsl:variable>
      <xsl:call-template name="startPageNumbering">
        <xsl:with-param name="is-special" select="$is-special-topic" />
        <xsl:with-param name="topic-abbr" select="$title-abbr" />
      </xsl:call-template>
    </fo:page-sequence>
  </xsl:when>
</xsl:template>

步骤2:新增startPageNumbering模板,实现页脚差异化渲染

在commons.xsl中新增同名模板,根据传入的参数判断显示缩写还是十进制页码:

<xsl:template name="startPageNumbering">
  <xsl:param name="is-special" select="false()" />
  <xsl:param name="topic-abbr" select="''" />
  <!-- 页脚静态内容定义 -->
  <fo:static-content flow-name="xsl-region-after">
    <fo:block text-align="center" font-size="9pt">
      <xsl:choose>
        <!-- 特殊主题显示标题缩写 -->
        <xsl:when test="$is-special">
          <xsl:value-of select="$topic-abbr" />
        </xsl:when>
        <!-- 普通主题保留原有十进制页码 -->
        <xsl:otherwise>
          <fo:page-number format="1" />
        </xsl:otherwise>
      </xsl:choose>
    </fo:block>
  </fo:static-content>
  <!-- 正文流渲染,不要删除否则页面内容不会显示 -->
  <fo:flow flow-name="xsl-region-body">
    <xsl:apply-imports />
  </fo:flow>
</xsl:template>

后续扩展说明

如果需要新增更多特殊主题,仅需要做两个操作:

  • 给对应topic的title节点加上outputclass="CD_Section_Heading"属性
  • 在步骤1的缩写映射规则中新增标题和对应缩写的匹配条目即可,无需修改核心逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 21:36:09