You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@xmlbeans.apache.org by Pascal Maugeri <pa...@gmail.com> on 2009/10/15 16:31:57 UTC

Force the default namespace of an XML fragment

Hi

I'm writing a method to set the default namespace of a given XML fragment:

    public static XmlObject setDefaultNamespace(XmlObject xmlFragment,
String namespace) {

        XmlCursor eltCursor = xmlFragment.newCursor();
        if (eltCursor.currentTokenType().isStartdoc())
            eltCursor.toNextToken();

        String defaultNs = eltCursor.namespaceForPrefix("");
        if (defaultNs == null || defaultNs.length() == 0) {
            System.out.println("No default namespace detected!");
            eltCursor.insertNamespace("", namespace);
        }

        eltCursor.dispose();
        return xmlFragment;
    }

The call to "eltCursor.insertNamespace("", namespace);" does not work, it
doesn't set the default namespace in the result returned. I observed that it
works only when the prefix is set (eg. eltCursor.insertNamespace("pre",
namespace);).

What am I doing wrong ?

Thanks in advance for any help
Pascal