XML::Li...Pattern(3)
π XML::LibXML::Pattern β the manual page, but make it make sense π
okay bestie, so this is a section 3 page from the User Contributed Perl Documentation β it's the whole vibe on XML::LibXML::Pattern, which is basically your backstage pass to libxml2's XPath pattern matching. no cap. π¦
β¨ NAME β¨
XML::LibXML::Pattern β the interface to libxml2 XPath patterns. that's it, that's the name, periodt. π
πΈ SYNOPSIS πΈ
here's the whole thing in action, copy it exactly bestie:
use XML::LibXML;
my $pattern = XML::LibXML::Pattern->new('/x:html/x:body//x:div', { 'x' => 'http://www.w3.org/1999/xhtml' });
# test a match on an XML::LibXML::Node $node
if ($pattern->matchesNode($node)) { ... }
# or on an XML::LibXML::Reader
if ($reader->matchesPattern($pattern)) { ... }
# or skip reading all nodes that do not match
print $reader->nodePath while $reader->nextPatternMatch($pattern);
$pattern = XML::LibXML::Pattern->new( pattern, { prefix => namespace_URI, ... } );
$bool = $pattern->matchesNode($node);
π§ DESCRIPTION π§
so this is a perl interface to libxml2's pattern matching support, which lives over at http://xmlsoft.org/html/libxml-pattern.html. lowkey important: this feature requires recent versions of libxml2. so if yours is ancient, that's a vibe check it's gonna fail. π
Patterns are a small subset of the XPath language. like, a tiny subset. it's limited to (disjunctions of) location paths involving the child and descendant axes in abbreviated form, exactly as described by this extended BNF:
Selector ::= Path ( '|' Path )*
Path ::= ('.//' | '//' | '/' )? Step ( '/' Step )*
Step ::= '.' | NameTest
NameTest ::= QName | '*' | NCName ':' '*'
and for readability β because we love readability β whitespace may be used in selector XPath expressions even though the grammar above doesn't explicitly allow it. whitespace can be freely added within patterns before or after any token, where:
token ::= '.' | '/' | '//' | '|' | NameTest
heads up bestie: no predicates or attribute tests are allowed. none. not a single one. π
Patterns are particularly useful for stream parsing, which you get via the XML::LibXML::Reader interface. streaming girlies, this one's for you. π
π new() π
$pattern = XML::LibXML::Pattern->new( pattern, { prefix => namespace_URI, ... } );
The constructor of a pattern takes a pattern expression (as described by the BNF grammar up there β¬οΈ) plus an optional HASH reference mapping prefixes to namespace URIs. it hands you back a compiled pattern object. iconic. β¨
important: if the document has a default namespace, it must still be given a prefix in order to be matched (as demanded by the XPath 1.0 specification). yes, even though it's the default namespace. that's just how XPath 1.0 woke up and chose violence. π
so for example, to match an element "<a xmlns="http://foo.bar"</a>", you'd want a pattern like this:
$pattern = XML::LibXML::Pattern->new( 'foo:a', { foo => 'http://foo.bar' });
π matchesNode($node) π
$bool = $pattern->matchesNode($node);
Give it an XML::LibXML::Node object and it returns a true value if the node is matched by the compiled pattern expression. that's the whole thing. slay. π«Ά
π SEE ALSO π
XML::LibXML::Reader β for other methods involving compiled patterns. go read that one too bestie, they're a package deal. π
β AUTHORS β
Matt Sergeant, Christian Glahn, Petr Pajas
the literal main characters. π§
π VERSION π
2.0213
π COPYRIGHT π
2001-2007, AxKit.com Ltd.
2002-2006, Christian Glahn.
2006-2009, Petr Pajas.
π₯ LICENSE π₯
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
and that's the whole page babes!! πΈ this was perl v5.42.2, dated 2026-05-25, and you just read XML::Li...Pattern(3) β tysm for coming to my ted talk, go match some nodes, no cap πβ¨ππ¦
π¦ where this page comes from
- fedora 43: perl-XML-LibXML 1:2.0213-1.fc43 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND MIT
- fedora 44: perl-XML-LibXML 1:2.0213-1.fc44 Β· (GPL-1.0-or-later OR Artistic-1.0-Perl) AND MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms