xmlmavenant

XML configuration inheritance, avoid duplications


Imagine a situation when you have about 10+ big XML configuration files for multiple environments which are almost identical.

Sometimes you have to add a new option, which leads to modification of all those 10..20..30+ files with the same data.

You make mistakes. You get conflicts when merging with other branches. You got nervous and depressed.

Are there any good tools that provide something like inheritance for plain XML files (not Spring.cgf or POM)?

Or I have to write an ant or maven bicycle-script on my own?


Solution

  • You can use entities/entity references for referencing and reusing content at multiple places, within or across files.

    An XML file declaring and using an entity looks like this:

    <!DOCTYPE doc [
      <!ENTITY myent "<x>Text content of myent</x>">
    ]>
    <doc>
      &myent;
      &myent;
    </doc>
    

    This XML file is handled as if

    <doc>
      <x>Text content of myent</x>
      <x>Text content of myent</x>
    </doc>
    

    had been specified. That is, the entity reference &myent; is substituted by its replacement text <x>Text content of myent</x>.

    This works also with replacement text stored in an external file (called an external entity). Assuming the file above is stored as doc.xml, you can reference it from another XML file like this:

    <!DOCTYPE otherdoc [
      <!ENTITY doc SYSTEM "doc.xml">
    ]>
    <otherdoc>
      &doc;
    </otherdoc>
    

    and an XML parser will treat it as if

    <otherdoc>
      <doc>
        <x>Text content of myent</x>
        <x>Text content of myent</x>
      </doc>
    </otherdoc>
    

    had been specified.

    Using entities, you can organize common XML content without redundancy within or accross files. Hope that helps.

    Edit: note that you have to adapt the DOCTYPE - it must match the document element of your XML