views:

27

answers:

1

Hi,

I am using XML DOM documents with two namespaces. For example, consider the following document:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:libx="http://libx.org/xml/libx2"&gt;
  <id>5</id>
  <title>Put Google Book results into Addison</title>
  <updated>2009-02-23T10:12:15Z</updated>
  <author>
    <name>LibX Team</name>
    <uri>http://libx.org&lt;/uri&gt;
    <email>[email protected]</email>
  </author>
  <libx:libapp>
    <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="6"/>
    <libx:entry xmlns:libx="http://libx.org/xml/libx2" src="7"/>
  </libx:libapp>
</entry>

I have the following doubts with respect to namespaces:

1) If I don't prefix the namespace to the nodes(id, title, author etc.) as in the above example, will they be assigned the default namespace: "http://www.w3.org/2005/Atom" ? If not, which namespace would they be assigned to ?

2) The getNameSpaceURI method of a Node returns - The namespace URI of the node, or null if it is unspecified. But, how do I get to know the namespace of the node if it is unspecified. Meaning is there a way to know whether default namespace is assigned to the node or if some other namespace is assigned?

3) When, I copy the nodes(with default namespaces) of a particular document into another document, will the nodes retain the original namespaces or will they take up the default namespace of the new document into which these nodes are copied ? If the latter is true, how do I retain the original namespaces ?

Lastly, could someone point me to a good online material to understand these conflicting namespace issues better?

Thanks, Sidhartha

A: 

For namespace tutorials see this, this. and this

Now for your questions:

  1. With xmlns you declare the default namespace -> if you don't prefix an element the default namespace will be searched for the element definition.
  2. If my understanding is correct, for a non prefixed node getNameSpaceURI will return the default namespace (or null if no default namespace is declared).
  3. The solution depends on what tools (XSLT, .net wrappers, etc) you are using to do the copy. If you what to do the copy using .net take a look at this tutorial (important node: to correctly move a node use ImportNode).
Ando