Add built local::lib
[catagits/Gitalist.git] / local-lib5 / man / man3 / XML::LibXML::XPathContext.3pm
1 .\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.10)
2 .\"
3 .\" Standard preamble:
4 .\" ========================================================================
5 .de Sp \" Vertical space (when we can't use .PP)
6 .if t .sp .5v
7 .if n .sp
8 ..
9 .de Vb \" Begin verbatim text
10 .ft CW
11 .nf
12 .ne \\$1
13 ..
14 .de Ve \" End verbatim text
15 .ft R
16 .fi
17 ..
18 .\" Set up some character translations and predefined strings.  \*(-- will
19 .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
20 .\" double quote, and \*(R" will give a right double quote.  \*(C+ will
21 .\" give a nicer C++.  Capital omega is used to do unbreakable dashes and
22 .\" therefore won't be available.  \*(C` and \*(C' expand to `' in nroff,
23 .\" nothing in troff, for use with C<>.
24 .tr \(*W-
25 .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
26 .ie n \{\
27 .    ds -- \(*W-
28 .    ds PI pi
29 .    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
30 .    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
31 .    ds L" ""
32 .    ds R" ""
33 .    ds C` ""
34 .    ds C' ""
35 'br\}
36 .el\{\
37 .    ds -- \|\(em\|
38 .    ds PI \(*p
39 .    ds L" ``
40 .    ds R" ''
41 'br\}
42 .\"
43 .\" Escape single quotes in literal strings from groff's Unicode transform.
44 .ie \n(.g .ds Aq \(aq
45 .el       .ds Aq '
46 .\"
47 .\" If the F register is turned on, we'll generate index entries on stderr for
48 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
49 .\" entries marked with X<> in POD.  Of course, you'll have to process the
50 .\" output yourself in some meaningful fashion.
51 .ie \nF \{\
52 .    de IX
53 .    tm Index:\\$1\t\\n%\t"\\$2"
54 ..
55 .    nr % 0
56 .    rr F
57 .\}
58 .el \{\
59 .    de IX
60 ..
61 .\}
62 .\"
63 .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
64 .\" Fear.  Run.  Save yourself.  No user-serviceable parts.
65 .    \" fudge factors for nroff and troff
66 .if n \{\
67 .    ds #H 0
68 .    ds #V .8m
69 .    ds #F .3m
70 .    ds #[ \f1
71 .    ds #] \fP
72 .\}
73 .if t \{\
74 .    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
75 .    ds #V .6m
76 .    ds #F 0
77 .    ds #[ \&
78 .    ds #] \&
79 .\}
80 .    \" simple accents for nroff and troff
81 .if n \{\
82 .    ds ' \&
83 .    ds ` \&
84 .    ds ^ \&
85 .    ds , \&
86 .    ds ~ ~
87 .    ds /
88 .\}
89 .if t \{\
90 .    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
91 .    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
92 .    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
93 .    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
94 .    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
95 .    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
96 .\}
97 .    \" troff and (daisy-wheel) nroff accents
98 .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
99 .ds 8 \h'\*(#H'\(*b\h'-\*(#H'
100 .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
101 .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
102 .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
103 .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
104 .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
105 .ds ae a\h'-(\w'a'u*4/10)'e
106 .ds Ae A\h'-(\w'A'u*4/10)'E
107 .    \" corrections for vroff
108 .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
109 .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
110 .    \" for low resolution devices (crt and lpr)
111 .if \n(.H>23 .if \n(.V>19 \
112 \{\
113 .    ds : e
114 .    ds 8 ss
115 .    ds o a
116 .    ds d- d\h'-1'\(ga
117 .    ds D- D\h'-1'\(hy
118 .    ds th \o'bp'
119 .    ds Th \o'LP'
120 .    ds ae ae
121 .    ds Ae AE
122 .\}
123 .rm #[ #] #H #V #F C
124 .\" ========================================================================
125 .\"
126 .IX Title "XML::LibXML::XPathContext 3"
127 .TH XML::LibXML::XPathContext 3 "2009-10-07" "perl v5.8.7" "User Contributed Perl Documentation"
128 .\" For nroff, turn off justification.  Always turn off hyphenation; it makes
129 .\" way too many mistakes in technical documents.
130 .if n .ad l
131 .nh
132 .SH "NAME"
133 XML::LibXML::XPathContext \- XPath Evaluation
134 .SH "SYNOPSIS"
135 .IX Header "SYNOPSIS"
136 .Vb 10
137 \&  my $xpc = XML::LibXML::XPathContext\->new();
138 \&  my $xpc = XML::LibXML::XPathContext\->new($node);
139 \&  $xpc\->registerNs($prefix, $namespace_uri)
140 \&  $xpc\->unregisterNs($prefix)
141 \&  $uri = $xpc\->lookupNs($prefix)
142 \&  $xpc\->registerVarLookupFunc($callback, $data)
143 \&  $data = $xpc\->getVarLookupData();
144 \&  $callback = $xpc\->getVarLookupFunc();
145 \&  $xpc\->unregisterVarLookupFunc($name);
146 \&  $xpc\->registerFunctionNS($name, $uri, $callback)
147 \&  $xpc\->unregisterFunctionNS($name, $uri)
148 \&  $xpc\->registerFunction($name, $callback)
149 \&  $xpc\->unregisterFunction($name)
150 \&  @nodes = $xpc\->findnodes($xpath)
151 \&  @nodes = $xpc\->findnodes($xpath, $context_node )
152 \&  $nodelist = $xpc\->findnodes($xpath, $context_node )
153 \&  $object = $xpc\->find($xpath )
154 \&  $object = $xpc\->find($xpath, $context_node )
155 \&  $value = $xpc\->findvalue($xpath )
156 \&  $value = $xpc\->findvalue($xpath, $context_node )
157 \&  $bool = $xpc\->exists( $xpath_expression, $context_node );
158 \&  $xpc\->setContextNode($node)
159 \&  my $node = $xpc\->getContextNode;
160 \&  $xpc\->setContextPosition($position)
161 \&  my $position = $xpc\->getContextPosition;
162 \&  $xpc\->setContextSize($size)
163 \&  my $size = $xpc\->getContextSize;
164 \&  $xpc\->setContextNode($node)
165 \&The XML::LibXML::XPathContext class provides an almost complete interface to
166 \&libxml2\*(Aqs XPath implementation. With XML::LibXML::XPathContext is is possible
167 \&to evaluate XPath expressions in the context of arbitrary node, context size,
168 \&and context position, with a user\-defined namespace\-prefix mapping, custom
169 \&XPath functions written in Perl, and even a custom XPath variable resolver.
170 .Ve
171 .SH "EXAMPLES"
172 .IX Header "EXAMPLES"
173 .SS "Namespaces"
174 .IX Subsection "Namespaces"
175 This example demonstrates \f(CW\*(C`registerNs()\*(C'\fR method. It finds all paragraph nodes in an \s-1XHTML\s0 document.
176 .PP
177 .Vb 3
178 \&  my $xc = XML::LibXML::XPathContext\->new($xhtml_doc);
179 \&  $xc\->registerNs(\*(Aqxhtml\*(Aq, \*(Aqhttp://www.w3.org/1999/xhtml\*(Aq);
180 \&  my @nodes = $xc\->findnodes(\*(Aq//xhtml:p\*(Aq);
181 .Ve
182 .SS "Custom XPath functions"
183 .IX Subsection "Custom XPath functions"
184 This example demonstrates \f(CW\*(C`registerFunction()\*(C'\fR method by defining a function filtering nodes based on a Perl regular
185 expression:
186 .PP
187 .Vb 8
188 \&  sub grep_nodes { 
189 \&    my ($nodelist,$regexp) =  @_;
190 \&    my $result = XML::LibXML::NodeList\->new;
191 \&    for my $node ($nodelist\->get_nodelist()) {
192 \&      $result\->push($node) if $node\->textContent =~ $regexp;
193 \&    }
194 \&    return $result;
195 \&  };
196 \&
197 \&  my $xc = XML::LibXML::XPathContext\->new($node);
198 \&  $xc\->registerFunction(\*(Aqgrep_nodes\*(Aq, \e&grep_nodes);
199 \&  my @nodes = $xc\->findnodes(\*(Aq//section[grep_nodes(para,"\ebsearch(ing|es)?\eb")]\*(Aq);
200 .Ve
201 .SS "Variables"
202 .IX Subsection "Variables"
203 This example demonstrates \f(CW\*(C`registerVarLookup()\*(C'\fR method. We use XPath variables to recycle results of previous evaluations:
204 .PP
205 .Vb 4
206 \&  sub var_lookup {
207 \&    my ($varname,$ns,$data)=@_;
208 \&    return $data\->{$varname};
209 \&  }
210 \&
211 \&  my $areas = XML::LibXML\->new\->parse_file(\*(Aqareas.xml\*(Aq);
212 \&  my $empl = XML::LibXML\->new\->parse_file(\*(Aqemployees.xml\*(Aq);
213 \&
214 \&  my $xc = XML::LibXML::XPathContext\->new($empl);
215 \&
216 \&  my %variables = ( 
217 \&    A => $xc\->find(\*(Aq/employees/employee[@salary>10000]\*(Aq),
218 \&    B => $areas\->find(\*(Aq/areas/area[district=\*(AqBrooklyn\*(Aq]/street\*(Aq), 
219 \&  );
220 \&
221 \&  # get names of employees from $A working in an area listed in $B
222 \&  $xc\->registerVarLookupFunc(\e&var_lookup, \e%variables);
223 \&  my @nodes = $xc\->findnodes(\*(Aq$A[work_area/street = $B]/name\*(Aq);
224 .Ve
225 .SH "METHODS"
226 .IX Header "METHODS"
227 .IP "new" 4
228 .IX Item "new"
229 .Vb 1
230 \&  my $xpc = XML::LibXML::XPathContext\->new();
231 .Ve
232 .Sp
233 Creates a new XML::LibXML::XPathContext object without a context node.
234 .Sp
235 .Vb 1
236 \&  my $xpc = XML::LibXML::XPathContext\->new($node);
237 .Ve
238 .Sp
239 Creates a new XML::LibXML::XPathContext object with the context node set to \f(CW$node\fR.
240 .IP "registerNs" 4
241 .IX Item "registerNs"
242 .Vb 1
243 \&  $xpc\->registerNs($prefix, $namespace_uri)
244 .Ve
245 .Sp
246 Registers namespace \f(CW$prefix\fR to \f(CW$namespace_uri\fR.
247 .IP "unregisterNs" 4
248 .IX Item "unregisterNs"
249 .Vb 1
250 \&  $xpc\->unregisterNs($prefix)
251 .Ve
252 .Sp
253 Unregisters namespace \f(CW$prefix\fR.
254 .IP "lookupNs" 4
255 .IX Item "lookupNs"
256 .Vb 1
257 \&  $uri = $xpc\->lookupNs($prefix)
258 .Ve
259 .Sp
260 Returns namespace \s-1URI\s0 registered with \f(CW$prefix\fR. If \f(CW$prefix\fR is not registered to any namespace \s-1URI\s0 returns \f(CW\*(C`undef\*(C'\fR.
261 .IP "registerVarLookupFunc" 4
262 .IX Item "registerVarLookupFunc"
263 .Vb 1
264 \&  $xpc\->registerVarLookupFunc($callback, $data)
265 .Ve
266 .Sp
267 Registers variable lookup function \f(CW$prefix\fR. The registered function is executed by the XPath engine each time an XPath
268 variable is evaluated. It takes three arguments: \f(CW$data\fR, variable name, and variable ns-URI and must return one value: a number or
269 string or any \f(CW\*(C`XML::LibXML::\*(C'\fR object that can be a result of findnodes: Boolean, Literal, Number, Node (e.g.
270 Document, Element, etc.), or NodeList. For convenience, simple (non-blessed)
271 array references containing only XML::LibXML::Node objects can be used instead of a XML::LibXML::NodeList.
272 .IP "getVarLookupData" 4
273 .IX Item "getVarLookupData"
274 .Vb 1
275 \&  $data = $xpc\->getVarLookupData();
276 .Ve
277 .Sp
278 Returns the data that have been associated with a variable lookup function
279 during a previous call to \f(CW\*(C`registerVarLookupFunc\*(C'\fR.
280 .IP "getVarLookupFunc" 4
281 .IX Item "getVarLookupFunc"
282 .Vb 1
283 \&  $callback = $xpc\->getVarLookupFunc();
284 .Ve
285 .Sp
286 Returns the variable lookup function previously registered with \f(CW\*(C`registerVarLookupFunc\*(C'\fR.
287 .IP "unregisterVarLookupFunc" 4
288 .IX Item "unregisterVarLookupFunc"
289 .Vb 1
290 \&  $xpc\->unregisterVarLookupFunc($name);
291 .Ve
292 .Sp
293 Unregisters variable lookup function and the associated lookup data.
294 .IP "registerFunctionNS" 4
295 .IX Item "registerFunctionNS"
296 .Vb 1
297 \&  $xpc\->registerFunctionNS($name, $uri, $callback)
298 .Ve
299 .Sp
300 Registers an extension function \f(CW$name\fR in \f(CW$uri\fR namespace. \f(CW$callback\fR must be a \s-1CODE\s0 reference. The arguments of the callback function are either
301 simple scalars or \f(CW\*(C`XML::LibXML::*\*(C'\fR objects depending on the XPath argument types. The function is responsible for
302 checking the argument number and types. Result of the callback code must be a
303 single value of the following types: a simple scalar (number, string) or an
304 arbitrary \f(CW\*(C`XML::LibXML::*\*(C'\fR object that can be a result of findnodes: Boolean, Literal, Number, Node (e.g.
305 Document, Element, etc.), or NodeList. For convenience, simple (non-blessed)
306 array references containing only XML::LibXML::Node objects can be used instead of a XML::LibXML::NodeList.
307 .IP "unregisterFunctionNS" 4
308 .IX Item "unregisterFunctionNS"
309 .Vb 1
310 \&  $xpc\->unregisterFunctionNS($name, $uri)
311 .Ve
312 .Sp
313 Unregisters extension function \f(CW$name\fR in \f(CW$uri\fR namespace. Has the same effect as passing \f(CW\*(C`undef\*(C'\fR as \f(CW$callback\fR to registerFunctionNS.
314 .IP "registerFunction" 4
315 .IX Item "registerFunction"
316 .Vb 1
317 \&  $xpc\->registerFunction($name, $callback)
318 .Ve
319 .Sp
320 Same as \f(CW\*(C`registerFunctionNS\*(C'\fR but without a namespace.
321 .IP "unregisterFunction" 4
322 .IX Item "unregisterFunction"
323 .Vb 1
324 \&  $xpc\->unregisterFunction($name)
325 .Ve
326 .Sp
327 Same as \f(CW\*(C`unregisterFunctionNS\*(C'\fR but without a namespace.
328 .IP "findnodes" 4
329 .IX Item "findnodes"
330 .Vb 1
331 \&  @nodes = $xpc\->findnodes($xpath)
332 \&
333 \&  @nodes = $xpc\->findnodes($xpath, $context_node )
334 \&
335 \&  $nodelist = $xpc\->findnodes($xpath, $context_node )
336 .Ve
337 .Sp
338 Performs the xpath statement on the current node and returns the result as an
339 array. In scalar context returns a XML::LibXML::NodeList object. Optionally, a node may be passed as a second argument to set the
340 context node for the query.
341 .Sp
342 The xpath expression can be passed either as a string or or as a XML::LibXML::XPathExpression object.
343 .IP "find" 4
344 .IX Item "find"
345 .Vb 1
346 \&  $object = $xpc\->find($xpath )
347 \&
348 \&  $object = $xpc\->find($xpath, $context_node )
349 .Ve
350 .Sp
351 Performs the xpath expression using the current node as the context of the
352 expression, and returns the result depending on what type of result the XPath
353 expression had. For example, the XPath \f(CW\*(C`1 * 3 +              52\*(C'\fR results in a XML::LibXML::Number object being returned. Other expressions might return a XML::LibXML::Boolean object, or a XML::LibXML::Literal object (a string). Each of those objects uses Perl's overload feature to ``do
354 the right thing'' in different contexts. Optionally, a node may be passed as a
355 second argument to set the context node for the query.
356 .Sp
357 The xpath expression can be passed either as a string or or as a XML::LibXML::XPathExpression object.
358 .IP "findvalue" 4
359 .IX Item "findvalue"
360 .Vb 1
361 \&  $value = $xpc\->findvalue($xpath )
362 \&
363 \&  $value = $xpc\->findvalue($xpath, $context_node )
364 .Ve
365 .Sp
366 Is exactly equivalent to:
367 .Sp
368 .Vb 1
369 \&  $xpc\->find( $xpath, $context_node )\->to_literal;
370 .Ve
371 .Sp
372 That is, it returns the literal value of the results. This enables you to
373 ensure that you get a string back from your search, allowing certain shortcuts.
374 This could be used as the equivalent of <xsl:value\-of select=``some_xpath''/>.
375 Optionally, a node may be passed in the second argument to set the context node
376 for the query.
377 .Sp
378 The xpath expression can be passed either as a string or or as a XML::LibXML::XPathExpression object.
379 .IP "exists" 4
380 .IX Item "exists"
381 .Vb 1
382 \&  $bool = $xpc\->exists( $xpath_expression, $context_node );
383 .Ve
384 .Sp
385 This method behaves like \fIfindnodes\fR, except that it only returns a boolean value (1 if the expression matches a
386 node, 0 otherwise) and may be faster than \fIfindnodes\fR, because the XPath evaluation may stop early on the first match (this is true
387 for libxml2 >= 2.6.27).
388 .Sp
389 For XPath expressions that do not return node-set, the method returns true if
390 the returned value is a non-zero number or a non-empty string.
391 .IP "setContextNode" 4
392 .IX Item "setContextNode"
393 .Vb 1
394 \&  $xpc\->setContextNode($node)
395 .Ve
396 .Sp
397 Set the current context node.
398 .IP "getContextNode" 4
399 .IX Item "getContextNode"
400 .Vb 1
401 \&  my $node = $xpc\->getContextNode;
402 .Ve
403 .Sp
404 Get the current context node.
405 .IP "setContextPosition" 4
406 .IX Item "setContextPosition"
407 .Vb 1
408 \&  $xpc\->setContextPosition($position)
409 .Ve
410 .Sp
411 Set the current context position. By default, this value is \-1 (and evaluating
412 XPath function \f(CW\*(C`position()\*(C'\fR in the initial context raises an XPath error), but can be set to any value up
413 to context size. This usually only serves to cheat the XPath engine to return
414 given position when \f(CW\*(C`position()\*(C'\fR XPath function is called. Setting this value to \-1 restores the default
415 behavior.
416 .IP "getContextPosition" 4
417 .IX Item "getContextPosition"
418 .Vb 1
419 \&  my $position = $xpc\->getContextPosition;
420 .Ve
421 .Sp
422 Get the current context position.
423 .IP "setContextSize" 4
424 .IX Item "setContextSize"
425 .Vb 1
426 \&  $xpc\->setContextSize($size)
427 .Ve
428 .Sp
429 Set the current context size. By default, this value is \-1 (and evaluating
430 XPath function \f(CW\*(C`last()\*(C'\fR in the initial context raises an XPath error), but can be set to any
431 non-negative value. This usually only serves to cheat the XPath engine to
432 return the given value when \f(CW\*(C`last()\*(C'\fR XPath function is called. If context size is set to 0, position is
433 automatically also set to 0. If context size is positive, position is
434 automatically set to 1. Setting context size to \-1 restores the default
435 behavior.
436 .IP "getContextSize" 4
437 .IX Item "getContextSize"
438 .Vb 1
439 \&  my $size = $xpc\->getContextSize;
440 .Ve
441 .Sp
442 Get the current context size.
443 .IP "setContextNode" 4
444 .IX Item "setContextNode"
445 .Vb 1
446 \&  $xpc\->setContextNode($node)
447 .Ve
448 .Sp
449 Set the current context node.
450 .SH "BUGS AND CAVEATS"
451 .IX Header "BUGS AND CAVEATS"
452 XML::LibXML::XPathContext objects \fIare\fR reentrant, meaning that you can call methods of an XML::LibXML::XPathContext
453 even from XPath extension functions registered with the same object or from a
454 variable lookup function. On the other hand, you should rather avoid
455 registering new extension functions, namespaces and a variable lookup function
456 from within extension functions and a variable lookup function, unless you want
457 to experience untested behavior.
458 .SH "AUTHORS"
459 .IX Header "AUTHORS"
460 Ilya Martynov and Petr Pajas, based on XML::LibXML and XML::LibXSLT code by
461 Matt Sergeant and Christian Glahn.
462 .SH "HISTORICAL REMARK"
463 .IX Header "HISTORICAL REMARK"
464 Prior to XML::LibXML 1.61 this module was distributed separately for
465 maintenance reasons.
466 .SH "AUTHORS"
467 .IX Header "AUTHORS"
468 Matt Sergeant, 
469 Christian Glahn, 
470 Petr Pajas
471 .SH "VERSION"
472 .IX Header "VERSION"
473 1.70
474 .SH "COPYRIGHT"
475 .IX Header "COPYRIGHT"
476 2001\-2007, AxKit.com Ltd.
477 .PP
478 2002\-2006, Christian Glahn.
479 .PP
480 2006\-2009, Petr Pajas.