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

如何通过DITA OT EPUB插件修改EPUB输出中的XHTML主题文件名

嘿,我来帮你搞定通过DITA OT EPUB插件修改EPUB输出主题文件名的事儿!结合你给的ditamap、topic文件和现有XSL模板,咱们直接上手改:

核心修改思路

要自定义EPUB输出里的主题文件名,关键是替换现有XSL模板中生成$topicResultUri的逻辑——原来的代码用htmlutil:getTopicResultUrl()生成默认路径,咱们改成从topic的outputclass或者navtitle提取自定义名称就行。

具体修改步骤

1. 调整topicref处理的XSL模板

找到你提供的第二个模板(匹配*[df:isTopicRef(.)]的generate-content模式),替换掉生成$topicResultUri的代码行:

方案1:用topic的outputclass作为文件名

原来的代码行:

<xsl:variable name="topicResultUri" select="htmlutil:getTopicResultUrl($outdir, root($topic), $rootMapDocUrl, $doDebug)" as="xs:string" />

替换成以下逻辑(直接用topic的outputclass属性,同时保证路径正确):

<!-- 从当前topic的outputclass提取自定义文件名 -->
<xsl:variable name="customFileName" select="$topic/@outputclass" as="xs:string"/>
<!-- 构建完整输出路径,保持原有outdir目录结构 -->
<xsl:variable name="topicResultUri" select="concat($outdir, '/topics/', $customFileName, '.html')" as="xs:string" />

用你给的示例测试的话,topic_1会输出topics/Ram-Files-RajRFR.html,topic_2会输出topics/Files-SampletitleFST.html。

方案2:用topicref的navtitle作为文件名

如果想从导航标题提取,记得先清理特殊字符(比如括号、空格)避免生成无效路径:

<!-- 先清理navtitle里的特殊字符,只保留字母、数字、下划线和连字符 -->
<xsl:variable name="cleanNavTitle" select="replace(./topicmeta/navtitle, '[^a-zA-Z0-9-_]', '')" as="xs:string"/>
<!-- 构建完整输出路径 -->
<xsl:variable name="topicResultUri" select="concat($outdir, '/topics/', $cleanNavTitle, '.html')" as="xs:string" />

这样示例里的Ram-Files-Raj (RFR)会变成Ram-Files-RajRFR,对应的文件名就是Ram-Files-RajRFR.html。

2. 完整修改后的模板片段

把修改后的逻辑嵌入你原有的模板,确保代码连贯:

<xsl:template match="*[df:isTopicRef(.)]" mode="generate-content">
  <xsl:param name="doDebug" as="xs:boolean" tunnel="yes" select="false()"/>
  <xsl:param name="rootMapDocUrl" as="xs:string" tunnel="yes"/>
  <!-- <xsl:variable name="doDebug" as="xs:boolean" select="true()"/>-->
  <xsl:if test="$doDebug">
    <xsl:message> + [DEBUG] Handling topicref to "<xsl:sequence select="string(@href)"/>" in mode generate-content</xsl:message>
  </xsl:if>
  <xsl:variable name="topic" select="df:resolveTopicRef(.)" as="element()*"/>
  <xsl:choose>
    <xsl:when test="not($topic)">
      <xsl:message> + [WARNING] generate-content: Failed to resolve topic reference to href "<xsl:sequence select="string(@href)"/>"</xsl:message>
    </xsl:when>
    <xsl:otherwise>
      <!-- 这里用方案1的outputclass逻辑,你可以换成方案2的navtitle逻辑 -->
      <xsl:variable name="customFileName" select="$topic/@outputclass" as="xs:string"/>
      <xsl:variable name="topicResultUri" select="concat($outdir, '/topics/', $customFileName, '.html')" as="xs:string" />
      
      <!-- 原有href修复逻辑保持不变 -->
      <xsl:variable name="tempTopic" as="document-node()">
        <xsl:apply-templates select="$topic" mode="href-fixup">
          <xsl:with-param name="topicResultUri" select="$topicResultUri" tunnel="yes"/>
          <xsl:with-param name="topicref" as="element()" select="." tunnel="yes"/>
        </xsl:apply-templates>
      </xsl:variable>
      <!-- 后续原有处理逻辑继续保留 -->
      ...
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

3. 加个容错 fallback(可选)

如果有些topic没有outputclass或者navtitle,可以加个 fallback 逻辑避免报错,比如用原来的默认文件名生成方式:

<xsl:variable name="topicResultUri" as="xs:string">
  <xsl:choose>
    <xsl:when test="$topic/@outputclass">
      <xsl:value-of select="concat($outdir, '/topics/', $topic/@outputclass, '.html')"/>
    </xsl:when>
    <xsl:otherwise>
      <!-- 用默认逻辑生成文件名 -->
      <xsl:value-of select="htmlutil:getTopicResultUrl($outdir, root($topic), $rootMapDocUrl, $doDebug)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:variable>
测试验证

把修改后的XSL模板放到DITA OT EPUB插件的对应目录(通常是plugins/org.dita.html5/xsl/下的相关文件),然后用你提供的示例ditamap和topic文件生成EPUB,就能看到主题文件名已经变成你自定义的名称啦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 04:22:43