> ...except that it completely fails to work on root elements in a document.
>
What do you mean for root element?
> Fortunately (for my sanity), it appears that createElementNS *does*
appear to work the way that I thought it should. Here's what I see
happen.
>
> If I create an element with createElementNS(nsX, prefix + ...) and
append it to an element which was *also* created with
> createElementNS(nsX, prefix + ...), the attached element will NOT have
an xmlns attribute inserted.
>
> That's why we do the 4-line process above, to make sure that xmlns lines
do appear, so that c14n works as expected (as per my original plaintive
email).
>
> Unfortunately, if we do this to the root element, then createElementNS
will insert it's own xmlns line due to there being no parent to inherit
xmlns from (presumably). If we then create our own, additional, xmlns
attribute, everything proceeds correctly, until we dump it out as a
string and then try to read it back again, at which point the parser
whinges about duplicate xmlns entries.
>
> The interesting thing is that if you either
> a) do createElement(prefix + tag) instead of createElementNS, or b) skip
the extra xmlns attribute setting on the root element,
> it still breaks, just in different places.
>
> If (a), then all child elements get a duplicate xmlns attribute, if (b),
then it complains that "prefix must resolve to a namespace" during the
XMLSignature, somehow ignoring the xmlns attribute that was
> automatically inserted by the createElementNS (back to the original
problem of XMLSignature not seeing namespaces without the 4 line
stanza).
>
First of all, this is a plain DOM question, nothing related with xml-enc
thing.  Ok the DOM creating interface is not well desined regarding
namespace issues. First they created without having namespaces in mind,
and latter they try to fix it. But in my opinion not doing a good job. So
let me explain. Everytime you do createElementNS you just create an
element nothing more, nothing else. No "xmlns=x" attribute, no nothing.
And if you see the signature of the method is more clear:
public Element createElementNS(String namespaceURI,
                               String qualifiedName)
The second argument is the qualifiedName (not the localname). And it is
the only function you need to used(forget about createElement(X) it ends
calling createElementNS(null,X) ). If you use only this and add the xmlns
& xmlns:prefix attributes in the right elements(i.e. the first time they
are used), everything must work. I promise.

The problems you are having is that the xercer serializer add all the
prefix and xmlns definitions for you. But this is not what the xml-sec is
specting(If you want to fix it, feel free to send the patches). So don't
think your dom tree is right because you pretty serilaze it and looks
fine, is sure you are forgetting xmlns binding that are added by the
xerces serializer.

Regards,

Raul
http://r-bg.com



Reply via email to