I'm trying to generate a web page page using XML and XSL, according
the codes below. When I load the XML on IE6 and IE7, it works. When I
load the XML on Firefox (2.0.0.11), the command <xsl:copy-of> doesn't
work. Why it happens? Should I change something on codes?
Thanks,
Daniel
<< cars.xml >>
<xml>
<search>
<year>2007</year>
</search>
<chart>
<series>
<values id="car1">3000</values>
<values id="car1">5000</values>
</series>
</chart>
</xml>
<< template.xsl >>
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" extension-element-
prefixes="msxsl" version="1.0">
<xsl:template match="/">
<textarea rows="40" cols="80" id="meuxml">
<xsl:copy-of select ="xml/chart"/>
</textarea>
<script>
alert(meuxml.value);
</script>
</xsl:template>
</xsl:stylesheet>
In both FF and IE when a textarea element is encontered during the parsing
of HTML the inner content of the textarea is considered to be its value and
no further parsing of markup occurs on that content. (See
http://www.w3.org/TR/html4/interact/forms.html#h-17.7)
In IE the transform is actually performed by an external component which has
no knowledge of the HTML DOM used inside IE. It simply generates the output
as character stream which is then parsed by IE as if the stream were coming
directly from HTTP. Hence the textarea handling is as you would expect.
Firefox, OTH, takes the output of the transform directly into the DOM
because FF's XSL processor is more tightly integrated and can create DOM
elements directly (FF has a single DOM for handling both HTML and XML). So
having created the textarea element it performs the copy-of by literally
doing a deep copy of the selected element and appending it to the textarea
element. Hence the textarea ends up with child nodes. (Note that it is
debatable whether this is a bug, IE for example will allow script to create
child nodes and append them in a textarea, their contents would also not be
displayed by the textarea).
The following code works in FF but in IE it captialises the element names.
<xsl:template match="/">
<textarea rows="40" cols="80" id="meuxml">
</textarea>
<div style="display:none" id="divXml">
<xsl:copy-of select ="xml/chart"/>
</div>
<script>
copyHTML();
function copyHTML()
{
var txt = document.getElementById('meuxml')
var div = document.getElementById('divXml')
txt.value = div.innerHTML
}
</script>
</xsl:template>
--
Anthony Jones - MVP ASP/ASP.NET
<textarea>
<series>
<values id="car1">3000</values>
<values id="car1">5000</values>
</series>
</textarea>
Note that you are not creating that markup, but rather that *DOM*. I.e.
you're getting a DOM like
textarea
+ series
+ values
+ values
However textareas expect a single textnode as a child. During normal
HTML parsing the contents of the textarea is parsed as text, not as
markup, so you'll always get a textnode as child. However when using
XSLT in firefox you are directly creating a DOM, not markup that is
later parsed.
If you use the DOM Inspector you'll see that all the nodes are there and
that xsl:copy-of worked as it should.
Hope that explains what happens.
Best Regards,
Jonas Sicking