Re: XSL Problems

Martin Honnen <[email protected]> Fri, 29 Dec 2006 15:20:10 +0100
Newsgroups gmane.comp.mozilla.devel.xml
Organization Liberty Development
Message-ID <[email protected]>
Leon Bezuidenhout wrote:

> var oNode = document.implementation.createDocument("","xsl:choose", 
> null).firstChild;
> 
> oNode.setAttribute("xmlns:xsl", "http://www.w3.org/1999/XSL/Transform");
> 
> 
> Why are my nodes generated without the "xsl:" ? I know I am not setting the 
> namesoace in the proper manner, but this is the only way I have found that 
> displays the namespace in my XML.

Use namespace aware DOM Level 2 methods like createElementNS and then 
make sure you pass in namespace URIs where required e.g.

var xslNs = 'http://www.w3.org/1999/XSL/Transform';

var stylesheet = document.implementation.createDocument(xslNs, 
'xsl:stylesheet', null);

stylesheet.documentElement.setAttributeNS(null, 'version', '1.0');

var template = stylesheet.createElementNS(xslNs, 'xsl:template');
template.setAttributeNS(null, 'match', '@* | node()');
var copy = stylesheet.createElementNS(xslNs, 'xsl:copy');
var applyTemplates = stylesheet.createElementNS(xslNs, 
'xsl:apply-templates');
applyTemplates.setAttributeNS(null, 'select', '@* | node()');
copy.appendChild(applyTemplates);
template.appendChild(copy);

stylesheet.documentElement.appendChild(template);

So every time you create an element you need to pass in the proper 
namespace URI as the namespace of an element is determined when it is 
created.



-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/