Preserve HTML in XML with XSL

I spent longer on this than I should have, when the answer is so simple; I’m posting here to remind myself when it comes up again, and I hope the included keywords assist in anyone’s search for the same information.

The xsl:copy-of element can be used to insert a result tree fragment into the result tree, without first converting it to a string as xsl:value-of does

1) I’m assuming you have XHTML nested in your XML document, if you’re not well-formed, that’s your first problem to address. You can’t validate something you don’t restrict.

2) In your xsl:stylesheet, declare your intent to display HTML markup with:
<xsl:output method="html"/>

3) In your xsl:template, extract the XHTML with:
<xsl:copy-of select="nodeContainingHtml"/>

If you think you want to instead use output=”text” and CDATA to push your non-XHTML markup through, return to step 1.

4 thoughts on “Preserve HTML in XML with XSL

  1. this is the first web page i’ve found that clearly explains how to preserve html in an xsl transformation,

    -thanks

  2. this is the first web page i’ve found that clearly explains how to preserve html in an xsl transformation,

    -thanks

  3. Awesome. After trawing a dozen sites this was exactly what I was looking for, no spitting over acronyms just straight-forward explanation.

    Thanks!!!

  4. Awesome. After trawing a dozen sites this was exactly what I was looking for, no spitting over acronyms just straight-forward explanation.

    Thanks!!!

Leave a Reply

Your email address will not be published.