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.
Another option, which I recently had to employ, is to:
- Omit the XML declaration in all cases.
- Conditionally, output the declaration as unescaped text.
This only works in XSLT 1.0 and 2.0 if you're outputting to a file -- this won't work if you need to process the output as XML in the same pass, such as when storing in a variable.
(Note that XSLT 2.0 extension functions might make it possible to take this output and treat it as XML in one go, and XSLT 3.0 has a built-in function to parse an input string as XML.)
Example snippet:
<!-- Omit the XML declaration as the base case:
we can conditionally output a declaration
as text, but we *cannot* apply conditions on
this `omit-xml-declaration` attribute here. -->
<xsl:output method="xml" indent="no"
omit-xml-declaration="yes"
/>
<!-- Root element match: evaluate different cases, output XML declaration,
XHTML DOCTYPE, or something else, then process the rest of the input. -->
<xsl:template match="/">
<xsl:choose>
<xsl:when test="'... some condition ...'">
<xsl:text disable-output-escaping="yes"><?xml version="1.0" encoding="UTF-8"?></xsl:text>
</xsl:when>
<xsl:when test="'... some other condition ...'">
<xsl:text disable-output-escaping="yes"><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"></xsl:text>
</xsl:when>
<xsl:otherwise>
<!-- ... some third kind of output ... -->
</xsl:otherwise>
</xsl:choose>
<!-- Just process the rest -->
<xsl:apply-templates/>
</xsl:template>
... [ other code ] ...
In XSLT the value of omit-xml-declaration
must be either yes
or no
, you can't use Attribute Value Templates there. This applies to both 1.0 and 2.0.
The doctype attributes can use AVTs, but the problem is that you cannot omit the attribute, you can only output an empty attribute and this leads to output that has empty doctype strings.
Sorry, can't be done with XSLT. You can either use two different stylesheets, or set the output parameters in the code that calls the XSLT processor.