1

I'm just curious about default namespaces. I have the follow xml:

<root xmlns="myNamespace">
    <someElement xmlns="anotherNamespace">
        <data>Info</data>
    </someElement>
    <anotherElement>
        <moreData>Info2</moreData>
    </anotherElement>
</root>

My guess is that myNamespace is inherit for <root> and <anotherElement>. And anotherNamespace is a default namespace in <someElement> so in this element and for its child's overrides the other myNamespace.

I know that I can rewrite the above xml code like:

<my:root xmlns:my="myNamespace">
    <a:someElement xmlns:a="anotherNamespace">
        <a:data>Info</a:data>
    </a:someElement>
    <my:anotherElement>
        <my:moreData>Info2</my:moreData>
    </my:anotherElement>
</my:root>

I think that both are totally valid but I have some problems with some xml beans implementation in java which doesn't accept the first one, so I'm curious if there is an xml specification where specifies if first aproach is or not correct.

2
  • You are correct, namespaces work the way you describe. If you find software that gets it wrong, please raise a question that describes what you're doing and what the effect is. Commented Jul 9, 2014 at 11:43
  • @MichaelKay thanks for your comment :). The problem is that my WS responds to a 3rd-party java client with the first approach but this client don't recognize it. If I change my WS to respond with second approach the client works correctly. I don't know the exact implementation of this client and I make this question to know if the problem is with my WS or with the client implementation. Commented Jul 9, 2014 at 12:17

1 Answer 1

1

The first approach is valid. In xml-names specification in section 6.2 Namespace Defaulting it explains:

The scope of a default namespace declaration extends from the beginning of the start-tag in which it appears to the end of the corresponding end-tag, excluding the scope of any inner default namespace declarations.

And also includes this example:

<!-- initially, the default namespace is "books" -->
<book xmlns='urn:loc.gov:books'
      xmlns:isbn='urn:ISBN:0-395-36341-6'>
    <title>Cheaper by the Dozen</title>
    <isbn:number>1568491379</isbn:number>
    <notes>
      <!-- make HTML the default namespace for some commentary -->
      <p xmlns='http://www.w3.org/1999/xhtml'>
          This is a <i>funny</i> book!
      </p>
    </notes>
</book>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.