I had a real hard time today trying to work out why my encoded XML document was being re-encoded when rendered using XSLT.
My problem was that an apostrophy ( ‘ ) in my XML was properly encoded to ’ If this is presented to the browser “as-is” then the html entity is correctly rendered as ( ‘ ).
However, after transforming the XML document using XSLT, the output is encoded again, resulting in ’ which renders in the browser as ’
At first I thought there was something wrong with my code, something in my XslCompiledTransform .net object that I had forgotten to set. It was however an inbuilt specification that any conforming XSLT processor should adhere to known as Output Escaping.
The solution was to disable output escaping on the elements within the XSLT document. This is done on a per element basis.
Here is a snippet from my XML document.
1 2 3 4 5 | <description> <![CDATA[ Has DateTime.Parse ever thrown the following exception when you passed it a legally formatted value, leaving you bemused as to why it can’t parse it? ]]> </description> |
And here is a snippet of my XSLT document that reads the XML snippet above.
(notice disable-output-escaping=”yes”)
1 | <xsl:value-of select="description" disable-output-escaping="yes" /> |
Of course, you will need to be confident that the XML feed that you are reading from has been properly encoded before you transform it, otherwise something could break.
For a detailed explanation of disable-output-escaping see Section 16.4 of the XSL Transformations (XSLT) W3 Documentation – http://www.w3.org/TR/xslt#disable-output-escaping.