【问题标题】:How to determine if two XML files have the same structure even if the tags have different values?即使标签具有不同的值,如何确定两个 XML 文件是否具有相同的结构?
【发布时间】:2019-12-08 19:38:16
【问题描述】:

我希望比较两个 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();
}

【问题讨论】:

    标签: java xml dom xml-parsing xmlunit


    【解决方案1】:

    试试这个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多