1Test::XML::XPath(3) User Contributed Perl Documentation Test::XML::XPath(3)
2
3
4
6 Test::XML::XPath - Test XPath assertions
7
9 use Test::XML::XPath tests => 3;
10 like_xpath( '<foo />', '/foo' ); # PASS
11 like_xpath( '<foo />', '/bar' ); # FAIL
12 unlike_xpath( '<foo />', '/bar' ); # PASS
13
14 is_xpath( '<foo>bar</foo>', '/foo', 'bar' ); # PASS
15 is_xpath( '<foo>bar</foo>', '/bar', 'foo' ); # FAIL
16
17 # More interesting examples of xpath assertions.
18 my $xml = '<foo attrib="1"><bish><bosh args="42">pub</bosh></bish></foo>';
19
20 # Do testing for attributes.
21 like_xpath( $xml, '/foo[@attrib="1"]' ); # PASS
22 # Find an element anywhere in the document.
23 like_xpath( $xml, '//bosh' ); # PASS
24 # Both.
25 like_xpath( $xml, '//bosh[@args="42"]' ); # PASS
26
28 This module allows you to assert statements about your XML in the form
29 of XPath statements. You can say that a piece of XML must contain
30 certain tags, with so-and-so attributes, etc. It will try to use any
31 installed XPath module that it knows about. Currently, this means
32 XML::LibXML and XML::XPath, in that order.
33
34 NB: Normally in XPath processing, the statement occurs from a context
35 node. In the case of like_xpath(), the context node will always be the
36 root node. In practice, this means that these two statements are
37 identical:
38
39 # Absolute path.
40 like_xpath( '<foo/>', '/foo' );
41 # Path relative to root.
42 like_xpath( '<foo/>', 'foo' );
43
44 It's probably best to use absolute paths everywhere in order to keep
45 things simple.
46
47 NB: Beware of specifying attributes. Because they use an @-sign, perl
48 will complain about trying to interpolate arrays if you don't escape
49 them or use single quotes.
50
52 like_xpath ( XML, XPATH [, NAME ] )
53 Assert that XML (a string containing XML) matches the statement
54 XPATH. NAME is the name of the test.
55
56 Returns true or false depending upon test success.
57
58 unlike_xpath ( XML, XPATH [, NAME ] )
59 This is the reverse of like_xpath(). The test will only pass if
60 XPATH does not generates any matches in XML.
61
62 Returns true or false depending upon test success.
63
64 is_xpath ( XML, XPATH, EXPECTED [, NAME ] )
65 Evaluates XPATH against XML, and pass the test if the is EXPECTED.
66 Uses findvalue() internally.
67
68 Returns true or false depending upon test success.
69
70 set_xpath_processor ( CLASS )
71 Set the class name of the XPath processor used. It is up to you to
72 ensure that this class is loaded.
73
74 In all cases, XML must be well formed, or the test will fail.
75
77 Test::XML.
78
79 XML::XPath, which is the basis for this module.
80
81 If you are not conversant with XPath, there are many tutorials
82 available on the web. Google will point you at them. The first one
83 that I saw was: <http://www.zvon.org/xxl/XPathTutorial/>, which appears
84 to offer interactive XPath as well as the tutorials.
85
87 Dominic Mitchell <cpan2 (at) semantico.com>
88
90 Copyright 2002 by semantico
91
92 This library is free software; you can redistribute it and/or modify it
93 under the same terms as Perl itself.
94
95
96
97perl v5.36.0 2023-01-20 Test::XML::XPath(3)