[ 
https://issues.apache.org/jira/browse/WSCOMMONS-517?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12796892#action_12796892
 ] 

Andreas Veithen commented on WSCOMMONS-517:
-------------------------------------------

Actually, I would expect Axiom to throw an exception in this case. The reason 
is as follows:

As the name of the method implies and as explained by its Javadoc ("This will 
declare a default namespace for this element explicitly"), 
declareDefaultNamespace should only add a namespace declaration, but not change 
the namespace of the element itself. The test case shows that the 
implementation actually doesn't behave in the expected way: in that particular 
example, it changes the namespace of the element from "" to 
"http://someNamespace";.

That this behavior is incorrect is confirmed by two facts:

1. It seems that declareDefaultNamespace only exists because 
declareNamespace(String, String) doesn't allow to declare a default namespace: 
passing an empty prefix to that method has a different meaning, namely to 
instruct Axiom to auto-generate a prefix. See also the Javadoc of the 
declareDefaultNamespace method in the two OMElement implementations: "We use "" 
to store the default namespace of this element. As one can see user can not 
give "" as the prefix, when he declare a usual namespace." [sic] Since 
declareNamespace never changes the namespace of the element, there is no reason 
why declareDefaultNamespace would do so.

2. Only the LLOM implementation of OMElement has this behavior. DOOM's 
implementation (which started as a fork of LLOM) never changes the namespace of 
the element.

That means that the expected result would be as follows:

<p:outerTag xmlns="http://someNamespace"; xmlns:p="">
    <p:innerTag>
        <p:node1>Hello</p:node1>
        <p:node2>Hello</p:node2>
    </p:innerTag>
</p:outerTag>

Indeed, that would be the only way to add a default namespace declaration to 
outerTag without changing its namespace (and that of its descendants). However, 
xmlns:p="" is not a valid namespace declaration (at least not in XML 1.0, not 
sure about XML 1.1): the empty namespace can only be used as default namespace 
and can not be bound to a prefix. Thus, Axiom should trigger an error here.

More investigation is needed to understand why this particular behavior of 
declareDefaultNamespace has been implemented.

> OMElement.declareDefaultNamespace() declares empty namespace for child 
> element.
> -------------------------------------------------------------------------------
>
>                 Key: WSCOMMONS-517
>                 URL: https://issues.apache.org/jira/browse/WSCOMMONS-517
>             Project: WS-Commons
>          Issue Type: Bug
>          Components: AXIOM
>    Affects Versions: Axiom 1.2.8
>         Environment: Microsoft Windows 2003 Server, Java 1.5.0_11. Axis2 
> version 1.3 (uses AXIOM version 1.2.5). 
>            Reporter: Yechiel Mondrowitz
>            Assignee: Andreas Veithen
>
> When calling declareDefaultNamespace() on a parent node, AXIOM assignes a 
> blank namespace on the first level's child element.  So if XML is passed to 
> AXIOM that looks like this:
> <outerTag>
>     <innerTag>
>         <node1>Hello</node1>
>         <node2>Hello</node2>
>     </innerTag>
> </outerTag>
> And then declareDefaultNamespace() of "http://someNamespace"; is called on the 
> <outerTag>, the resulting XML will be this:
> <outerTag xmlns="http://someNamespace";>
>     <innerTag xmlns="">
>         <node1>Hello</node1>
>         <node2>Hello</node2>
>     </innerTag>
> </outerTag>
> Notice the xmlns="" declared in the <innerTag>.  According to my 
> understanding of XML namespaces, the <innerTag> and its child nodes will no 
> longer belong to the parent namespace of "http://someNamespace";, since it 
> explicitly overrides it with an empty namespace.  So <innerTag> and its child 
> nodes will in fact not belong to any namespace!  Here is a small program to 
> illustrate:
> import org.apache.axiom.om.*;
> import org.apache.axiom.om.impl.llom.util.*;
> public class Test2 {
>     public static void main(String [] args) {
>         try {
>             String xmlString =
>                 "<outerTag>" +
>                     "<innerTag>" +
>                         "<node1>Hello</node1>" +
>                         "<node2>Hello</node2>" +
>                    "</innerTag>" +
>                 "</outerTag>";
>             OMElement elem = AXIOMUtil.stringToOM(xmlString);
>             elem.declareDefaultNamespace("http://someNamespace";);
>             System.out.println(elem.toString());
>         }
>         catch(Exception e) {
>             e.printStackTrace();
>         }
>     }
> }
> The output of this program is this (I added line breaks in the XML for easier 
> readability):
> <outerTag xmlns="http://someNamespace";>
>     <innerTag xmlns="">
>         <node1>Hello</node1>
>         <node2>Hello</node2>
>     </innerTag>
> </outerTag>
> As you can see, the <innerTag> was assigned xmlns="" by AXIOM.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to