在比较xml文件时,我需要在比较中添加一条规则。我需要忽略以包含单词exclude="true"
的内容开头的节点
并以</File>
<File name="LogData.txt" exclude="true" type="TIMESTAMP">
<Date>2012-09-27 17:43:36.211</Date>
</File>
我已设法为我的示例实例创建一个规则,如下所示:
从:<File name="LogData.txt" exclude="true"
开头
结束于:</File>
但是我需要一些更通用的东西,它会忽略任何包含exclude =“true”属性的File节点。
答案 0 :(得分:0)
Text from-expression <File (.*)exclude="true"
让.*
尽可能频繁地表达任何字符
另一种解决方案是使用外部转换。
我写了这样的e litte ruby-script:
File.open(ARGV.last, "w"){|f|
f.write(File.read( ARGV.first).gsub( %r{<(.+?)\s[^>]+exclude="true".*?>(.*?)</\1>}m, ""))
}
它将第一个参数作为源文件名,并将转换后的xml写入目标文件。
此脚本可用作转换工具:
而不是脚本,我也成功使用了一个班轮
如果您将脚本改编为
File.open(ARGV.last, "w"){|f|
f.write(File.read( ARGV.first).gsub( %r{(<(.+?)\s[^>]+exclude="true".*?>(.*?)</\2>)}m, "<!--\n\\1\n-->"))
}
排除的部分未被删除,但设置为评论(<!-- ... -->
),您会得到:
差异现在是一个不重要的差异(蓝色而不是红色),你可能会忽略这种差异。
说明: *如果你想使用我的脚本,你需要红宝石。 *如果需要,您可以用其他语言编写转换脚本。 *我使用的正则表达式未经过充分测试。嵌套标签可能有问题。