Re: How to dynamically insert HTML from XSLT into document?

Martin Honnen <[email protected]>
Newsgroups gmane.comp.mozilla.devel.xml
Organization Liberty Development
Message-ID <[email protected]>

Siegfried Heintze wrote:

> Here is a fragment of my code that is working:
> 
>   processor.importStylesheet(xslNode);
> 
>   var xmlNode = (new DOMParser()).parseFromString(
>     '<?xml version="1.0"?>'
>       +'<document>'
>         +'<x name="x">x</x>'
>         +'<y name="y">y</y>'
>         +'<z name="z">z</z>'
>       +'<abc>'
>         +'<def>def</def>'
>       +'</abc>'
>       +'</document>', "text/xml");
>   var newDocument = processor.transformToDocument(xmlNode);
>   xslTarget.innerHTML = (new
> XMLSerializer()).serializeToString(newDocument);
> 
> 
> This seems terribly inefficient to take the DOM resulting from the XSLT and
> convert it to a string only to parse it again when it is stuffed into the
> innerHTML. Is there a simpler way? Perhaps I could just add "newDocument" as
> a child of xslTarget? Does anyone have an example of better way of
> displaying the result of a XSLT?

See the FAQ on scripting XSLT transformations in Mozilla
   <http://www.mozilla.org/projects/xslt/js-interface.html>
it explains that besides transformToDocument there is 
transformToFragment and when it is useful (which very much describes the 
case then you have above).
So
   var docFragment = processor.transformToFragment(xmlNode, 
xslTarget.ownerDocument);
gives you a document fragment node as a result of the transformation and 
document fragment nodes can be inserted with normal DOM operations like 
appendChild, replaceChild, insertBefore into other nodes.


> Also, is there a better way to load xml (than what I have done above) since
> Martin Honnen is discouraging me from using XML data islands? I suppose I
> could load an XML file from the server. 

Loading from file is what I suggested and pretty much prefer from having 
some hard coded XML inside some other document, if you write your script 
to be able to load from a file then you have page that can apply an XSLT 
transformation to any XML file data you later load compared to the XML 
data embedded directly in the page.

 > Are there any other alternatives to
> data islands?

As said, data islands is something MSIE extends normal HTML (served as 
text/html) with. If you are only looking at Mozilla and really don't 
want to load XML from a separate file but want to have XML data inside 
your HTML document with script then you need to use XHTML (real XHTML, 
meaning content type application/xhtml+xml so that Mozilla uses its XML 
parser to parse it) and put the XML data in a separate namespace inside 
the XHTML document, here is a very simply example:
<http://home.arcor.de/martin.honnen/xsltScripting/test2005020101.xhtml>
That works flawlessly here for me with Firefox 1.0 (based on Mozilla 
1.7.5) and Netscape 7.2 (based on Mozilla 1.7.2), I expect it to work 
with older Mozilla versions starting from Mozilla 1.2 but I haven't 
tested that.

But I don't see anything gained doing this, at least not on the web in 
general, if you load XML with script then only those browsers supporting 
that have to deal with loading the XML, and as already said loading with 
script lets you write a single (X)HTML page and then loads as many XML 
files as wanted when needed without cluttering your HTML document with 
embedded XML data.

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.