1

Are the two dog elements in the following XML fragments identical?

fragment 1:

<animals>
      <animals:dog xmlns:animals="urn:animals">
             The dog was the first species to be domesticated
      </animals:dog>
</animals>

fragment 2:

<animals>
      <tiere:dog xmlns:tiere="urn:animals">
              The dog was the first species to be domesticated
     </tiere:dog>
</animals>

I got two different answers. Tell me the right one. For me may be identical.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147

2 Answers2

1

There were some whitespace differences between those two examples that make them NOT equal. Assuming that those were just formatting mistakes when posting the document and the question is really about the difference in the namespace-prefix, then yes they are equal.

You can use the function fn:deep-equal() to test. Below is an XQuery example:

let $a := 
<animals>
      <animals:dog xmlns:animals="urn:animals">
             The dog was the first species to be domesticated
      </animals:dog>
</animals>
let $b :=
<animals>
      <tiere:dog xmlns:tiere="urn:animals">
             The dog was the first species to be domesticated
      </tiere:dog>
</animals>
return 
  fn:deep-equal($a, $b)

Should anyone care what namespace-prefix is used in an instance document? 99.99% of the time, no. If they are using proper tooling and XML APIs, it will not matter. You address elements by their QName, and as long as the namespace and the local-name are correct, it doesn't matter which namespace-prefix was used.

If they are string parsing angle brackets and the namespace-prefix causes problems, then they are doing something wrong and should fix their program.

Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
0

The word "identical" is very loaded, and you have to say exactly what you mean by it.

Given the input <doc><foo/><foo/></doc>, the XPath expression foo[1] is foo[2] returns false; the two foo nodes are NOT identical, in that sense of the word. It's not only the "is" operator that distinguishes them, there are other expressions that return different results for the two nodes, for example count($foo/following-sibling::*).

When it comes to your question about namespace prefixes, it's generally recommended good practice that an application that reads XML should work the same way regardless what namespace prefix is used. But saying it's recommended good practice to treat the two nodes as equivalent isn't the same as saying they are identical.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164