in reply to Comparison of XML files ignoring ordering of child elements

Hello adikan123, and welcome to the Monastery!

Have a look at the Test::XML::Ordered module. The author says:

This module is a test module which compares two XML files for equivalence in an ordered fashion. It was written after I ... realised that XML::SemanticDiff, which is the basis for Test::XML, ... compares two XML files for equivalence in a "semantic" fashion where elements can be present in several possible orders. (... [which] is not normally what I want.).

Caveats:

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

  • Comment on Re: Comparison of XML files ignoring ordering of child elements

Replies are listed 'Best First'.
Re^2: Comparison of XML files ignoring ordering of child elements
by adikan123 (Novice) on Jan 17, 2019 at 08:33 UTC
    Thank you @Athanasius. I will use this approach, will let you know if it works or not.
      Need hep to understand usage of this module - Test::XML::Ordered. I am new to Perl, need help to understand with example. Thank you!

        G'day adikan123,

        Welcome to the Monastery.

        I appreciate that you're new to both Perl and PerlMonks: the following is not intended as a rebuke. Asking for help without indicating what you're having difficulty with is problematic; it's difficult to know exactly what sort of aid you're looking for. Please look at "How do I post a question effectively?" and "Short, Self-Contained, Correct Example" for information about how you can help us to help you. I'll attempt to provide some assistance here. You may have follow-up questions: if so, please follow the linked guidelines when replying.

        The first place to look for information on a module's usage is the SYNOPSIS. Other sections of the documentation usually provide additional usage information: those with headings like Description and Methods are obvious candidates, but don't overlook Caveats, Limitations, and similar.

        There's often a plethora of useful information in the distribution files which should all be listed in a MANIFEST. Test scripts written by the module's author (in the t/ directory) can often be very helpful; also look for directories with names like examples/ and demos/.

        Probably as a last resort, you can also look at the module's source code.

        I've never used Test::XML::Ordered. It installed without any problems for me. If you had any problems at this stage, you'll need to provide details.

        cpan[1]> install Test::XML::Ordered ... SHLOMIF/Test-XML-Ordered-0.0.9.tar.gz ./Build install -- OK

        The first line of the SYNOPSIS is:

        use Test::More tests => 1;

        If the Test::* modules, in general, are new to you, you should familiarise yourself with the documentation for Test::More. That's a core module which you should already have installed.

        I had problems with the next line:

        use Test::XML::Ordered;

        Although the documentation says that is_xml_ordered is exported, apparently it's not:

        Undefined subroutine &main::is_xml_ordered called at pm_1228677_test_x +ml_ordered.t line 28.

        Perhaps that's where you had problems. I fixed it with:

        use Test::XML::Ordered 'is_xml_ordered';

        The remainder of the SYNOPSIS code seemed to work without any further issues. Here's an example test script I put together.

        use strict; use warnings; use Test::More tests => 2; use Test::XML::Ordered 'is_xml_ordered'; my $expected = <<'EOX'; <a> <b>first</b> <b>second</b> </a> EOX my $got_good = <<'EOX'; <a> <b>first</b> <b>second</b> </a> EOX my $got_bad = <<'EOX'; <a> <b>second</b> <b>first</b> </a> EOX is_xml_ordered( [string => $got_good], [string => $expected], {}, 'Test $got_good' ); is_xml_ordered( [string => $got_bad], [string => $expected], {}, 'Test $got_bad' );

        The output was as expected: $got_good succeeded; $got_bad failed.

        $ prove -v pm_1228677_test_xml_ordered.t pm_1228677_test_xml_ordered.t .. 1..2 ok 1 - Test $got_good # Texts differ: Got <<second>> at 5 ; Expected <<first>> at 5 not ok 2 - Test $got_bad # Failed test 'Test $got_bad' # at pm_1228677_test_xml_ordered.t line 35. # Looks like you failed 1 test of 2. Dubious, test returned 1 (wstat 256, 0x100) Failed 1/2 subtests Test Summary Report ------------------- pm_1228677_test_xml_ordered.t (Wstat: 256 Tests: 2 Failed: 1) Failed test: 2 Non-zero exit status: 1 Files=1, Tests=2, 0 wallclock secs ( 0.03 usr 0.00 sys + 0.12 cusr + 0.01 csys = 0.16 CPU) Result: FAIL

        I've used prove. This is a core utility which you should have installed: you can run my test script the same way. Of course, you may want to use "make test" or something else; again, without details of your intended usage, I can't really advise further.

        — Ken