1XML::XPath(3) User Contributed Perl Documentation XML::XPath(3)
2
3
4
6 XML::XPath - Parse and evaluate XPath statements.
7
9 Version 1.43
10
12 This module aims to comply exactly to the XPath specification at
13 http://www.w3.org/TR/xpath and yet allow extensions to be added in the
14 form of functions.Modules such as XSLT and XPointer may need to do this
15 as they support functionality beyond XPath.
16
18 use XML::XPath;
19 use XML::XPath::XMLParser;
20
21 my $xp = XML::XPath->new(filename => 'test.xhtml');
22
23 my $nodeset = $xp->find('/html/body/p'); # find all paragraphs
24
25 foreach my $node ($nodeset->get_nodelist) {
26 print "FOUND\n\n",
27 XML::XPath::XMLParser::as_string($node),
28 "\n\n";
29 }
30
32 There is an awful lot to all of this, so bear with it - if you stick
33 it out it should be worth it. Please get a good understanding of XPath
34 by reading the spec before asking me questions. All of the classes and
35 parts herein are named to be synonymous with the names in the
36 specification, so consult that if you don't understand why I'm doing
37 something in the code.
38
40 The API of XML::XPath itself is extremely simple to allow you to get
41 going almost immediately. The deeper API's are more complex, but you
42 shouldn't have to touch most of that.
43
44 new()
45 This constructor follows the often seen named parameter method call.
46 Parameters you can use are: filename, parser, xml, ioref and context.
47 The filename parameter specifies an XML file to parse. The xml
48 parameter specifies a string to parse, and the ioref parameter
49 specifies an ioref to parse. The context option allows you to
50 specify a context node. The context node has to be in the format of a
51 node as specified in XML::XPath::XMLParser. The 4 parameters
52 filename, xml, ioref and context are mutually exclusive - you should
53 only specify one (if you specify anything other than context, the
54 context node is the root of your document). The parser option allows
55 you to pass in an already prepared XML::Parser object, to save you
56 having to create more than one in your application (if, for example,
57 you are doing more than just XPath).
58
59 my $xp = XML::XPath->new( context => $node );
60
61 It is very much recommended that you use only 1 XPath object
62 throughout the life of your application. This is because the object
63 (and it's sub-objects) maintain certain bits of state information
64 that will be useful (such as XPath variables) to later calls to
65 find(). It's also a good idea because you'll use less memory this way.
66
67 find($path, [$context])
68 The find function takes an XPath expression (a string) and returns
69 either an XML::XPath::NodeSet object containing the nodes it found (or
70 empty if no nodes matched the path), or one of XML::XPath::Literal (a
71 string), XML::XPath::Number or XML::XPath::Boolean. It should always
72 return something - and you can use ->isa() to find out what it
73 returned. If you need to check how many nodes it found you should check
74 $nodeset->size. See XML::XPath::NodeSet. An optional second parameter
75 of a context node allows you to use this method repeatedly, for example
76 XSLT needs to do this.
77
78 findnodes($path, [$context])
79 Returns a list of nodes found by $path, optionally in context $context.
80 In scalar context returns an XML::XPath::NodeSet object.
81
82 matches($node, $path, [$context])
83 Returns true if the node matches the path (optionally in context
84 $context).
85
86 findnodes_as_string($path, [$context])
87 Returns the nodes found reproduced as XML.The result isn't guaranteed
88 to be valid XML though.
89
90 findvalue($path, [$context])
91 Returns either a "XML::XPath::Literal", a "XML::XPath::Boolean" or a
92 "XML::XPath::Number" object.If the path returns a
93 NodeSet,$nodeset->to_literal is called automatically for you (and thus
94 a "XML::XPath::Literal" is returned).Note that for each of the objects
95 stringification is overloaded, so you can just print the value found,
96 or manipulate it in the ways you would a normal perl value (e.g. using
97 regular expressions).
98
99 exists($path, [$context])
100 Returns true if the given path exists.
101
102 getNodeText($path)
103 Returns the XML::XPath::Literal for a particular XML node. Returns a
104 string if exists or '' (empty string) if the node doesn't exist.
105
106 setNodeText($path, $text)
107 Sets the text string for a particular XML node. The node can be an
108 element or an attribute. If the node to be set is an attribute, and the
109 attribute node does not exist, it will be created automatically.
110
111 createNode($path)
112 Creates the node matching the $path given. If part of the path given or
113 all of the path do not exist, the necessary nodes will be created
114 automatically.
115
116 set_namespace($prefix, $uri)
117 Sets the namespace prefix mapping to the uri.
118
119 Normally in "XML::XPath" the prefixes in XPath node test take their
120 context from the current node. This means that foo:bar will always
121 match an element <foo:bar> regardless of the namespace that the
122 prefix foo is mapped to (which might even change within the document,
123 resulting in unexpected results). In order to make prefixes in XPath
124 node tests actually map to a real URI, you need to enable that via a
125 call to the set_namespace method of your "XML::XPath" object.
126
127 clear_namespaces()
128 Clears all previously set namespace mappings.
129
130 $XML::XPath::Namespaces
131 Set this to 0 if you don't want namespace processing to occur. This
132 will make everything a little (tiny) bit faster, but you'll suffer for
133 it, probably.
134
136 See XML::XPath::Node, XML::XPath::Node::Element,
137 XML::XPath::Node::Text, XML::XPath::Node::Comment,
138 XML::XPath::Node::Attribute, XML::XPath::Node::Namespace, and
139 XML::XPath::Node::PI.
140
142 XPath nodes work in a special way that allows circular references, and
143 yet still lets Perl's reference counting garbage collector to clean up
144 the nodes after use. This should be totally transparent to the
145 user, with one caveat: If you free your tree before letting go of a
146 sub-tree,consider that playing with fire and you may get burned. What
147 does this mean to the average user? Not much. Provided you don't free
148 (or let go out of scope) either the tree you passed to XML::XPath->new,
149 or if you didn't pass a tree, and passed a filename or IO-ref, then
150 provided you don't let the XML::XPath object go out of scope before
151 you let results of find() and its friends go out of scope, then
152 you'll be fine. Even if you do let the tree go out of scope before
153 results, you'll probably still be fine. The only case where you may
154 get stung is when the last part of your path/query is either an
155 ancestor or parent axis. In that case the worst that will happen is
156 you'll end up with a circular reference that won't get cleared until
157 interpreter destruction time.You can get around that by explicitly
158 calling $node->DESTROY on each of your result nodes, if you really need
159 to do that.
160
161 Mail me direct if that's not clear. Note that it's not doom and gloom.
162 It's by no means perfect,but the worst that will happen is a long
163 running process could leak memory. Most long running processes will
164 therefore be able to explicitly be careful not to free the tree (or
165 XML::XPath object) before freeing results.AxKit, an application that
166 uses XML::XPath, does this and I didn't have to make any changes to
167 the code - it's already sensible programming.
168
169 If you really don't want all this to happen, then set the variable
170 $XML::XPath::SafeMode, and call $xp->cleanup() on the XML::XPath object
171 when you're finished, or $tree->dispose() if you have a tree instead.
172
174 Please see the test files in t/ for examples on how to use XPath.
175
177 Original author Matt Sergeant, "<matt at sergeant.org>"
178
179 Currently maintained by Mohammad S Anwar, "<mohammad.anwar at
180 yahoo.com>"
181
183 XML::XPath::Literal, XML::XPath::Boolean, XML::XPath::Number,
184 XML::XPath::XMLParser, XML::XPath::NodeSet, XML::XPath::PerlSAX,
185 XML::XPath::Builder.
186
188 This module is copyright 2000 AxKit.com Ltd. This is free software,
189 and as such comes with NO WARRANTY. No dates are used in this module.
190 You may distribute this module under the terms of either the Gnu GPL,
191 or the Artistic License (the same terms as Perl itself).
192
193 For support, please subscribe to the Perl-XML
194 <http://listserv.activestate.com/mailman/listinfo/perl-xml> mailing
195 list at the URL
196
197
198
199perl v5.28.0 2018-10-10 XML::XPath(3)