ok, I've sussed this out.
I'm posting this here so that the next person who has the problem can find something.
(sorry to those who have already got their head around namesapce nodes)
How to generate an XSL file on the fly as a DOM document using JAXP.
warning: If prior attempts do not work, remember that just because the DOM document
prints out as exactly what you want does not mean that the DOM java object is
correctly structured (this is a bit of a weakness in the DOM API no?)
1. declare your DocumentBuilderFactory as name space aware.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
2. Create your root element thus :
Element xslRoot = (Element)
stylesheet.createElementNS("http://www.w3.org/1999/XSL/Transform", "xsl:stylesheet");
xslRoot.setAttribute( "version", "1.0");
xslRoot.setAttribute( "xmlns:xsl", "http://www.w3.org/1999/XSL/Transform" );
3. remember to create elements using createElementNS() for the descendants.
Element caller = (Element)
stylesheet.createElementNS("http://www.w3.org/1999/XSL/Transform",
"xsl:call-template");
caller.setAttribute("name", templateName);
selecter.appendChild( caller );
etienne
> Gary,
>
> There is something about namespaces I have missunderstood, and the documentation
> for doing this stuff (in java) is practically non-existent (or very well hidden).
> Forgive me for my ignorance, but, syntactically, what is the :
>
> namespaceURI
> qualifiedName
> value
>
> required for setAttributeNS ?
>
> This code produces the right XML file (when printed out)..........
>
> Element xslRoot = (Element) stylesheet.createElementNS("test", "xsl:stylesheet");
> xslRoot.setAttributeNS( "test", "version", "1.0");
> xslRoot.setAttributeNS( "test", "xmlns:xsl",
> "http://www.w3.org/1999/XSL/Transform" );
> stylesheet.appendChild( xslRoot );
>
> but still gives me an "attibute: version" is missing error.
>
> etienne.
>
> > Etienne --
> >
> > This is not correct because you are not putting these elements into a
> > namespace. You need to use createElementNS to create an element in the
> > namespace.
> >
> > Gary
> >
> > Etienne Deleflie wrote:
> > >
> > > Hi,
> > >
> > > more on this dynamic XSL DOM document creation. Perhaps I am not creating the
> > > DOM document correctly (even though it prints out correctly)
> > >
> > > ........... I am setting both the attributes of the root node (version and
> > > xmlns:xsl)
> > >
> > > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
> > >
> > > by doing the following
> > >
> > > // make a root node for the xml Document
> > > Element root = (Element) document.createElement("page");
> > > document.appendChild(root);
> > >
> > > // namespace and version
> > > Element xslRoot = (Element) stylesheet.createElement("xsl:stylesheet");
> > > xslRoot.setAttribute("version", "1.0");
> > > xslRoot.setAttribute("xmlns:xsl", "http://www.w3.org/1999/XSL/Transform");
> > >
> > > does this seem correct ?
> > >
> > > etienne