Rendering HTML Tags from within CDATA tag in XSL
Asked Answered
A

3

12

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

Attic answered 31/3, 2009 at 15:58 Comment(1)
Final solution... Please see this posting. #706814Attic
S
5

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."

Selfdrive answered 31/3, 2009 at 16:9 Comment(5)
@Big d-o-e may or maynot solve the OP's problem, depending on which XSLT processor (s)he is using.Selfdrive
disable-output-escaping does work, but not in all browsers. Since this module will be used by thousands of people, it will need to work in all browsers. It looks like I will need to take the data in my ASP code and parse it into multiple tags instead of one large string of text.Attic
@Shane: you should add the detail that you need to work in all browsers in your question. Would a server-side transform work for you?Yamen
It would be nice to transform on the server side, but since this is a cross-domain solution, it will need to be transformed on the client-side.Attic
Since this segment has the most information in it I am marking this as the right answer. The other answer is correct too, but not for all browsers. Was trying to take a shortcut by inserting HTML into the tags, but now I've realized that I really need to break out the info into well formed XML.Attic
B
11
<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.

Big answered 31/3, 2009 at 16:7 Comment(5)
@Randell: It's not clear at all from the question that the XSLT transformation is being done in a browser.Big
Could you expound on this a bit?Headwind
@Randell: Not sure what you mean. For my part, any XSLT I do runs on a server of some sort. I have not yet encountered a situation where disable-output-escaping was not available in a server environment.Big
Interesting. I've only used XSLT for browsers. What kind of data do you process on the server? Also, if you may edit your answer, I can cancel my downvote.Headwind
@Randell: Probably the same data that you would send to the client along with an XSLT stylesheet. I like being in control over the conversion process and being independent, so it runs on all clients down to smart phones. Same with RSS - if you're going to convert XML to RSS, the place to do it is on the server. XSLT capabilities of different clients have always been too inconsistent for my tastes.Big
S
5

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."

Selfdrive answered 31/3, 2009 at 16:9 Comment(5)
@Big d-o-e may or maynot solve the OP's problem, depending on which XSLT processor (s)he is using.Selfdrive
disable-output-escaping does work, but not in all browsers. Since this module will be used by thousands of people, it will need to work in all browsers. It looks like I will need to take the data in my ASP code and parse it into multiple tags instead of one large string of text.Attic
@Shane: you should add the detail that you need to work in all browsers in your question. Would a server-side transform work for you?Yamen
It would be nice to transform on the server side, but since this is a cross-domain solution, it will need to be transformed on the client-side.Attic
Since this segment has the most information in it I am marking this as the right answer. The other answer is correct too, but not for all browsers. Was trying to take a shortcut by inserting HTML into the tags, but now I've realized that I really need to break out the info into well formed XML.Attic
V
0
<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>
Videlicet answered 22/10, 2015 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.