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


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