{ Frameless }

XSLT2Processor.setInitialMode()

The setInitialMode($name) function in XSLT templates can be tagged using a 'mode', where more then one template can match the same element but do different things in different modes.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

  <xsl:template match="chapter">
    <section>...</section>
  </xsl:template>

  <xsl:template match="chapter" mode="table-of-contents">
    <li>...</li>
  </xsl:template>

</xsl:stylesheet>

In this example you can create a table of contents like this:

xslt = new XSLT2Processor
xslt.setInitialMode("table-of-contents")
xslt.transformToDocument(bookDocument)

Then instead of creating a section for all chapters with all the chapters text in it, you can create one list item with just the chapter title, without creating two .xsl documents.

book.xsl

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    xmlns:ns="http://ridiculo.us/">

  <xsl:template match="chapter">
    <section>...</section>
  </xsl:template>

  <xsl:template match="chapter" mode="ns:table-of-contents">
    <li>...</li>
  </xsl:template>

</xsl:stylesheet>

script.js

xslt = new XSLT2Processor
xslt.setInitialMode("{http://ridiculo.us/}table-of-contents")
xslt.transformToDocument(bookDocument)