Title: Ignore a multiple occurrence of a tag while comparing using xmlUnit
Tags: regex;xml;xmlunit
Question: I have an XML file which looks like below.

Expected XML

```<doc&gt;
<tag&gt;
    <file&gt;a.c</file&gt;
    <line&gt;10</line&gt;
    <type&gt;c</type&gt;
<tag&gt;
<tag&gt;
    <file&gt;b.h</file&gt;
    <line&gt;14</line&gt;
    <type&gt;h</type&gt;
<tag&gt;
<tag&gt;
    <file&gt;d.he</file&gt;
    <line&gt;49</line&gt;
    <type&gt;he</type&gt;
<tag&gt;
</doc&gt;
```

Now XML for testing

```<doc&gt;
<tag&gt;
    <file&gt;a1.c</file&gt;
    <line&gt;10</line&gt;
    <type&gt;c</type&gt;
<tag&gt;
<tag&gt;
    <file&gt;b1.h</file&gt;
    <line&gt;14</line&gt;
    <type&gt;h</type&gt;
<tag&gt;
<tag&gt;
    <file&gt;d1.he</file&gt;
    <line&gt;49</line&gt;
    <type&gt;he</type&gt;
<tag&gt;
</doc&gt;
```

I want to compare this file with another XML file which has same structure.

I am using xmlUnit for comparison. While comparing I want to ignore XML tag ```<file&gt;```

Below is the comparison code which I wrote

```public static Diff compareXML(String expXMLPath, String genXMLPath)
        throws IOException, SAXException {
    XMLUnit.setIgnoreWhitespace(true);
    XMLUnit.setIgnoreAttributeOrder(true);
    final List<String&gt; ignorableXPathsRegex = new ArrayList<String&gt;();// list of regular expressions that custom difference listener used during xml 
    //comparison                                                                                
    ignorableXPathsRegex
            .add("\\/doc\\[1\\]\\/tag\\[1\\]\\/file\\[1\\]\\/text()");        
    Diff diff = null;
    try(FileInputStream fileStream1 = new FileInputStream(expXMLPath)) {
        try(FileInputStream fileStream2 = new FileInputStream(genXMLPath)) {
            InputSource inputSource1 = new InputSource(fileStream1);
            InputSource inputSource2 = new InputSource(fileStream2);
            diff = new Diff(inputSource1, inputSource2);
            RegDiffListener ignorableElementsListener = new RegDiffListener(
                    ignorableXPathsRegex);
            diff.overrideDifferenceListener(ignorableElementsListener);
            return diff;
        }
    }                
}
```

This is not working if XML file has more than one ```<tag&gt;...</tag&gt;``` block. I basically need a regex here which ignores all the ```<file&gt;``` tag which are under ```<doc&gt;<tag&gt;```

I want the comparison of expected and test XML to show that both are same by ignoring the value of file tag, so ```diff.similar()``` should return ```true```

Please suggest how to do it.

Comment: I found the solution. ignorableXPathsRegex
            .add("\\/doc\\[1\\]\\/tag\\[1\\]\\/file\\[1\\]\\/text()"); tells to check only for the first tag. We should use ignorableXPathsRegex
            .add("\\/doc\\[1\\]\\/tag\\[\\d*\\]\\/file\\[1\\]\\/text()"); to ignore all the  inside all the 

Comment: It would be easier to understand your goal if you added the expected result compared to your file structure.


Here is another answer: I found the solution.

```ignorableXPathsRegex .add("\\/doc\[1\]\\/tag\[1\]\\/file\[1\]\\/text()");
```

tells to check only for the first tag.

We should use

```ignorableXPathsRegex .add("\\/doc\[1\]\\/tag\[\\d+\]\\/file\[1\]\\/text()");
```

to ignore all the ```<file&gt;``` inside all the ```<tag&gt;```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2015:00:07/22/15', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<regex><xml><xmlunit>', 'Title': 'Ignore a multiple occurrence of a tag while comparing using xmlUnit', 'ViewCount': 268, 'id': 31554801, 'user_AboutMe': None, 'user_AccountId': 4336019.0, 'user_DisplayName': 'user3540481', 'user_Reputation': 221.0, 'user_Views': 46.0, 'user_id': 3540481.0}}