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

How to determine if two XML files have the same structure even if the tags have different values?

我想比较两个 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>