开发者

XML DOM conflicting namespace issues

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">
  <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</uri>
    <email>libx.org@gmail.com</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 namespac开发者_JS百科e 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


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).
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜