Search code examples
htmldocbook

Add metadata value only to doc page


I have a need to know which page is the toc page in our html output from our Solr index. I would like to add a metadata entry that identifies the output as the toc. Everything that I have tried puts the value in all pages. We are using docbook 4.5, and I am very new to xslt. Any pointers to the right direction would be a big help.

Thanks!


Solution

  • I don’t think the DocBook XSL stylesheets provide any pretty way to do this, so here’s an ugly hack:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:import
        href="/usr/share/xml/docbook/stylesheet/docbook-xsl/html/chunk.xsl"/>
      <xsl:template name="head.content.generator">
        <xsl:param name="node" select="."/>
        <xsl:if test="not(parent::*)">
          <meta name="TOC" content="This is the TOC"/>
        </xsl:if>
        <meta name="generator" content="DocBook {$DistroTitle} V{$VERSION}"/>
      </xsl:template>
    </xsl:stylesheet>
    

    You’d need to put that in a file named docbook-custom.xsl and call that from your XSLT engine:

    xsltproc docbook-custom.xsl BOOK.xml
    

    Within the docbook-custom.xsl “customization layer” file, you need to set the href value of the xsl:import element to wherever you actually have the DocBook XSL stylesheets installed.

    http://www.sagehill.net/docbookxsl/CustomMethods.html#CustomizationLayer has general details.

    Of course instead of the <meta name="TOC" content="This is the TOC"/> in the stylesheet above, you want to change that to whatever the actual meta element is that you want to add.

    This customization layer customizes the head.content.generator template to cause it to take whatever you actually set as your custom meta element, and inject that just before the meta name="generator" element that the stock DocBook stylesheets themselves output.

    The <xsl:if test="not(parent::*)">…</xsl:if> causes the stylesheets to output your custom meta element only in the first output HTML “chunk”, which by default is named index.html and which by default is the same file the TOC is included in.