Search code examples
xmlxml-parsingxml-namespacesaxiom

Axiom OMElement generates child with empty default name space


Hi this is what i do for generating an xml code:

OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace nsSequence = factory.createOMNamespace("http://ws.apache.org/ns/synapse", "");
OMElement rootSequence = factory.createOMElement("sequence",nsSequence);


/*<FILTER>*/
OMNamespace nsFilter = factory.createOMNamespace("http://org.apache.synapse/xsd", "ns");        
OMElement filter = factory.createOMElement("filter",nsFilter);
OMAttribute regex = factory.createOMAttribute("regex", null, "applID");
OMAttribute source = factory.createOMAttribute("source", null, "get-property('applicationID')");

filter.addAttribute(regex);
filter.addAttribute(source);

/*<THEN>*/
OMElement then = factory.createOMElement("then",null);          

filter.addChild(then);
rootSequence.addChild(filter);

the generated code is like this one:

<sequence xmlns="http://ws.apache.org/ns/synapse">
    <ns:filter xmlns:ns="http://org.apache.synapse/xsd" regex="APPP" source="get-property('applicationID')">
        <then xmlns=""></then>
    </ns:filter>
</sequence>

The XMLNS="" inside the THEN element is a big problem for me.

i m using axiom-api 1.2.14... and i read somewhere this is a problem (bug) experienced by others (maybe already solver?). Is there a way to work around this problem in order to obtain a clean xml code? or better to solve it?


Solution

  • You are creating the then element without namespace:

    OMElement then = factory.createOMElement("then", null);
    

    Therefore Axiom generates a xmlns="" so that the element has no namespace, exactly as you have requested. In fact, without the xmlns="", the element would have the default namespace http://ws.apache.org/ns/synapse, which would be wrong.

    If you think that the namespace declaration is wrong, then this probably means that the element should actually belong to one of the other namespaces used in the document. If that's the case, then you should fix the code to request an element in the right namespace.