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::Coverage happy
  • 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 an XML::XPathEngine::NodeSet object ✨
  • 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 an XML::XPathEngine::NodeSet object containing the nodes it found (or empty if no nodes matched the path), or one of XML::XPathEngine::Literal (a string), XML::XPathEngine::Number, or XML::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. See XML::XPathEngine::NodeSet πŸ’…
  • getNodeText ($path) β€” returns the text string for a particular node. Returns a string, or undef if the node doesn’t exist 😭
  • set_namespace ($prefix, $uri) β€” sets the namespace prefix mapping to the uri. Normally in XML::XPathEngine the prefixes in XPath node tests take their context from the current node. This means foo:bar will always match an element <foo:bar> regardless of the namespace that the prefix foo is 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 the set_namespace method of your XML::XPathEngine object ✨
  • 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::XPathEngine has 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 to 0 if 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::XPath for exemples of using this module 🌸
  • Tree::XPathEngine for 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 reuse XML::XPath. As XML::XPathEngine offers 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 πŸ’€

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