New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Riyavanwari opened this Issue Jul 23, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@Riyavanwari

Riyavanwari commented Jul 23, 2018

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;
        }
    }

@Riyavanwari Riyavanwari changed the title from Question regarding ignore ordering - XML file to ElementSelectors.byXPath Selector fails to find a match - XML file Jul 24, 2018

@bodewig bodewig added the question label Jul 27, 2018

@bodewig

This comment has been minimized.

Show comment
Hide comment
@bodewig

bodewig Jul 27, 2018

Member

Every time XPath and Namespaces are used together I need to re-lookup how they interact and then forget about it immediately. Your XPath ./SequenceID didn't match any nodes at all (so the Report elements were used in order). Using a slightly modified version of

        Map<String, String> m = new HashMap<>();
        m.put("foo", "Example");
...
            .thenUse(ElementSelectors.byXPath("./foo:SequenceID", m, ElementSelectors.byNameAndText))

makes the node matcher select the proper Report elements.

Member

bodewig commented Jul 27, 2018

Every time XPath and Namespaces are used together I need to re-lookup how they interact and then forget about it immediately. Your XPath ./SequenceID didn't match any nodes at all (so the Report elements were used in order). Using a slightly modified version of

        Map<String, String> m = new HashMap<>();
        m.put("foo", "Example");
...
            .thenUse(ElementSelectors.byXPath("./foo:SequenceID", m, ElementSelectors.byNameAndText))

makes the node matcher select the proper Report elements.

@bodewig bodewig closed this Aug 10, 2018

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