XML::XPathEngine(3)
π XML::XPathEngine(3) π
okay bestie, welcome to the XML::XPathEngine(3) page from the User Contributed Perl Documentation crew π«Ά. This is the re-usable XPath engine one, and yes, weβre getting into all of it β every flag, method, warning, and little chaotic detail included π
.
πΈ NAME πΈ
XML::XPathEngine β a re-usable XPath engine for DOM-like trees β¨
π¦ DESCRIPTION π¦
This module gives you an XPath engine that other modules/classes implementing trees can re-use. To use it, nodes in your module need to mimick DOM nodes. The closer your tree is to a DOM, the more XPath features you can use β a module implementing all of the DOM should be able to use this module very easily, though you might need to add the cmp method on nodes to get ordered result sets π
This code is more or less a direct copy of the XML::XPath module by Matt Sergeant. The changes, fr fr:
- the XML processing part was removed, so thereβs no dependency on
XML::Parser - a couple of patches were applied
- a whole lot of methods were renamed to make
Pod::Coveragehappy - the docs were changed
The article eXtending XML XPath at http://www.xmltwig.com/article/extending_xml_xpath/ should give authors who want to use this module enough background to do so π
Otherwise, my email is below ;--)
WARNING: while the underlying code is rather solid, this module mostly lacks docs. As they say, βpatches welcomeββ¦ π
π SYNOPSIS π
use XML::XPathEngine;
my $tree= my_tree->new( ...);
my $xp = XML::XPathEngine->new();
my @nodeset = $xp->find('/root/kid/grandkid[1]', $tree); # find all first grankids
package XML::MyTree;
# needs to provide DOM methods
π DETAILS π
π API π
XML::XPathEngine will provide the following methods, bestie:
newβ itβs listed as an API method; the original page does not add a description here πfindnodes ($path, $context)β returns a list of nodes found by$path, optionally in context$context. In scalar context it returns anXML::XPathEngine::NodeSetobject β¨findnodes_as_string ($path, $context)β returns the nodes found as a single string. The result is not guaranteed to be valid XML though; it could, for example, be just text if the query returns attribute values π¦findnodes_as_strings ($path, $context)β returns the nodes found as a list of strings, one per node found πfindvalue ($path, $context)β returns the result as a string, aka the concatenation of the values of the result nodes πΈfindvalues($path, $context)β returns the values of the result nodes as a list of strings πexists ($path, $context)β returns true if the given path exists πmatches($node, $path, $context)β returns true if the node matches the path π₯find ($path, $context)β takes an XPath expression (a string) and returns either anXML::XPathEngine::NodeSetobject containing the nodes it found (or empty if no nodes matched the path), or one ofXML::XPathEngine::Literal(a string),XML::XPathEngine::Number, orXML::XPathEngine::Boolean. It should always return something, and you can use->isa()to find out what it returned. If you need to check how many nodes it found, check$nodeset->size. SeeXML::XPathEngine::NodeSetπgetNodeText ($path)β returns the text string for a particular node. Returns a string, orundefif the node doesnβt exist πset_namespace ($prefix, $uri)β sets the namespace prefix mapping to the uri. Normally inXML::XPathEnginethe prefixes in XPath node tests take their context from the current node. This meansfoo:barwill always match an element<foo:bar>regardless of the namespace that the prefixfoois mapped to β which might even change within the document, resulting in unexpected results π. To make prefixes in XPath node tests actually map to a real URI, you need to enable that via a call to theset_namespacemethod of yourXML::XPathEngineobject β¨clear_namespaces ()β clears all previously set namespace mappings π§get_namespace ($prefix, $node)β returns the uri associated to the prefix for the node, mostly for internal usage πset_strict_namespaces ($strict)β by default, for historical as well as convenience reasons,XML::XPathEnginehas a slightly non-standard way of dealing with the default namespace. If you search for"//tag"it will return elements"tag". As far as I understand it, if the document has a default namespace, this should not return anything. You would have to first do a"set_namespace", and then search using the namespace. Passing a true value to"set_strict_namespaces"will activate this behaviour; passing a false value will return it to its default behaviour πset_var ($var. $val)β sets an XPath variable that can be used in queries as$varπget_var ($var)β returns the value of the XPath variable, mostly for internal usage π«$XML::XPathEngine::Namespacesβ set this to0if you donβt want namespace processing to occur. This will make everything a little (tiny) bit faster, but youβll suffer for it, probably π
πΏ Node Object Model πΏ
Nodes need to provide the same API as nodes in XML::XPath β at least the access API, not the tree manipulation one, bestie π«Ά
π Example π
Please see the test files in t/ for examples on how to use XPath β¨
π§ XPath extension π§
The module supports the XPath recommendation to the same extent as XML::XPath β that is, rather completely π
It includes a perl-specific extension: direct support for regular expressions π₯
You can use the usual (in Perl!) =~ and !~ operators. Regular expressions are / delimited β no other delimiter is accepted, \ inside regexp must be backslashed β and the imsx modifiers can be used π
$xp->findnodes( '//@att[.=~ /^v.$/]'); # returns the list of attributes att
# whose value matches ^v.$
π SEE ALSO π
XML::XPathπHTML::TreeBuilder::XPath,XML::Twig::XPathfor exemples of using this module πΈTree::XPathEnginefor a similar module for non-XML trees π¦<http://www.xmltwig.com/article/extending_xml_xpath/>for background information. The last section of the article summarizes how to reuseXML::XPath. AsXML::XPathEngineoffers the same API, it should help you π
π AUTHOR π
Michel Rodriguez, "<mirod@cpan.org>" β most code comes directly from XML::XPath, by Matt Sergeant π
π BUGS π
Please report any bugs or feature requests to "bug-tree-xpathengine@rt.cpan.org", or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=XML-XPathEngine>. I will be notified, and then youβll automatically be notified of progress on your bug as I make changes π«Ά
π«Ά ACKNOWLEDGEMENTS π«Ά
This section is literally empty in the original, bestie β nothing listed here π
π COPYRIGHT & LICENSE π
XML::XPath Copyright 2000 AxKit.com Ltd. Copyright 2006 Michel Rodriguez, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself π
k byeee β this was the XML::XPathEngine(3) page from perl v5.42.0, dated 2013-06-04, and yes she ate π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-XML-XPathEngine 0.14-160000.2.2 Β· GPL-1.0+ or Artistic-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms