【问题标题】:Numbered bookmarks in PDF output from DITA bookmaps with and without partsDITA 书籍地图的 PDF 输出中的编号书签(带和不带部件)
【发布时间】:2018-04-16 17:50:35
【问题描述】:

我正在尝试使用 DITA OT 和自定义插件创建带有编号标题的 PDF 输出。默认情况下,输出在标题和目录中包含部件编号、章节编号和附录编号,但在书签中没有编号。到目前为止,我已经设法对标题和目录中的所有剩余主题进行编号,如下所示(章节编号在每个部分重新开始):

  • 图书地图
    • 第一部分
      • 第1章
        • 主题 1.1
        • 主题 1.2
      • 第2章
    • 第二部分
      • 第一章

但是,我无法获得相同的书签编号。

我正在使用以下代码(或覆盖)来选择必须编号的书签:

    <xsl:template match="*[contains(@class, ' topic/topic ')]" mode="bookmark">
    <xsl:variable name="mapTopicref" select="key('map-id', @id)[1]"/>
    <xsl:variable name="topicTitle">
        <xsl:call-template name="getNavTitle"/>
    </xsl:variable>

    <xsl:choose>
        <xsl:when test="$mapTopicref[@toc = 'yes' or not(@toc)] or
            not($mapTopicref)">
            <fo:bookmark>
                <xsl:attribute name="internal-destination">
                    <xsl:call-template name="generate-toc-id"/>
                </xsl:attribute>
                <xsl:if test="$bookmarkStyle!='EXPANDED'">
                    <xsl:attribute name="starting-state">hide</xsl:attribute>
                </xsl:if>
                <fo:bookmark-title>
                    <xsl:choose>
                        <xsl:when test="contains($mapTopicref/@class, ' bookmap/part ')">
                            <xsl:call-template name="getChapterPrefix"/>
                            <xsl:text> </xsl:text>
                        </xsl:when>
                        <xsl:when test="contains($mapTopicref/@class, ' bookmap/appendix ')">
                            <xsl:call-template name="getChapterPrefix"/>
                            <xsl:text> </xsl:text>
                        </xsl:when>
                        <xsl:when test="contains($mapTopicref/@class, ' bookmap/chapter ')">
                            <xsl:call-template name="getChapterPrefix"/>
                            <xsl:text> </xsl:text>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:value-of select="normalize-space($topicTitle)"/>
                </fo:bookmark-title>
                <xsl:apply-templates mode="bookmark"/>
            </fo:bookmark>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates mode="bookmark"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

我正在使用以下代码来创建数字(源自 DITA for Print 中的示例):

    <xsl:template name="getChapterPrefix">
    <xsl:variable name="topicType">
        <xsl:call-template name="determineTopicType"/>
    </xsl:variable>
    <xsl:variable name="partsCount">
        <xsl:value-of select="count($map//*[contains(@class, ' bookmap/part')])"/>
    </xsl:variable>
    <xsl:variable name="containingChapter" select="ancestor-or-self::*[contains(@class, ' topic/topic')][position()=1]"/>
    <xsl:variable name="id" select="$containingChapter/@id"/>
    <xsl:variable name="topicChapters">
        <xsl:copy-of select="$map//*[contains(@class, ' bookmap/chapter')]"/>
    </xsl:variable>
    <xsl:variable name="topicAppendices">
        <xsl:copy-of select="$map//*[contains(@class, ' bookmap/appendix')]"/>
    </xsl:variable>
    <xsl:variable name="topicParts">
        <xsl:copy-of select="$map//*[contains(@class, ' bookmap/part')]"/>
    </xsl:variable>
    <xsl:variable name="chapterNumber">
        <xsl:choose>
            <xsl:when test="$topicChapters/*[@id = $id]">
                <xsl:choose>
                    <xsl:when test="$partsCount=0"> <!-- Bookmaps without parts work fine -->
                        <xsl:number format="1" value="count($topicChapters/*[@id =$id]/preceding-sibling::*) + 1"/>                           
                    </xsl:when>
                    <xsl:otherwise> <!-- This does not work yet. -->
                        <xsl:number format="1" value="count($topicChapters/*[@id =$id]/preceding-sibling::*) + 1"/>                
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>
            <xsl:when test="$topicAppendices/*[@id = $id]">
                <xsl:number format="A" value="count($topicAppendices/*[@id =$id]/preceding-sibling::*) + 1"/>
            </xsl:when>
            <xsl:when test="$topicParts/*[@id = $id]">
                <xsl:number format="I" value="count($topicParts/*[@id =$id]/preceding-sibling::*) + 1"/>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:variable> 
    <xsl:choose>
        <xsl:when test="$chapterNumber != ''">
            <xsl:value-of select="$chapterNumber"/>
        </xsl:when>
    </xsl:choose>
</xsl:template>

使用此代码,零件、附录和没有零件的书图可以正确编号。但是,对于有部分的书图,章节是连续编号的,这是不一致的。

  • 图书地图
    • 第一部分
      • 第1章
        • 主题 1.1
        • 主题 1.2
      • 第2章
    • 第二部分
      • 第三章

谁能帮我纠正这个问题?

【问题讨论】:

  • 你能提供源XML吗?这样会更容易回答你。
  • 我在gist.github.com/SusanneM有一个公开的要点
  • 最好将相关部分合并到问题中以创建minimal reproducible example,因为链接随时可能消失。
  • 那么当你使用&lt;xsl:copy-of select="$map//*[contains(@class, ' bookmap/chapter')]"/&gt;将变量topicChapters绑定到章节副本时,是不是没有办法保留层次结构呢?您复制的那些元素不是包含在部件的父元素中,因此您应该复制部件或转换它们以包含您感兴趣的章节?显然,在您当前的方法中,您的变量包含一个平面元素列表,当您计算兄弟元素时,您确实会得到所有前面的章节。
  • 是的,你是对的。我找到了一种解决方法(这对我来说看起来不是很优雅,但我可以):当我有一个带有零件的书图时,我只是忽略了 topicChapters 变量。

标签: xslt dita


【解决方案1】:

我刚刚找到了一种方法来获得我想要的结果。计算带有部分的书图的章节编号的代码也被修改如下:

            <!-- If there's something in $topicChapters with an id that matches the id of the
    context node, then I'm inside a chapter. -->
            <xsl:when test="$topicChapters/*[@id = $id]">
                <xsl:choose>
                    <xsl:when test="$partsCount=0"> <!-- Bookmaps without parts -->
                        <xsl:number format="1" value="count($topicChapters/*[@id =$id]/preceding-sibling::*) + 1"/>                           
                    </xsl:when>
                    <xsl:otherwise> <!-- Bookmaps with parts. -->
                        <xsl:number format="1" value="count(//*[contains(@class,' bookmap/chapter ')][@id =$id]/preceding-sibling::*)"/>                
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:when>

这可能不是优雅的,但是,我是一名技术作家......

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多