Test::XML::XPath(3pm)
β¨ Test::XML::XPath β¨
ok so bestie, welcome to Test::XML::XPath π this is the Perl module for making assertions about your XML using XPath, and honestly? it's giving main character energy. (also it's Test::XML::XPath(3pm) if you're keeping track of the manual name, which, slay.)
πΈ NAME πΈ
Test::XML::XPath β Test XPath assertions β¨
π SYNOPSIS π
ok so here's the whole vibe, no cap:
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
look at those PASSes. we love to see it π«Ά
π¦ DESCRIPTION π¦
This module lets you assert statements about your XML in the form of XPath statements. Like, you can say that a piece of XML <em>must</em> 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. (so it checks LibXML first, then falls back to XPath β the order matters bestie, don't forget it π₯)
NB: 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. (ngl that's just good advice π)
NB: 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. π the@is <em>literally</em> perl's array sigil, she's doing too much.
π FUNCTIONS π
π
like_xpath ( XML, XPATH [, NAME ] )
Assert that XML (a string containing XML) matches the statement XPATH. NAME is the name of the test.
Returns true or false depending upon test success. π
π
unlike_xpath ( XML, XPATH [, NAME ] )
This is the reverse of like_xpath(). The test will only pass if XPATH does not generate any matches in XML.
Returns true or false depending upon test success. π
π
is_xpath ( XML, XPATH, EXPECTED [, NAME ] )
Evaluates XPATH against XML, and pass the test if the is EXPECTED. Uses findvalue() internally.
Returns true or false depending upon test success. β
π
set_xpath_processor ( CLASS )
Set the class name of the XPath processor used. It is up to you to ensure that this class is loaded. π
In all cases, XML must be well formed, or the test will fail. π no well-formed XML, no passing test, that's the rule fr fr.
π SEE ALSO π
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.
and that's the whole page bestie, iykyk π this was perl v5.34.0, dated 2022-06-18, and it's the Test::XML::XPath(3pm) manual β go forth and assert some XPath, you absolute icon π¦πβ¨
π¦ where this page comes from
- debian bookworm: libtest-xml-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtest-xml-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtest-xml-perl 0.08-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms