in reply to Re^2: Comparison of XML files ignoring ordering of child elements
in thread Comparison of XML files ignoring ordering of child elements

Need hep to understand usage of this module - Test::XML::Ordered. I am new to Perl, need help to understand with example. Thank you!
  • Comment on Re^3: Comparison of XML files ignoring ordering of child elements

Replies are listed 'Best First'.
Re^4: Comparison of XML files ignoring ordering of child elements
by kcott (Archbishop) on Jan 17, 2019 at 11:30 UTC

    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

      Thank you very much Ken! Need one more help, can you please show me how can I use this approach providing XMLs as input file to the Perl script. Thank You

        I am using below code to compare 2 different XML files but I am getting below error

        1..1 Entity: line 1: parser error : ParsePI: PI xm space expected <?xm="UTF-8"?> ^ # Looks like your test exited with 255 before it could output anything +.

        Please suggest me what can be wrong in the script.

        use strict; use warnings; use Test::More tests => 1; use Test::XML::Ordered 'is_xml_ordered'; my $parser1 = XML::LibXML->new(); my $expected = $parser1->parse_file( $ARGV[0] ); my $parser2 = XML::LibXML->new(); my $actual = $parser2->parse_file( $ARGV[1] ); is_xml_ordered( [string => $actual], [string => $expected], {}, 'Test $got_good' );