XML配置继承,避免重复 [英] XML configuration inheritance, avoid duplications

查看:85
本文介绍了XML配置继承,避免重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

设想一种情况,当您有大约10个以上的大型XML配置文件用于几乎完全相同的多个环境时.

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

有时您必须添加一个新选项,以对所有具有相同数据的10..20..30 +文件进行修改.

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.

是否有任何好的工具可以为纯XML文件(而不是Spring.cgf或POM)提供类似继承的功能?

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?

推荐答案

您可以使用实体/实体引用在文件内或文件间的多个位置引用和重用内容.

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

声明和使用实体的XML文件如下所示:

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>

此XML文件的处理方式就像

This XML file is handled as if

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

已指定

.也就是说,实体引用&myent;被其替换文本<x>Text content of myent</x>代替.

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

这也适用于存储在外部文件(称为外部实体)中的替换文本.假设上面的文件存储为doc.xml,则可以从另一个XML文件中引用它,如下所示:

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>

,并且XML解析器会将其视为

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>

已被指定.

使用实体,您可以组织通用XML内容,而在文件内或文件之间没有冗余.希望有帮助.

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

请注意,您必须修改DOCTYPE-它必须与XML的document元素匹配

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

这篇关于XML配置继承,避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆