Menu

Compare two xml files using attribute reference

Help
Fabio Nepi
2015-03-23
2015-04-02
  • Fabio Nepi

    Fabio Nepi - 2015-03-23

    Hi,

    i'd like compare two xml files with these following features

    // control.xml

    <ela id="user-content-01">
    <..>...</..>
    </ela>

    <elb id="user-content-02">
    <..>...</..>
    </elb>

    <elc elaref="01" elbref="02">
    <..>...</..>
    </elc>


    // test.xml

    <ela id="user-content-05">
    <..>...</..>
    </ela>

    <elb id="user-content-06">
    <..>...</..>
    </elb>

    <elc elaref="05" elbref="06">
    <..>...</..>
    </elc>


    The element <elc> have two reference to elements <ela> and <elb>. Doesn't matter that id's attributes are identical so i've created my custom DifferenceListener that ignore difference about different id, elaRef and elbRef.</elb></ela></elc>

    The problem is that i'd like compare two content's elements <elc> and its content's reference ignoring
    id value attribute.</elc>

    I've seen the function XMLUnit.setExpandEntityReferences(Boolean.TRUE); could help me, but i'm not sure to had understood the real behavior of it.

    Can you help me?

     

    Last edit: Fabio Nepi 2015-03-23
  • Fabio Nepi

    Fabio Nepi - 2015-03-23

    I've solved it editing my DifferenceListener in this way:

    1) I've declared following variables for my class that implements DifferenceListener:

    private static Map<String,String> map;
    private final int ATTR_VALUE = DifferenceConstants.ATTR_VALUE.getId();
    

    2) I've editing my method isIgnoredDifference(Difference difference) returning a boolean:

    int differenceId = difference.getId();
    NodeDetail control = difference.getControlNodeDetail();
    NodeDetail test = difference.getTestNodeDetail();
    
    if(differenceId==ATTR_VALUE){
        String attrName = difference.getControlNodeDetail().getNode().getNodeName();
        if(attrName.equalsIgnoreCase("id")){
               map.put(control.getValue(), test.getValue());
                    return true;
                    }           
        if(attrName.equalsIgnoreCase("unitRef") || attrName.equalsIgnoreCase("contextRef")){
               if(map.get(control.getValue()).equalsIgnoreCase(test.getValue()))
                    return true;
       }
     }
     return false;
    

    }

    but I'd like know if exists a more efficient method to get it.

    Thanks.

    Fabio

     

    Last edit: Fabio Nepi 2015-03-23
  • Stefan Bodewig

    Stefan Bodewig - 2015-04-02

    Hi Fabio

    Entity References are the sequences in XML that start with & and end with ; so it is not related to your case. This looks more like https://github.com/xmlunit/xmlunit/issues/3 - if you've got a schema that identifies the attributes as IDs or IDREFs. So far nobody has worked on the issue.

    I'm not completely sure I understand your usecase, if you want to ignore the value of certain attributes than your custom DifferenceListener is the best approach you can take. You could have used TextDifferenceListenerBase from the examples package to reduce some boilerplate code, but not by much.

    I would have expected that you needed to ensure the elc-elements actually pointed to the same (or at least existing) elements, that would be a lot harder to do.

     

Log in to post a comment.

MongoDB Logo MongoDB