1

Are the following XML documents technically the same?

<sampledata xmlns="http://www.mycompany.com"> <firstname>James</firstname> <lastName>Dean</lastName> <age>19</age> </sampledata> 

and:

<ns2:sampledata xmlns:ns2="http://www.mycompany.com"> <ns2:firstname>James</firstname> <ns2:lastName>Dean</lastName> <ns2:age>19</age> </ns2:sampledata> 
0

2 Answers 2

1

These two documents are semantically identical. In XML, there are two things to think about: the namespace URI (the "http://..." part) and the alias (the "ns2" part.)

When querying your XML document, in either document, if you try to find "sampledata" without assigning a namespace to the node, you won't find it.

In .NET's System.Xml.XmlNode classes, you have to set up your namespace aliases before you can query anything. Technically, when querying the DOM, there's no reason why you couldn't create an alias "foo" and assign it the URI "http://www.mycompany.com". With that setup, querying for "foo:sampledata" will return the node in either document.

 XmlDocument doc1 = new XmlDocument(); doc1.LoadXml(@" <sampledata xmlns=""http://www.mycompany.com""> <firstname>James</firstname> <lastName>Dean</lastName> <age>19</age> </sampledata>"); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml(@" <ns2:sampledata xmlns:ns2=""http://www.mycompany.com""> <ns2:firstname>James</ns2:firstname> <ns2:lastName>Dean</ns2:lastName> <ns2:age>19</ns2:age> </ns2:sampledata>"); XmlNamespaceManager nsMgr = new XmlNamespaceManager(new NameTable()); nsMgr.AddNamespace("foo", "http://www.mycompany.com"); int count1 = doc1.SelectNodes("foo:sampledata", nsMgr).Count; int count2 = doc2.SelectNodes("foo:sampledata", nsMgr).Count; 

In this example, both count1 and count2 are 1.

Sign up to request clarification or add additional context in comments.

Comments

0

No, they are technically not.

  • The default namespace for the second document is an empty string.
  • The first document is not aware about the ns2 prefix.

This can be fixed by making sure both xmlns:ns2="http://www.mycompany.com" and xmlns="http://www.mycompany.com" are present in both documents, despite only one will be used in each.

However, in terms of querying these XMLs, yes, they are identical.
Well, not exactly.
The nodes will be found, but the output format will be different (at least, using .NET xml thingies). For instance, if you query these two documents for "http://www.mycompany.com":firstname, you will get

<firstname xmlns="http://www.mycompany.com">James</firstname> 

and

<ns2:firstname xmlns:ns2="http://www.mycompany.com">James</ns2:firstname> 

respectively.

But if you make sure both xmlns and xmlns:ns2 are known to both documents, then the output format will be the same:

<firstname xmlns="http://www.mycompany.com">James</firstname> <firstname xmlns="http://www.mycompany.com">James</firstname> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.