David Oliver
David Oliver

Reputation: 2502

Only applying XSLT template if a template with the given mode exists

In my site's master XSLT template I have:

<xsl:apply-templates select="/data" mode="page-scripts"/>

On another page that imports the above master stylesheet, I have a template that inserts some JavaScript:

<xsl:template match="/data" mode="page-scripts">
    <!-- JavaScript -->
</xsl:template>

On pages that don't have the above template, the contents of the data node is output according to the default template.

What would be a good way to avoid this without having to have a blank template that matches the page-scripts mode?

Upvotes: 3

Views: 733

Answers (2)

David Oliver
David Oliver

Reputation: 2502

Sorry, I've just found the answer here.

I had to put <xsl:template match="*" mode="page-scripts"/> in my master stylesheet to override the default template.

Upvotes: 2

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243529

On pages that don't have the above template, the contents of the data node is output according to the default template.

What would be a good way to avoid this without having to have a blank template that matches the page-scripts mode?

There is no way to prevent the XSLT built-in templates from being selected for execution, except by overriding them.

A good solution is to place the "blank" overriding template in the "master" stylesheet itself, thus avoiding the need to add any such templates in any of the importing stylesheets.

Upvotes: 3

Related Questions