tags:

views:

509

answers:

2

I have a XSLT 1.0 (2.0 is not an option) stylesheet which produces XHTML. It can, depending on a parameter, produces a full XHTML validable document or just a <div>...</div> snippet, intented 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"/&gt;

And for the <div> one:

<xsl:output omit-xml-declaration="yes"/>

But <xsl:output> cannot be included in an <xsl:if>. It can be only 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 advices from Andrew Hare and jelovirt, I wrote two "drivers", two simple stylesheets which calls 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"/&gt;

</xsl:stylesheet>
+1  A: 

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.

Andrew Hare
+1  A: 

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.

jelovirt