views:

314

answers:

2

The input to my XSL is an XHTML. After applying the XSL the DOCTYPE declaration that was present in the input XHTML gets lost in the output. Do i have an option to copy/retain the DOCTYPE declaration in the output using XSL. The XSL processor that i am using is SAXON.

+1  A: 

Add an output directive:

<xsl:output 
  doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
  doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
/>

By the way, output directives stack - you can have as many of them as you want.

Tomalak
If the input xhtml had the DOCTYPE <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> If i have set the DOCTYPE as shown above then I will end up setting the DOCTYPE of the output as <!DOCTYPE html PUBLIC "="-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Is there a way to get the DOCTYPE of the input to the output?
Rachel
@Rachel: Hm… not easy. I'm not aware of a way to do this in XSLT 1.0. What XSLT version do you use? This page indicates there is a way to do it in XSLT 2.0: http://www.biglist.com/lists/lists.mulberrytech.com/xsl-list/archives/200807/msg00398.html
Tomalak
I use XSL 2.0. Will check the link.
Rachel
Can i get the 2 info doctype public and doctype system as input params and set them in output dynamically as shown below? <xsl:param name="doctype.public" as="xsd:string" /> <xsl:param name="doctype.system" as="xsd:string" /> <xsl:output doctype-public="$doctype.public" doctype-system="$doctype.system" /> I am getting the output as: <!DOCTYPE html PUBLIC "$doctype.public" "$doctype.system"> I am getting the variable name in the output as shown above. Am i missing something?
Rachel
@Rachel: Yes you are missing something, namely the curly braces that trigger variable evaluation: `<xsl:output doctype-public="{$doctype.public}" doctype-system="{$doctype.system}" />`
Tomalak
I tried it and got the error, XTSE0020: Invalid character in doctype-public parameter where i passed "-//W3C//DTD XHTML 1.0 Transitional//EN" for public. I just tried adding only doctype system using, <xsl:output doctype-system="{$doctype.system}" /> Output: <!DOCTYPE html SYSTEM "{$doctype.system}"> Same issue.
Rachel
@Rachel: Go back to the document I linked, and have a closer look: They've used `<xsl:result-document>`, not `<xsl:output>` (I was convinced that this would not work with `<xsl:output>` anyway, but I was unsure if XSLT 2.0 maybe allowed it).
Tomalak
A: 

You can do this, but not in XSLT since DOCTYPE is not part of the infoset given to XSLT. When you invoke your transformation, you read the DOCTYPE properties from the source document and set them as output properties on your XSLT transformation.

.NET API

Java API

Max Toro