0

I was considering using xml namespaces in my configuration file. If I tag the config file content using a namespace then options for a program version are identified.

Is there a way to select all the nodes with a specific namespace version number and lower? This allows any version of the app to read any version configuration file without change.

Example:

  <ns1:config xmlns:ns1="version 1.0">
     <ns1:somesetting>blah</ns1:somesetting>
     <ns2:SomeNewSetting xmlns:ns2="version 2.0">foob</ns2:SomeNewSetting>
  </ns1:config>

Version 1 of the app selects namespace = "version 1.0".

Version 2 of the app selects namespace = "version 1.0" or namespace = "version 2.0".

Is there are better method than the brute force solution?

Jay
  • 13,803
  • 4
  • 42
  • 69

1 Answers1

1

Using namespaces to manage versioning of an XML vocabulary is generally considered a bad idea. One of the reasons is well illustrated by your question: namespaces are not ordered. It's much better to use a version attribute. Dynamic selection of elements based on the value of an attribute is much easier than dynamic selection by namespace.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Why is it considered "bad"? It appears to be the reason they were created. The version attribute is much simpler but bloats the document. – Jay Apr 01 '18 at 14:43
  • 1
    See for example https://stackoverflow.com/questions/2014237/what-are-the-best-practices-for-versioning-xml-schemas – Michael Kay Apr 01 '18 at 21:12
  • After having read the pundits discussion of the issue the version attribute does not ensure backward compatibility unless the first version is written with compatibility in mind. If the first version doesn't respect the version attribute it will be exposed to multiple nodes. I don't see either as having any benefit that puts them head and shoulders above the other – Jay Apr 03 '18 at 17:23
  • There's no perfect answer to the version problem. But remember, if the changes in the new version are small, then with a change in version attribute most recipients need to make zero change, but with a change in namespace, every recipient needs to change, and the changes may be extensive. – Michael Kay Apr 03 '18 at 21:32