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