合并 XML 文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/80609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Merge XML documents
提问by Manrico Corazzi
I need to "merge" two XML documents, overwriting the overlapsed attributes and elements. For instance if I have document1:
我需要“合并”两个 XML 文档,覆盖重叠的属性和元素。例如,如果我有document1:
<mapping>
<key value="assigned">
<a/>
</key>
<whatever attribute="x">
<k/>
<j/>
</whatever>
</mapping>
and document2:
和文件2:
<mapping>
<key value="identity">
<a/>
<b/>
</key>
</mapping>
I want to merge the two like this:
我想像这样合并两者:
<mapping>
<key value="identity">
<a/>
<b/>
</key>
<whatever attribute="x">
<k/>
<j/>
</whatever>
</mapping>
I prefer Javaor XSLT-based solutions, antwill do fine, but if there's an easy way to do that in Rake, Rubyor Pythonplease don't be shy :-)
我更喜欢基于 Java或XSLT的解决方案,ant会很好,但如果在Rake、Ruby或Python 中有一种简单的方法可以做到这一点,请不要害羞:-)
EDIT:actually I find I'd rather use an automated tool/script, even writing it by myself, because manually merging some 30 XML files is a bit unwieldy... :-(
编辑:实际上我发现我宁愿使用自动化工具/脚本,甚至自己编写它,因为手动合并大约 30 个 XML 文件有点笨拙...... :-(
采纳答案by Craig.Nicol
If you like XSLT, there's a nice merge script I've used before at: Oliver's XSLT page
如果您喜欢 XSLT,我以前在以下位置使用过一个不错的合并脚本: Oliver 的 XSLT 页面
回答by Craig.Nicol
I know this is an old thread, but Project: Mergecan do this for you. It can merge two XML files together, and can be run from the command line, so you can batch everything up together, run it and just resolve any conflicts (such as the changing attribute value of 'key' in your above example) manually with a few clicks. (You can tell it to run silently providing there are no conflicts.)
我知道这是一个旧线程,但是Project: Merge可以为您做到这一点。它可以将两个 XML 文件合并在一起,并且可以从命令行运行,因此您可以将所有内容一起批处理,运行它并手动解决任何冲突(例如上面示例中“key”的更改属性值)点击几下。(如果没有冲突,您可以告诉它静默运行。)
It can perform two-way and three-way comparisons of XML files and two-way and three-way merges. (Where a three-way operation assumes the two files being compared/merged have a common ancestor.)
它可以进行XML文件的双向和三向比较以及双向和三向合并。(三向操作假设被比较/合并的两个文件有一个共同的祖先。)
回答by S?awek
Check XmlCombinerwhich is a Java library that implements XML merging in exactly this way. It is loosely based on a similar functionality offered by plexus-utilslibrary.
检查XmlCombiner,它是一个以这种方式实现 XML 合并的 Java 库。它松散地基于plexus-utils库提供的类似功能。
XmlCombiner default convention is to overwrite the overlapping attributes and elements. But the exact merging behavior can be altered using special 'combine.self' and 'combine.children'attributes.
XmlCombiner 默认约定是覆盖重叠的属性和元素。但是可以使用特殊的“combine.self”和“combine.children”属性来改变确切的合并行为。
Usage:
用法:
import org.atteo.xmlcombiner.XmlCombiner;
// create combiner
XmlCombiner combiner = new XmlCombiner();
// combine files
combiner.combine(firstFile);
combiner.combine(secondFile);
// store the result
combiner.buildDocument(resultFile);
Disclaimer: I am the author.
免责声明:我是作者。
回答by mwallner
(also using Oliver's XSLT stlyesheets)
(也使用Oliver 的 XSLT stlyesheets)
XSLT merge from PowerShell:
来自 PowerShell 的 XSLT 合并:
param(
[Parameter(Mandatory = $True)][string]$file1,
[Parameter(Mandatory = $True)][string]$file2,
[Parameter(Mandatory = $True)][string]$path
)
# using only abs paths .. just to be safe
$file1 = Join-Path $(Get-Location) $file1
$file2 = Join-Path $(Get-Location) $file2
$path = Join-Path $(Get-Location) $path
# awesome xsl stylesheet from Oliver Becker
# http://web.archive.org/web/20160502194427/http://www2.informatik.hu-berlin.de/~obecker/XSLT/merge/merge.xslt
$xsltfile = Join-Path $(Get-Location) "merge.xslt"
$XsltSettings = New-Object System.Xml.Xsl.XsltSettings
$XsltSettings.EnableDocumentFunction = 1
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xslt.Load($xsltfile , $XsltSettings, $(New-Object System.Xml.XmlUrlResolver))
[System.Xml.Xsl.XsltArgumentList]$al = [System.Xml.Xsl.XsltArgumentList]::new()
$al.AddParam("with", "", $file2)
$al.AddParam("replace", "", "true")
[System.Xml.XmlWriter]$xmlwriter = [System.Xml.XmlWriter]::Create($path)
$xslt.Transform($file1, $al, $xmlwriter)
Using 'plain ol' Saxon:
使用 'plain ol' 撒克逊语:
java -jar saxon9he.jar .\FileA.xml .\merge.xslt with=FileB.xml replace=true
回答by moobaa
Unsure as to whether you want to do this programatically or not.
不确定是否要以编程方式执行此操作。
Edit: Ah, I posted that before the Edit. Don't I look like an idiot now! ;)
编辑:啊,我在编辑之前发布了。我现在看起来是不是像个白痴!;)
If you just want to merge two files together, IBM have an XML Diff and Merge Tool, and there's also Altova's DiffDog.
如果您只想将两个文件合并在一起,IBM 有一个XML Diff and Merge Tool,还有 Altova 的DiffDog。

