当上下文是主题或图像时,如何获取 DITA 书图中 mainbooktitle 元素的值?

How can I get the value of the mainbooktitle element in a DITA bookmap when the context is a topic or an image?

在处理 DITA 主题中的图像时,我想包含书名或地图标题。那可能吗?如何在图像的 XSLT 上下文中访问书名值?

How do I access the book title value in the XSLT context of an image?

假设您正在为您的 HTML 发布使用 DITA-OT HTML5 转换,您可以通过实施 Customizing DITA Open Toolkit 中描述的三个扩展点从主题模板访问地图信息。

  • dita.conductor.html5.param
  • dita.conductor.html5.toc.param
  • dita.xsl.html5

示例插件结构,命名为"com.acme.html5.param"和"com.acme.html5":

dita-ot-3.3/plugins
   |
   +-- com.acme.html5.param
   |     |
   |     +-- plugin.xml
   |         buildHtml5Param.xml
   |
   +-- com.acme.html5
         |
         +-- plugin.xml
         |
         +--xsl
             |
             +-- dita2html5_acme_custom.xsl

[com.acme.html5.param/plugin.xml]

<?xml version="1.0" encoding="UTF-8"?>
<plugin id="com.acme.extend.html5.param">
    <feature extension="dita.conductor.html5.param" file="buildHtml5Param.xml"/>
    <feature extension="dita.conductor.html5.toc.param" file="buildHtml5Param.xml"/>
</plugin>

[com.acme.html5.param/buildHtml5Param.xml]

<?xml version="1.0" encoding="UTF-8"?>
<params xmlns:if="ant:if">
    <param name="input.map.url" expression="${html5.map.url}" if:set="html5.map.url"/>
</params>

[com.acme.html5/plugin.xml]

<?xml version="1.0" encoding="UTF-8"?>
<plugin id="com.acme.html5">
    <feature extension="dita.xsl.html5" file="xsl/dita2html5_acme_custom.xsl"/>
</plugin>

[com.acme.html5/xsl/dita2html5_acme_custom.xsl]

<?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"
    xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
    exclude-result-prefixes="xs dita-ot"
    version="2.0">

    <!-- map URL -->
    <xsl:param name="input.map.url" as="xs:anyURI" required="yes"/>

    <!-- map document -->
    <xsl:variable name="mapDoc" as="document-node()" select="doc($input.map.url)"/>

    <!-- map title -->
    <xsl:variable name="title" as="xs:string">
        <xsl:variable name="titleTexts" as="xs:string*">
            <xsl:choose>
                <xsl:when test="$mapDoc/*[contains(@class,' bookmap/bookmap ')]">
                    <xsl:apply-templates select="$mapDoc/*[contains(@class, ' bookmap/bookmap ')]/*[contains(@class, ' bookmap/booktitle ')]/*[contains(@class, ' bookmap/mainbooktitle ')]" mode="dita-ot:text-only"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates select="$mapDoc/*[contains(@class, ' map/map ')]/*[contains(@class, ' topic/title ')]" mode="dita-ot:text-only"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:sequence select="string-join($titleTexts,'')"/>
    </xsl:variable>

    <!-- override image template -->
    <xsl:template match="*[contains(@class, ' topic/image ')]" name="topic.image">
        <!-- snip! -->
        ...
        <!-- customization here! -->
        <div>Map: <xsl:value-of select="$title"/></div>
    </xsl:template>

</xsl:stylesheet>

通过添加以上插件,构建时可以得到如下样例结果dita-ot-3.3/docsrc/samples/sequence.ditamap

[dita-ot-3.3/docsrc/samples/sequence.ditamap]

<map>
  <title>Working in the garage</title>
  <topicref href="tasks/changingtheoil.xml" type="task"/>
  ...
  <topicref href="tasks/washingthecar.xml" type="task"/>
  ...
</map>

[dita-ot-3.3/docsrc/samples/tasks/washingthecar.xml]