XML 配置继承,避免重复 [英] XML configuration inheritance, avoid duplications
问题描述
想象一下,当您有大约 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)?
或者我必须自己编写一个 ant 或 maven 自行车脚本?
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>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 的文档元素相匹配
note that you have to adapt the DOCTYPE - it must match the document element of your XML
这篇关于XML 配置继承,避免重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!