SYNOPSIS

  use Test::XML::XPath tests => 3;
  like_xpath( '<foo />', '/foo' );   # PASS
  like_xpath( '<foo />', '/bar' );   # FAIL
  unlike_xpath( '<foo />', '/bar' ); # PASS

  is_xpath( '<foo>bar</foo>', '/foo', 'bar' ); # PASS
  is_xpath( '<foo>bar</foo>', '/bar', 'foo' ); # FAIL

  # More interesting examples of xpath assertions.
  my $xml = '<foo attrib="1"><bish><bosh args="42">pub</bosh></bish></foo>';

  # Do testing for attributes.
  like_xpath( $xml, '/foo[@attrib="1"]' ); # PASS
  # Find an element anywhere in the document.
  like_xpath( $xml, '//bosh' ); # PASS
  # Both.
  like_xpath( $xml, '//bosh[@args="42"]' ); # PASS

DESCRIPTION

This module allows you to assert statements about your \s-1XML\s0 in the form of XPath statements. You can say that a piece of \s-1XML\s0 must contain certain tags, with so-and-so attributes, etc. It will try to use any installed XPath module that it knows about. Currently, this means XML::LibXML and XML::XPath, in that order.

\s-1NB\s0: Normally in XPath processing, the statement occurs from a context node. In the case of like_xpath(), the context node will always be the root node. In practice, this means that these two statements are identical:

# Absolute path. like_xpath( '<foo/>', '/foo' ); # Path relative to root. like_xpath( '<foo/>', 'foo' );

It's probably best to use absolute paths everywhere in order to keep things simple.

\s-1NB\s0: Beware of specifying attributes. Because they use an @-sign, perl will complain about trying to interpolate arrays if you don't escape them or use single quotes.

FUNCTIONS

like_xpath ( \s-1XML\s0, \s-1XPATH\s0 [, \s-1NAME\s0 ] )

Assert that \s-1XML\s0 (a string containing \s-1XML\s0) matches the statement \s-1XPATH\s0. \s-1NAME\s0 is the name of the test. Returns true or false depending upon test success.

unlike_xpath ( \s-1XML\s0, \s-1XPATH\s0 [, \s-1NAME\s0 ] )

This is the reverse of like_xpath(). The test will only pass if \s-1XPATH\s0 does not generates any matches in \s-1XML\s0. Returns true or false depending upon test success.

is_xpath ( \s-1XML\s0, \s-1XPATH\s0, \s-1EXPECTED\s0 [, \s-1NAME\s0 ] )

Evaluates \s-1XPATH\s0 against \s-1XML\s0, and pass the test if the is \s-1EXPECTED\s0. Uses findvalue() internally. Returns true or false depending upon test success.

set_xpath_processor ( \s-1CLASS\s0 )

Set the class name of the XPath processor used. It is up to you to ensure that this class is loaded.

In all cases, \s-1XML\s0 must be well formed, or the test will fail.

RELATED TO Test::XML::XPath…

Test::XML.

XML::XPath, which is the basis for this module.

If you are not conversant with XPath, there are many tutorials available on the web. Google will point you at them. The first one that I saw was: <http://www.zvon.org/xxl/XPathTutorial/>, which appears to offer interactive XPath as well as the tutorials.

AUTHOR

Dominic Mitchell <cpan2 (at) semantico.com>

COPYRIGHT AND LICENSE

Copyright 2002 by semantico

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.