Jibin Balachandran
Jibin Balachandran

Reputation: 3441

How to compare 2 xml files to know if its the same or not

I want to compare two XML files and want to know if both the files are same or not. But I want to ignore the inner text difference. It should say it's different when there's a difference in structure i.e some new tags are added.

I tried XML Diff, But I didn't get an option to ignore the inner text in the XmlDiffOptions.

I even tried the answers in how-would-you-compare-two-xml-documents, but didn't worked for me.

Upvotes: 0

Views: 1744

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

Generally the simplest way to "compare two XML files while ignoring ZZZ" is to transform both files to eliminate ZZZ, and then use a standard comparison method (for example the XPath deep-equal() function, or canonicalization followed by string comparison).

In your case if ZZZ is "the content of text nodes", transform both files using XSLT to eliminate text nodes, and then compare. For example (XSLT 2.0):

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

<xsl:function name="f:prep" as="document-node">
  <xsl:param name="doc" as="document-node()"/>
  <xsl:apply-templates/>
</xsl:function>

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:apply-templates/>
  </xsl:copy>
</xsl:template>

<xsl:template match="text()"/>

Upvotes: 2

Related Questions