Search code examples
dita

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


When process images in DITA topics, I want to include the book title or the map title. Is that possible? How do I access the book title value in the XSLT context of an image?


Solution

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

    Provided you are using DITA-OT HTML5 transformation for your HTML publishing, you can access map information from topic template by implementing three extension points described in Customizing DITA Open Toolkit.

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

    The sample plug-in structure, named "com.acme.html5.param" and "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>
    

    By adding above plug-ins, you can get following sample result when building 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]

    washingthecar.html