wst
wst

Reputation: 11771

Use external XSLT only for certain templates

I want to import a XSLT stylesheet, but essentially to have it ignored by default, and only use its templates when called upon.

Our content contains custom XML + DocBook-style tables, so it's currently transformed by two XSLTs: XML => my.xsl => docbook.xsl => XHTML. Is it possible to do this all from my.xsl?

<!-- My XML -->
<xsl:template match="a"/>
<xsl:template match="b"/>
<xsl:template match="c"/>

<!-- DocBook XML -->
<xsl:template match="table">
  <xsl:apply-templates select="." mode="docbook"/>
</xsl:template>

The problem with importing the docbook.xsl stylesheet is that it includes many other templates that interfere with existing templates. Ideally, there would be a way to import docbook.xsl into a specified mode, but that's not possible. Is there a way to do this without modifying docbook.xsl?

Upvotes: 0

Views: 199

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

Use modes.

In the imported stylesheet have all templates be in a mode with unique name that nobody uses. For example use prefix:mymode and have the prefix prefix bound to a namespace that belongs to you.

To invoke processing from the imported stylesheet, use:

<xsl:apply-templates select="$vNodesToBeProcessed" mode="prefix:mymode"/>

Upvotes: 1

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

Templates in the main stylesheet always have higher priority than templates from imported stylesheets. To avoid invoking the imported templates, simply override them in the main, in the required circumstances.

This should be the preferred technique. An alternative is:

  1. Identify the match patterns where there is a potential conflict.
  2. Use modes to discriminate between the two.
  3. When you are invoking your templates with <xsl:apply-templates> on a selection which fits one of your identified overlapping patterns, use the appropriate mode (or no mode) to achieve your intended result (main template versus imported template).

Upvotes: 2

Related Questions