dayvonjersen / ajaxslt

Automatically exported from code.google.com/p/ajaxslt
Other
0 stars 0 forks source link

xsl:copy-of doesn't copy CDATA and COMMENT nodes (plus a clarification on xsl:value-of semantics in the discussion) #1

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. use the sample code that comes with ajaxslt with one modification on the
xml that follows:
<page>
<message><![CDATA[
<b>Hello</b> World.
]]></message>
</page>

2. Run it through AJAXSLT

What is the expected output? What do you see instead?
The expected output should be this:
<div style="color:green">
<b>Hello</b> World.
</div>

However the acutal output is this:
<div style="color:green">
&lt;b&gt;Hello</b> World.
</div>

What version of the product are you using? On what operating system?
I am using version 0.5 on a PC.

Please provide any additional information below.
Maybe I am not doing it correctly. If I am not please point out what I am
doing wrong.

Original issue reported on code.google.com by lea...@gmail.com on 2 Dec 2006 at 12:34

GoogleCodeExporter commented 8 years ago
I was able to get this working with a little modification to 2 files:
util.js and xslt.js

I noticed that just before returning the output in xsltProcess (xslt.js), the
function xmlText is used to generated my html. This function would stomp my 
CDATA
section (the <b>Hello</b> World) because at this point it was tagged as text 
and not
CDATA. So I thought that I could solve this by setting the cdata option to 
true. This
did not quite solve it so I went into the xmlTextR function (util.js) and 
modifed the
section when testing if a nodeType is DOM_TEXT_NODE. Instead of just escaping 
the
text, I tested if the cdata option was flagged as true. If the cdata option was
flagged as true then I did not escape the text node.

Now when I run my code it runs as expected. Now I do not know if this hack is 
the
correct way of processing the xslt but I hope that it gives a little insite 
into the
problem.

I am attaching the modifed versions if you would like to take a look.

Original comment by lea...@gmail.com on 3 Dec 2006 at 5:30

Attachments:

GoogleCodeExporter commented 8 years ago
That solution is wrong.

There are two issue here: first, xmlEscapeText() is broken (as pointed out in 
another
bug report). It only escapes the *first* occurrence of each markup character < 
> &,
but not *each*. This leads to the weird lt;b&gt;Hello</b> in the output. I'm 
fixing this.

The second issue is what output to expect. It is wrong to expect a CDATA node 
in the
output, because the output is generated using xsl:value-of. Cf.
[http://www.w3.org/TR/xslt#value-of]: "The xsl:value-of element is instantiated 
to
create a text node in the result tree. The required select attribute is an
expression; this expression is evaluated and the resulting object is converted 
to a
string as if by a call to the string function." Now, the string value of a 
CDATA node
is just its text, with no character treated as markup. That is, in the output 
appears
a TEXT node, not a CDATA node. Now if the nodeValue of a TEXT node contains 
markup
characters, they are represented in the XML text as entity references &lt; &gt;
&amp;, which is what you see. 

If you want to transfer the CDATA node into the output, you have to 
xsl:copy-of, as in 

<xsl:template match="page/message">
<div style="color:green">
<xsl:copy-of select="node()"/>
</div>
</xsl:template>

This surfaces another problem, namely that CDATA nodes are not copied by 
copy-of. I'm
fixing this too (and comment nodes are also not copied by copy-of).

Original comment by steffen....@gmail.com on 15 Dec 2006 at 3:24

GoogleCodeExporter commented 8 years ago
The xsl:copy problem is fixed in release-0-6, as well as the XML escaping 
problem.

Original comment by steffen....@gmail.com on 15 Dec 2006 at 8:47

GoogleCodeExporter commented 8 years ago

Original comment by steffen....@gmail.com on 15 Dec 2006 at 10:00