gpt4 book ai didi

java - 即使标签具有不同的值,如何确定两个 XML 文件是否具有相同的结构?

转载 作者:行者123 更新时间:2023-12-02 02:18:35 24 4
gpt4 key购买 nike

我希望比较两个 XML 文件并确定它们是否具有相同的结构,即相同类型和数量的标签,最好具有相同的属性。标签和属性的值可能不同。

此代码检测所有差异。即使结构相同但值不同。我想改进它以仅检测结构差异。

public static List compareXML(Reader source, Reader target) throws
SAXException, IOException{

//creating Diff instance to compare two XML files
Diff xmlDiff = new Diff(source, target);

//for getting detailed differences between two xml files
DetailedDiff detailXmlDiff = new DetailedDiff(xmlDiff);

return detailXmlDiff.getAllDifferences();
}

最佳答案

尝试这个 XSLT 3.0:

<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="text()"/>
<xsl:template match="@*">
<xsl:attribute name="name()"/>
</xsl:template>

<xsl:variable name="doc1">
<xsl:apply-templates select="doc('one.xml')"/>
</xsl:variable>

<xsl:variable name="doc2">
<xsl:apply-templates select="doc('two.xml')"/>
</xsl:variable>

<xsl:template name="xsl:initial-template">
<xsl:value-of select="deep-equal($doc1, $doc2)"/>
</xsl:template>

关于java - 即使标签具有不同的值,如何确定两个 XML 文件是否具有相同的结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com