Grizzly Peak Software
Grizzly Peak Software

Reputation: 1418

Rendering HTML Tags from within CDATA tag in XSL

I have a CDATA tag within my XML code which contains some hyperlinks.

<smartText><![CDATA[
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.]]>
</smartText>

I am trying to transform it into an HTML page as follows...

<p class="smartText">
    <xsl:copy-of select="marketSummaryModuleData/smartText"/>                                    
</p>    

Unfortunately the output onto the page shows up in pure text, not as html.

Among individual stocks, the top percentage gainers in the S.&P. 500 are <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=PLD'>ProLogis</a> and <a href ='http://investing.businessweek.com/research/stocks/snapshot/snapshot.asp?ric=LNC'>Lincoln National Corp</a>.

The CDATA section is being created from a classic ASP page, so the actual XML output does not contain the CDATA section. Could that be part of the problem? I cannot seem to get the information to render on the page. I have tried multiple solutions offered up by Google searches, such as disable-escape-tags, xsl:copy-of, xsl:value-of and more.

Thank you

Upvotes: 12

Views: 17487

Answers (3)

user5475062
user5475062

Reputation: 1

<xsl:for-each select="marketSummaryModuleData/smartText">
    <xsl:copy-of select="node()"/>
</xsl:for-each>

<smartText>
Among individual stocks, the top percentage gainers in the S.&P. 500 are
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=LNC'>Lincoln National Corp</a> and 
<a href ='http://investing.domain.com/research/stocks/snapshot
/snapshot.asp?ric=PLD'>ProLogis</a>.
</smartText>

Upvotes: 0

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243599

You have to correct the XML so that the desired HTML (and it needs to be well-formed XML) is not contained within a CDATA section.

Any CDATA section is just part of a text() node and the XSLT processor treats it as such.

Putting markup within CDATA is universally acknowledged as bad practice and the reported issue is one typical result.

DOE (disable-output-escaping) is an optional feature in XSLT and is not guaranteed to be implemented and producing the same expected results on different XSLT processors.

To quote the W3C XSLT Spec.:

"An XSLT processor is not required to support disabling output escaping. If an xsl:value-of or xsl:text specifies that output escaping should be disabled and the XSLT processor does not support this, the XSLT processor may signal an error; if it does not signal an error, it must recover by not disabling output escaping. "

and:

"Since disabling output escaping may not work with all XSLT processors and can result in XML that is not well-formed, it should be used only when there is no alternative."

Upvotes: 5

Tomalak
Tomalak

Reputation: 338396

<p class="smartText">
  <xsl:value-of 
    select="marketSummaryModuleData/smartText" 
    disable-output-escaping="yes"
  />
</p>

EDIT: As @Randell points out in the comments, disable-output-escaping is not present in all XSLT processors. For instance, the one in Firefox does not support this attribute. The above won't work for these processors. All stand-alone XSLT processors I know support it, though.

Upvotes: 11

Related Questions