Reputation: 41
I would like to use a single XSL to produce multiple output formats (xml and html for now)
I would like to define which output format by means of a stylesheet
So the code I have is as follows:
<xd:doc scope="stylesheet">
<xd:desc>
<xd:p><xd:b>Created on:</xd:b> July 1, 2015</xd:p>
<xd:p><xd:b>Author:</xd:b> me</xd:p>
<xd:p>A stylesheet to test the application of XYZABC</xd:p>
<xd:p>takes a single parameter - xslt_output_format</xd:p>
<xd:p>valid inputs - xml html</xd:p>
</xd:desc>
</xd:doc>
<xsl:output name="xml_out" encoding="UTF-8" indent="yes" method="xml" />
<xsl:output name="html_out" encoding="ISO-8859-1" indent="yes" method="html"/>
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$xslt_output_format = 'xml'">
<data>
<p>This is some test xml output</p>
</data>
</xsl:when>
<xsl:when test="$xslt_output_format = 'html'">
<html>
<head>
<title>HTML Test Output</title>
</head>
<body>
<p>This is some test html output</p>
</body>
</html>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
If I pass 'xml' as the parameter I get
This is some test xml output
and if I pass 'html' I get HTML Test Output
This is some test html output
That doesn't seem to respect my respect for ISO-8859-1 encoding on the html (which I was just using to test the was working)
Michael Kay's XSLT 2.0 and Xpath 2.0 tome is a little vague and definitely short of examples on using multiple statements (sorry Mike)
So I am just asking am I using it correctly?
Can I achieve what I am aiming for? TIA Feargal
Upvotes: 0
Views: 1172
Reputation: 163549
xsl:output by itself doesn't allow you to make any run-time selection of output method.
In 2.0 you can use xsl:output in conjunction with xsl:result-document: the xsl:result-document can select a named xsl:output declaration, or it can override some of its attributes selectively.
Another option (also available in 1.0) is to override xsl:output from the calling API: if you're using JAXP look at Transformer.setOutputProperty().
On the Saxon command line you can set output properties using the syntax !indent=yes on the command line (the "!" needs to be "\!" with some shells).
Upvotes: 0
Reputation: 167716
I think you need to use xsl:output
together with xsl:result-document
http://www.w3.org/TR/xslt20/#creating-result-trees, so try along the lines of
<xsl:template match="/">
<xsl:choose>
<xsl:when test="$xslt_output_format = 'xml'">
<xsl:result-document format="xml_out" href="output.xml">
<data>
<p>This is some test xml output</p>
</data>
</xsl:result-document>
</xsl:when>
<xsl:when test="$xslt_output_format = 'html'">
<xsl:result-document format="html_out" href="output.html">
<html>
<head>
<title>HTML Test Output</title>
</head>
<body>
<p>This is some test html output</p>
</body>
</html>
</xsl:result-document>
</xsl:when>
</xsl:choose>
</xsl:template>
I would probably use templates and modes to distinguish the two different ways of processing but the advice on using xsl:output
and xsl:result-document
remains the same.
Upvotes: 2