Skip to content

ElementSelectors.byXPath Selector fails to find a match - XML file #128

Description

@Riyavanwari

Hello Sir,

We have been using your library and it has very good documentation, usage examples and good support. I am running into this issue with the out of order elements. I really appreciate your response.

I am having issues with the comparison of these files, although these files are same but the elements are not in the same order. I am using ElementSelectors.byXPath for finding the match against the comparing XML. Do i need to include any other further selectors?
Here the "Report" elements are out of order and there are multiple Report elements in the XML, each Report element has a SequenceID which is unique. I wrote the Xpath based on that. I would really app

XML file 1 
<?xml version="1.0" encoding="UTF-8"?>
<par:Response xmlns:par="http://www.cmc.com/rules/param">
<par:Decision>c12</par:Decision>
<ns0:Files xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="Example">
<Information xmlns="Example">
<ReportId>600137023</ReportId>
</Information>
<Report xmlns="Example">
<Name>FGH</Name>
<SequenceID>503445</SequenceID>
<RequestHeader>
<FileID>22</FileID>
</RequestHeader>
<DataElements>
<DataElement>
<ID>1</ID>
<History>
<Age>1</Age>
<Gender>F</Gender>
</History>
<History>
<Age>2</Age>
<Gender>M</Gender>
</History>
<History>
<Age>4</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>2</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>3</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
</DataElements>
<Scores>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>1</Identifier>
<Code>H12</Code>
</Score>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>2</Identifier>
<Code>H13</Code>
</Score>
</Scores>
</Report>
<Report xmlns="Example">
<Name>ABC</Name>
<SequenceID>500222000</SequenceID>
<RequestHeader>
<FileID>12</FileID>
</RequestHeader>
<DataElements>
<DataElement>
<ID>1</ID>
<History>
<Age>1</Age>
<Gender>F</Gender>
</History>
<History>
<Age>2</Age>
<Gender>M</Gender>
</History>
<History>
<Age>4</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>2</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
</DataElements>
<Scores>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>1</Identifier>
<Code>H12</Code>
</Score>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>2</Identifier>
<Code>H13</Code>
</Score>
</Scores>
</Report>
</ns0:Files>
</par:Response>


XML 2:
<?xml version="1.0" encoding="UTF-8"?>
<par:Response xmlns:par="http://www.cmc.com/rules/param">
<par:Decision>c12</par:Decision>
<ns0:Files xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="Example">
<Information xmlns="Example">
<ReportId>600137023</ReportId>
</Information>
<Report xmlns="Example">
<Name>ABC</Name>
<SequenceID>500222000</SequenceID>
<RequestHeader>
<FileID>12</FileID>
</RequestHeader>
<DataElements>
<DataElement>
<ID>1</ID>
<History>
<Age>1</Age>
<Gender>F</Gender>
</History>
<History>
<Age>2</Age>
<Gender>M</Gender>
</History>
<History>
<Age>4</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>2</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
</DataElements>
<Scores>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>1</Identifier>
<Code>H12</Code>
</Score>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>2</Identifier>
<Code>H13</Code>
</Score>
</Scores>
</Report>
<Report xmlns="Example">
<Name>FGH</Name>
<SequenceID>503445</SequenceID>
<RequestHeader>
<FileID>22</FileID>
</RequestHeader>
<DataElements>
<DataElement>
<ID>1</ID>
<History>
<Age>1</Age>
<Gender>F</Gender>
</History>
<History>
<Age>2</Age>
<Gender>M</Gender>
</History>
<History>
<Age>4</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>2</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
<DataElement>
<ID>3</ID>
<History>
<Age>5</Age>
<Gender>F</Gender>
</History>
<History>
<Age>6</Age>
<Gender>M</Gender>
</History>
<History>
<Age>7</Age>
<Gender>5</Gender>
</History>
</DataElement>
</DataElements>
<Scores>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>1</Identifier>
<Code>H12</Code>
</Score>
<Score>
<Type>MYE</Type>
<Value>768</Value>
<Identifier>2</Identifier>
<Code>H13</Code>
</Score>
</Scores>
</Report>
</ns0:Files>
</par:Response>
My Code base: 
 public static boolean compare( String file1, String file2) {
      
        Diff myDiff = DiffBuilder.compare(file1)
                .withTest(file2)
                .ignoreElementContentWhitespace()
                .withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default,
                 DifferenceEvaluators.downgradeDifferencesToSimilar(ComparisonType.NAMESPACE_URI)))
                .checkForSimilar()
                .withNodeMatcher(
                        new DefaultNodeMatcher(ElementSelectors.conditionalBuilder().whenElementIsNamed("Report")
                                .thenUse(ElementSelectors.byXPath("./SequenceID", ElementSelectors.byNameAndText))
                                .whenElementIsNamed("Score")
                                .thenUse(ElementSelectors.byXPath("./Identifier", ElementSelectors.byNameAndText))
                                .elseUse(ElementSelectors.byNameAndText).build())).build();

      
        
        if (myDiff.hasDifferences()) {
            Iterator<Difference> iter = myDiff.getDifferences().iterator();
            String msg = "";
                while (iter.hasNext()) {
                Difference D = iter.next();
                msg = D.getComparison().toString();
                
                logger.info("" + msg + "=>" + D.getResult().toString());
          }
            return false;
        } else {
            
            return true;
        }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions