The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Test::XML::Assert - Tests XPaths into an XML Document for correct values/matches


 use Test::XML::Assert tests => 2;

 my $xml1 = "<foo xmlns="urn:message"><bar baz="buzz">text</bar></foo>";
 my $xml2 = "<f:foo xmlns:f="urn:message"><f:bar baz="buzz">text</f:bar></f:foo>";
 my $xml3 = "<foo><bar baz="buzz">text</bar></foo>";



This module allows you to test if two XML documents are semantically the same. This also holds true if different prefixes are being used for the xmlns, or if there is a default xmlns in place.

It uses XML::Assert to do all of it's checking.


In all of the following subroutines there are three common parameters.

$doc is a XML::LibXML documentElement(), which may or may not use namespaces.

$xmlns is a hashref of key value pairs which provide the namespace prefix and the namespace they map to. These namespace prefixes should be used in your $xpath. An empty hashref or null may also be passed if the $doc doesn't use namespaces.

$xpath is a string which contains the path to the element(s) you'd like to match against, whether this is for a count or a value match.

is_xpath_count($doc, $xmlns, $xpath, $count, $name)

Test passes if there are $count nodes referenced by $xpath in the $doc.

$count is the number of expected nodes which match the $xpath.

does_xpath_value_match($doc, $xmlns, $xpath, $match, $name)

Test passes if and only if $xpath matches one node in $doc and that node's value smart matches $match.

$match is the thing to match again. I say thing since it can be a string or a regex. In fact, it can be anything the smart smart operator can match against. See perlsyn for more details.

do_xpath_values_match($doc, $xmlns, $xpath, $match, $name)

Test passes if $path matches at least one node in $doc and all nodes matched smart matches against $match.

Again, $match can be a scalar, regex, arrayref or anything the smart match operator can match on.

does_attr_value_match($doc, $xmlns, $xpath, $attr, $match, $name)

Test passes if and only if $xpath matches one node in $doc, that node has an attr called $attr and the value of that smart matches $match.

do_attr_values_match($doc, $xmlns, $xpath, $match, $name)

Test passes if $xpath matches at least one node in $doc, those nodes all have an attr called $attr and those values smart matches $match.


Everything in "SUBROUTINES" by default, as expected.


XML::Assert, XML::Compare, Test::Builder, XML::LibXML



<andy at catalyst dot net dot nz>,


<andychilton at gmail dot com>,


This software development is sponsored and directed by New Zealand Registry Services,

The work is being carried out by Catalyst IT,

Copyright (c) 2010, NZ Registry Services. All Rights Reserved. This software may be used under the terms of the Artistic License 2.0. Note that this license is compatible with both the GNU GPL and Artistic licenses. A copy of this license is supplied with the distribution in the file COPYING.txt.