Re: using xslt/JS inside the page
Nic Ferrier <[email protected]>
| Newsgroups | gmane.comp.mozilla.devel.layout.xslt |
|---|---|
| Organization | Tapsell-Ferrier Limited |
| Message-ID | <[email protected]> |
Lucky <[email protected]> writes: > Nic Ferrier wrote: >> Here's an HTML document: >> <html> >> <head> >> <script> >> var proc = new XSLTProcessor(); >> var xslt = document.implementation.createDocument("", "", null); >> xslt.innerHTML = "<?xml version='1.0'?>" >> + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0>" >> + "<xsl:output method='html'/>" >> + "<xsl:template match='/test'><p>goodbye!</p></xsl:template>" >> + "</xsl:stylesheet>"; >> proc.importStylesheet(xslt); >> var src_doc = document.implementation.createDocument("", "", >> null); >> src_doc.innerHTML = "<?xml version='1.0'?>" >> + "<test>" >> + "<item><title>hello!</title><link>someplace</link></item>" >> + "<item><title>hello to you too!</title><link>otherplace</link></item>" >> + "</test>"; >> var result = proc.transformToFragment(src_doc, document); >> alert(result); >> alert(result.innerHTML); >> </script> >> </head> >> <body> >> <h1>hello</h1> >> </body> >> </html> >> result.innerHTML is null and looking at result it looks like it has >> no >> children, where it should have one: a text node saying "goodbye". >> Anybody know why this is? >> Nic Ferrier > > you should try using: > > var xslt = (new DOMParser()).parseFromString('<your>xml</your>','text/xml'); > > and same for the xmlDocument creations. > > should get you a step closer? Did you mean use DOMParser to parse the xslt document as well as the document to be transformed: var proc = new XSLTProcessor(); var xslt_str = "<?xml version='1.0'?>" + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0>" + "<xsl:output method='html'/>" + "<xsl:template match='/test'><p>goodbye!</p></xsl:template>" + "</xsl:stylesheet>"; var xslt = (new DOMParser()).parseFromString(xslt_str, 'application/xml'); proc.importStylesheet(xslt); This fails: Error: uncaught exception: [Exception... "Component returned failure code: 0x80600001 [nsIXSLTProcessor.importStylesheet]" nsresult: "0x80600001 (<unknown>)" location: ... It also fails using text/xml as the MIME type. Using the method I first outlined: var xslt = document.implementation.createDocument("", "", null); xslt.innerHTML = "<?xml version='1.0'?>" + "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0>" + "<xsl:output method='html'/>" + "<xsl:template match='/test'><p>goodbye!</p></xsl:template>" + "</xsl:stylesheet>"; proc.importStylesheet(xslt); Does result in a working stylesheet but I don't get any data. Using a source document parsed with DOMParser and the working XSLT doesn't work either. Do you *know* the DOMParser works with XSLT? Any more ideas? Nic