Linux的开源命令行工具,用于忽略元素顺序来区分XML文件 [英] Open source command line tool for Linux to diff XML files ignoring element order

查看:113
本文介绍了Linux的开源命令行工具,用于忽略元素顺序来区分XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个开放源代码命令行工具(适用于Linux)来区分忽略元素顺序的XML文件?

Is there an open source command-line tool (for Linux) to diff XML files which ignores the element order?

示例输入文件a.xml:

<tag name="AAA">
  <attr name="b" value="1"/>
  <attr name="c" value="2"/>
  <attr name="a" value="3"/>
</tag>

<tag name="BBB">
  <attr name="x" value="111"/>
  <attr name="z" value="222"/>
</tag>
<tag name="BBB">
  <attr name="x" value="333"/>
  <attr name="z" value="444"/>
</tag>

b.xml:

<tag name="AAA">
  <attr name="a" value="3"/>
  <attr name="b" value="1"/>
  <attr name="c" value="2"/>
</tag>

<tag name="BBB">
  <attr name="z" value="444"/>
  <attr name="x" value="333"/>
</tag>
<tag name="BBB">
  <attr name="x" value="111"/>
  <attr name="z" value="222"/>
</tag>

因此比较这两个文件不应输出任何差异. 我尝试过先使用XSLT对文件进行排序:

So comparing these 2 files should not output any differences. I have tried to sort the files with XSLT first:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" encoding="WINDOWS-1252" omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*">
        <xsl:sort select="@*" />
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

但是问题是元素<tag name="BBB">没有排序.它们只是按输入顺序输出.

But the problem is that for the elements <tag name="BBB"> there is no sorting. They are simply output the order which they are input.

我已经看过diffXmlxDiffXMLUnitxmlstarlet,但是这些都不能解决问题. diff输出应该是人类可读的,例如就像使用diff时一样.

I have already looked at diffXml, xDiff, XMLUnit, xmlstarlet but none of these solve the problem; the diff output should be human readable, e.g. like when using diff.

关于如何解决排序或忽略元素顺序差异的任何提示?谢谢!

Any hints on how either the sorting or ignoring element-order diff can be solved? Thanks!

推荐答案

我遇到了类似的问题,最终发现:

I had a similar problem and I eventually found: https://superuser.com/questions/79920/how-can-i-diff-two-xml-files

该帖子建议先进行规范的xml排序,然后再进行比较.由于您使用的是Linux,因此应该可以正常使用.它适用于我的Mac,如果安装了cygwin之类的软件,则适用于Windows上的人:

That post suggests doing a canonical xml sort then doing a diff. Being that you are on linux, this should work for you cleanly. It worked for me on my mac, and should work for people on windows if they have something like cygwin installed:

$ xmllint --c14n a.xml > sortedA.xml
$ xmllint --c14n b.xml > sortedB.xml
$ diff sortedA.xml sortedB.xml

这篇关于Linux的开源命令行工具,用于忽略元素顺序来区分XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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