1

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?

sergpank
  • 988
  • 10
  • 18

2 Answers2

2

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

imhotap
  • 2,275
  • 1
  • 8
  • 16
2

You can maintain a single XSLT script which generates the multiple configuration files, something like this:

<xsl:transform version='3.0' expand-text='true'....>

<xsl:variable name="outputs" as="element(output)*">
  <output file="config1.xml" os="mac" db="oracle"/>
  <output file="config2.xml" os="windows" db="mysql"/>
</xsl:variable>

<xsl:template name="main">
  <xsl:for-each select="$outputs">
    <xsl:result-document href="{$file}">
      <config>
        <aspiration>high</aspiration>
        <perspiration>low</perspiration>
        <condensation>{if (@db='oracle') then 'moderate' else 'none'}</condensation>
        <xsl:if test="@os='linux'>
          <permutation>inverse</permutation>
        </xsl:if>
     </config>
    </xsl:result-document>
  </xsl:for-each>
</xsl:template>

</xsl:transform>

Then when there's a change, just edit the stylesheet and rerun it.

Alternatively, you can create a configuration file with conditional markup, like this:

<config>
  <perspiration>high</perspiration>
  <exhumation for-os="linux">mediocre</exhumation>
  <exhumation for-os="windows">none</exhumation>
  ...
</config>

and then write a stylesheet to subset this by deleting the elements that don't apply.

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