NodeFilter(3pm)
β¨ NodeFilter π
okay bestie, so this is the manual page for XML::NodeFilter, part of the User Contributed Perl Documentation. it's a whole vibe and we're gonna get through it together, no cap.
πΈ NAME πΈ
XML::NodeFilter β the Generic XML::NodeFilter Class. that's it, that's the name, iconic.
π¦ SYNOPSIS π¦
here's the whole vibe in four lines:
use XML::NodeFilter;
my $filter = XML::NodeFilter->new();
$your_iterator->set_filter( $filter );
yes bestie that's literally it, you make one and you hand it to your iterator. slay.
π DESCRIPTION π
so, from the DOM Traversal and Range Specification itself, verbatim, and honestly it ate:
"Filters are objects that know how to "filter out" nodes. If a NodeIterator or a TreeWalker is given a NodeFilter, it applies the filter before it returns the next node. If the filter says to accept the node, the traversal logic returns it; otherwise, traversal looks for the next node and pretends that the node was rejected was not there."
this definition is given by the DOM Traversal and Range Specification. and honestly? it explains pretty well what this class is for: a XML::NodeFilter will receive a node from a traversal object β such as XML::LibXML::Iterator is one π β and tells you if the given node should be returned to the caller or not.
although I refer only to XML::LibXML here, XML::NodeFilter is implemented more open, so it can be used with other DOM implementations as well. versatility, we love to see it π«Ά
π The Spec And The Implementation π
the DOM Traversal and Range Specification just defines the constants and accept_node() for a node filter. that's all it gives you. the XML::NodeFilter implementation also adds the what_to_show() function to the class definition, since I think that it is a filter's job to decide which node-types should be shown and which not. like, that's literally the job, why would you make someone else do it.
also XML::NodeFilter adds two constants which are not part of the specification. let's talk about them because they're the spicy bits π₯
the first one is FILTER_DECLINED. it tells the traversal logic that it should apply another filter in order to decide if the node should be visible or not. while the spec only defines the traversal logic to have either one or no filter applied, it showed that it leads to cleaner code if more filters could be used in conjunction. if a traversal logic finds a single filter that returns FILTER_DECLINED, it should be handled as a synonym of FILTER_ACCEPT. while FILTER_ACCEPT is finite and would cause all others not to be executed, FILTER_DECLINED gives one more flexibility. periodt π
the second extension of the specification is the SHOW_NONE symbol. it was added for operational completeness, so one can explicitly switch the node type filter off (means all node types are rejected). this will cause the two calls of what_to_show to have a different result:
$filter->what_to_show( undef ); # will set SHOW_ALL
$filter->what_to_show( SHOW_NONE ); # will not set SHOW_ALL
see the difference? sneaky but important, don't sleep on it.
in fact SHOW_NONE is a NULL flag, that means it can be added to any list of flags without altering it.
$filter->what_to_show( SHOW_ELEMENT | SHOW_TEXT | SHOW_NONE );
is therefore identical to
$filter->what_to_show( SHOW_ELEMENT | SHOW_TEXT );
SHOW_NONE is especially useful to avoid numerically or even more ugly uninitialized values while building such flag lists dynamically. like, if you're generating flags at runtime, this is your girl π§
π How To write a Node Filter with XML::NodeFilter? π
actually writing a node filter becomes very simple with XML::NodeFilter: simply inherit your specialized node filter from XML::NodeFilter and implement the function accept_node(). this name is more perlish than the name given by the specification. if your implementation needs to stay very close to the specification, you can alternatively implement acceptNode(). implementing both functions makes no sense, since accept_node() should be preferred by the traversal logic. because of this acceptNode() will only be called if no accept_node() implementation was given.
example time, look at her:
package My::NodeFilter;
use XML::NodeFilter qw(:results);
use vars qw(@ISA);
@ISA = qw(XML::NodeFilter);
use XML::LibXML::Common;
sub accept_node {
my $filter = shift;
my $node = shift;
unless ( $node->getNodeType == ELEMENT_NODE
and defined $node->getNamespaceURI ) {
# ignore node without a defined namespace
return FILTER_REJECT;
}
return FILTER_DECLINED;
}
1;
this example shows a simple nodefilter that will reject any element without a namespace defined. note that FILTER_DECLINED is returned if the node was not rejected. this allows a traversal logic to apply another filter on the nodes with a namespace defined. if your application needs to use different filters on the namespaced elements depending on the state where you want to traverse your DOM but you need always to ignore elements without a namespace, such a filter will ensure that you need not to add redundant code to your filter or even to choose a base class. chef's kiss, honestly π
π How To make use of a XML::NodeFilter Filter? π
if you need to write some traversal code yourself, you should call the node filter's accept_node() function to test if the logic should return the current node. a node is not returned if any filter returns FILTER_SKIP or FILTER_REJECT. in this case you need to reinvoke your traversal code.
the following code snippet shows how you can make use of XML::NodeFilter in your traversal logic:
use XML::NodeFilter qw(:results);
#...
sub traversal_logic {
my $refnode = shift;
my @filters = @_;
my $node = undef;
TRAVERSE: while (1) {
my $state = FILTER_DECLINED;
# your traversal logic
# ...
last TRAVERSE unless defined $node;
FILTER: foreach my $filter ( @filters ) {
$state = $filter->accept_node($node);
last TRAVERSE if $state == FILTER_ACCEPT;
last FILTER unless $state == FILTER_DECLINED;
last TRAVERSE if $state == FILTER_DECLINED;
}
return $node;
}
as you see the traversal code will call only accept_node() on each filter. still this will work fine with filters that have acceptNode() implemented: XML::NodeFilter calls acceptNode() if the original accept_node() function is called. this assures that filters that use function names conforming to the specification will work as well. so no matter which one your fave implements, we gotchu π«Ά
note that XML::NodeFilter uses as default return value of accept_node() FILTER_ACCEPT rather than FILTER_DECLINED. this is done so you can write 100% specification conform traversal and filter logic. no cap, literally 100%.
β Functions β
new()β as the constructor of this class it accepts some parameters in form of a hash. this parameter hash will be blessed into a hash reference. the only parameter used by the class itself is-show. this parameter may hold a bitmask of node filter flags as described below or a hash reference containing the same information.
if -show is omitted SHOW_ALL is assumed as default.
what_to_show()β this function is added to the filter class, rather than assuming it is available directly from within a traversal logic.what_to_show()takes either a bitmask or a hash that holds the information what nodes should be filtered.
if what_to_show() is called without any parameter, it simply returns the bitmask in scalar context; if called in array context it returns a hash containing the corresponding information: if a bit is set in the bitmask the corresponding key has the value 1; otherwise it has the value 0.
accept_node()β this function is used to tell a calling traversal function if a given node should be returned to the caller or should be skipped. it has four possible return values:FILTER_DECLINEDto indicate that the filter itself would accept if no other (less significant) filters rejects or skips it. NOTEFILTER_DECLINEDis not defined by the spec.FILTER_ACCEPTto indicate that a node is accepted regardless what other filters may indicate.FILTER_SKIPto indicate a node is skipped, but its descendants should be still available.FILTER_REJECTto indicate a node and all its descendants should be skipped by the traversal logic.
by default accept_node() returns FILTER_ACCEPT.
acceptNode()β alternative function foraccept_node(). this is only available for spec conformance. any traversal logic should requestaccept_node(). node filter implementations may choose either to implementaccept_node()oracceptNode(). implementing both makes no sense at all! (she's dramatic but she's right)
π Constants π
FILTER_DECLINED(0)
additional symbol to allow stacked node filters.
FILTER_ACCEPT(1)
defined by the specification
FILTER_SKIP(2)
defined by the specification
FILTER_REJECT(3)
defined by the specification
SHOW_ALL
defined by the specification
SHOW_ELEMENT
defined by the specification
SHOW_ATTRIBUTE
defined by the specification
SHOW_TEXT
defined by the specification
SHOW_CDATA_SECTION
defined by the specification
SHOW_ENTITY_REFERENCE
defined by the specification
SHOW_ENTITY
defined by the specification
SHOW_PROCESSING_INSTRUCTION
defined by the specification
SHOW_COMMENT
defined by the specification
SHOW_DOCUMENT
defined by the specification
SHOW_DOCUMENT_TYPE
defined by the specification
SHOW_DOCUMENT_FRAGMENT
defined by the specification
SHOW_NOTATION
defined by the specification
SHOW_NONE
additional symbol to indicate a NULL filter. this is for operational completeness.
@FLAGNAMES
contains the names of the FLAGS used by what_to_show(). the combining symbols SHOW_ALL and SHOW_NONE are not included by this list.
%FLAGMAPPING
this hash maps flagnames (as listed by @FLAGNAMES) to their predefined values. the combining symbols SHOW_ALL and SHOW_NONE are not included by this list.
NOTE:@FLAGNAMESand%FALGMAPPINGare not exported. to make use of them you have to use the fully qualified namespace as follows
# gives the value of the SHOW_ELEMENT.
my $flag = $XML::NodeFilter::FLAGMAPPING{SHOW_ELEMENT};
π§ EXPORTS π§
XML::NodeFilter will not export any symbols at all. instead it gives two tags: :results and :flags. okay so:
:resultsβ this tag exports theFILTER_*constants. this is useful to avoid hardcoded numerical values within the filter code or the traversal logic. these symbols are used byaccept_node()and are required to indicate the state.:flagsβ exportsSHOW_*flags. import these symbols ifwhat_to_show()should be used conform to the specification rather than using named parameters.
alternatively you might import ':all' to get all symbols exported by both of the tags just described. get the whole bundle bestie π
more of this manual is coming, so don't go anywhere β part 2 is loadingβ¦ π
π AUTHOR β¨
This absolute legend right here:
- Christian Glahn, hmu at
<christian.glahn@uibk.ac.at>π«Ά
Like, the whole thing? Yeah. Him. Respect. π
π SEE ALSO π¦
Bestie, when you're done here and you want the whole cinematic universe, go read these:
perlβ yes the mothership herself πXML::LibXML::Iteratorβ the iterator, its ride-or-die companion π§XML::LibXML::NodeFilterβ the actual Perl module version of all this β¨- W3C DOM Level 2 Traversal and Range Specification β the sacred texts, the blueprint, the lore π
π COPYRIGHT AND LICENSE π
okay legal moment, but make it cute:
(c) 2002, Christian Glahn. All rights reserved.
And here's the vibe: this package is free software β you can redistribute it and/or modify it under the same terms as Perl itself. Slay, honestly. Free as in freedom, fr fr. ποΈ
and that's the whole page, bestie!! π this gem of a doc comes to you from perl v5.36.0 and it was dated 2022-12-04 β so yeah, it's giving vintage but make it iconic π NodeFilter(3pm), and honestly? obsessed. go filter some nodes, you main character πΈπ
β¨
π¦ where this page comes from
- debian bookworm: libxml-nodefilter-perl 0.01-8 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libxml-nodefilter-perl 0.01-8 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libxml-nodefilter-perl 0.01-8 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libxml-nodefilter-perl 0.01-8 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libxml-nodefilter-perl 0.01-8 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms