I have an XSLT 1.0 (2.0 is not an option) stylesheet which produces XHTML. It can, depending on a parameter, produce a full XHTML validable document or just a <div>...</div> snippet, intended for inclusion in a Web page.
My problem is to produce different XML declarations in these two cases. For the standalone page, I need:
<xsl:output doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN' doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'/>
And for the <div> one:
<xsl:output omit-xml-declaration='yes'/>
But <xsl:output> cannot be included in an <xsl:if>. It can only be the direct child of <xsl:stylesheet>.
The only solution I see is to create a stylesheet with most of the templates and then two small ‘wrappers’ with the right <xsl:output> and which will <xsl:import> the main stylesheet.
I was looking for a better idea but apparently there is none. Following advice from Andrew Hare and jelovirt, I wrote two ‘drivers’, two simple stylesheets which call the proper <xsl:output> and then the main stylesheet. Here is one of these drivers, the one for standalone HTML:
<?xml version='1.0' encoding='us-ascii'?> <!-- This file is intended to be used as the main stylesheet, it creates a standalone Web page. --> <xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'> <xsl:import href='traceroute2html.xsl'/> <xsl:param name='standalone' select=''true''/> <xsl:output doctype-public='-//W3C//DTD XHTML 1.0 Strict//EN' doctype-system='http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'/> </xsl:stylesheet>
It sounds like what you need is two different stylesheets. If at all possible you should create two separate stylesheets and dynamically call the one you need from code.