Working with XSLT and escaped HTML markup
I just encountered this issue working on an XSLT application: The xml given by our data export service was escaping out any HTML markup found within the attributes of my target XML node. This is done to ensure that the XML document remains well formed. However, the frustration in this scenario is that a web browser will also ignore the escaped markup when rendering, resulting in text being sent to the screen containing my markup, but not acting on it as HTML. (Not its fault, the browser handles this as expected)...so my text looked like:
Hey, this should be on <br> two lines!
instead of:
Hey, this should be on
two lines!
Initially there was some discussion that I could use CDATA for this. Keep in mind that CDATA is really just escaping every markup element, saving you from having to escape all of the items individually. Once we get to the browser, however, all differences between the two are lost.
Here's the short converstion with myself......
===========================================================
My issue is that html markup is being escaped in order to maintain valid XML. This is going to cause headaches when transformed via XSL, as I’ll have to catch and convert every instance of escaped elements, and un-escape them.
============================================================
I found a way around this: When selecting an attribute using xsl’s <value-of> method, an option exists to disable any escaped characters:
<xsl:value-of select="@Description" disable-output-escaping="yes"/>
This allows my escaped markup to exist peacefully in the XML, but still render as HTML elements. I wouldn’t recommend this method for open-ended production (it could allow badly formed structure to survive), but for this project we control both the source and destination of the data, and I feel confident this is a safe practice for our use.
=============================================================
NOTE:
I think I'm reversing myself on this one... Firefox does not (and will not) support the disable-output-escaping propery, as it violates the W3C spec for validity. I will be reorganizing my XML to move the attributes in question into separate elements, and then apply the CDATA markers.