.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "XML::LibXML::Iterator 3pm" .TH XML::LibXML::Iterator 3pm "2020-10-24" "perl v5.30.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" XML::LibXML::Iterator \- XML::LibXML's Tree Iteration Class .SH "VERSION" .IX Header "VERSION" version 1.06 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use XML::LibXML; \& use XML::LibXML::Iterator; \& \& my $doc = XML::LibXML\->new\->parse_string( $somedata ); \& my $iter= XML::LibXML::Iterator\->new( $doc ); \& \& $iter\->iterator_function( \e&iterator_function ); \& \& # more control on the flow \& while ( $iter\->nextNode ) { \& # do something \& } \& \& # operate on the entire tree \& $iter\->iterate( \e&callback_function ); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" XML::LibXML::Iterator is an iterator class for XML::LibXML parsed documents. This class allows one to iterate the document tree as it were a linear data structure. It is possible to step back and forth between the nodes of the tree and do certain operations on that nodes. Different to XPath the nodes are not prefetched but will be calculated for each step. Therefore an iterator is sensible towards the current state of a document tree on each step, while XPath is only per query executed. .SS "What is an iterator?" .IX Subsection "What is an iterator?" XML::LibXML offers by default a W3C \s-1DOM\s0 interface on the parsed \s-1XML\s0 documents. This tree has per definition four directions to be traversed: Up, down, foreward and backward. Therefore a tree can be considered two dimensional. Although a tree is still one more simple datastructure it is way to complex for some operations. So the XML::LibXML::Iterator class breaks the for operations down to only two: backward and forward. For some people this easier to understand than \s-1DOM\s0 or \s-1SAX\s0 as this follows more the way one actually reads an \s-1XML\s0 document. .PP Therefore an iterator has three basic functions: .IP "\(bu" 4 \&\fBnextNode()\fR .IP "\(bu" 4 \&\fBcurrent()\fR .IP "\(bu" 4 \&\fBpreviousNode()\fR .PP That's it. With an iterator one does not have to decide when to dive into a subtree or find a parent. It is not even required to care about the boundaries of a certain level. The iterator will get the next node for you until there is no node left to handle. .PP In short: An iterator will answer the question about what to do next. .SS "How to use XML::LibXML::Iterator?" .IX Subsection "How to use XML::LibXML::Iterator?" XML::LibXML::Iterator requires a parsed document or at least a node to operate on. This node is passed to the iterator class and will be used as the \fBfirst\fR node of the iteration. One can always reset the iterator to the first node by using the \fBfirst()\fR\-function. .PP Once XML::LibXML::Iterator is initialized the tree can be traversed by using either \fBnext()\fR or \fBprevious()\fR. Both function will return a XML::LibXML::Node object if there is such object available. .PP Since the current object hold by the iterator class is always available via the \fBcurrent()\fR function. .PP The following example may clarify this: .PP .Vb 2 \& # get the document from wherever you like \& my $doc = XML::LibXML\->new\->parse_stream( *SOMEINPUT ); \& \& # get the iterator for the document root. \& my $iter = XML::LibXML::Iterator\->new( $doc\->documentElement ); \& \& # walk through the document \& while ( $iter\->nextNode() ) { \& my $curnode = $iter\->current(); \& print $curnode\->nodeType(); \& } \& \& # now get back to the beginning \& $iter\->first(); \& my $curnode = $iter\->current(); \& print $curnode\->nodeType(); .Ve .PP Actually the functions \fBnextNode()\fR, \fBpreviousNode()\fR, \fBfirst()\fR, \fBlast()\fR and \&\fBcurrent()\fR do return the node which is current after the operation. E.g. \fBnextNode()\fR moves to the next node if possible and then returns the node. Thus the while-loop in the example can be written as .PP .Vb 3 \& while ( $iter\->nextNode() ) { \& print $_\->nodeType(); \& } .Ve .PP Note, that just relieing on the return value of \fBnext()\fR and \fBprevious()\fR is somewhat dangerous, because both functions return \fBundef\fR in case of reaching the iteration boundaries. That means it is not possible to iterate past the last element or before the first one. .SS "Node Filters" .IX Subsection "Node Filters" XML::LibXML::Iterator accepts XML::NodeFilters to limit the nodes made available to the caller. Any nodefilter applied to XML::LibXML::Iterator will test if a node returned by the iteration function is visible to the caller. .PP Different to the \s-1DOM\s0 Traversal Specification, XML::LibXML::Iterator allows filter stacks. This means it is possible to apply more than a single node filter to your node iterator. .SS "Complex Iterations" .IX Subsection "Complex Iterations" By default XML::LibXML::Iterator will access all nodes of a given \s-1DOM\s0 tree. An interation based on the default iterator will access each single node in the given subtree once. The order how the nodes will be accessed is given by the following order: .PP .Vb 1 \& node \-> node\*(Aqs childnodes \-> node\*(Aqs next sibling .Ve .PP In combination with XML::Nodefilter this is best for a wide range of scripts and applications. Nevertheless this is still to restrictive for some applications. XML::LibXML::Iterator allows one to change that behaviour. This is done by resetting XML::LibXML::Iterator's iterator function. By using the method \fBiterator_function()\fR to override the default iterator function, it is possible to implement iterations based on any iteration rule imaginable. .PP A valid iterator function has to take two parameters: As the first parameter it will receive the iterator object itself, as second the direction of the iteration will be passed. The direction is either 1 (for \fBnext()\fR) or \-1 (for \fBprevious()\fR). As the iterator-function is called by \fBnext()\fR and \fBprevious()\fR the interator-function has to be aware about the iteration boundaries. In case the iteration would pass the boundary for that operation, the function has to return undefined. Also the iterator function has to return the new current node, instead of setting it itself. .PP *DEVELOPER NOTE* In order a single stepping is rather limited, the direction is given by the sign of the passed integer value. The value of the passed parameter will be used as an indication how many steps should be done. Therefor the interation direction should be tested relative to '0' and not as a equation. A basic template for a iterator function therefore will look like this: .PP .Vb 5 \& sub iterator_func_templ { \& my $iter = shift; \& my $step = shift; \& my $node = undef; \& my $current = $iter\->current(); \& \& if ( $step > 0 ) { \& # move forward \& } \& else { \& # move backward \& $step *= \-1; # remove the sign \& } \& \& return $node; \& } .Ve .SS "Repeated Operation" .IX Subsection "Repeated Operation" Another feature of XML::LibXML::Iterator is the ability to repeat a single operation on all nodes in scope. Instead of writing a loop one can specify the operation as a function, that it applied on each node found. The function that does the trick, is named \fBiterate()\fR. .PP \&\fBiterate()\fR takes again two parameter: First the iterator object, second the node to operate on. \fBiterate()\fR will iterate through the entire document starting with the first node. If one has already started an iteration, the internal position will be reset to the first node. .PP The following example will show how this works: .PP .Vb 1 \& $iter\->iterate( sub {my ($iter,$node)=@_; map {$iter\->setNodeName( lc $iter\->nodeName ) if $iter\->nodeType != NAMESPACE_DECLARATION } ($node, $node\->attributes); } ); .Ve .PP This extra long line lowercases all tagnames and the names of the attributes in a given subtree. .SH "METHODS" .IX Header "METHODS" .IP "\(bu" 4 new($first_node) .IP "\(bu" 4 default_iterator .IP "\(bu" 4 \&\fBfirst()\fR .IP "\(bu" 4 \&\fBnext()\fR .IP "\(bu" 4 \&\fBnextNode()\fR .IP "\(bu" 4 \&\fBprevious()\fR .IP "\(bu" 4 \&\fBpreviousNode()\fR .IP "\(bu" 4 \&\fBlast()\fR .IP "\(bu" 4 \&\fBcurrent()\fR .IP "\(bu" 4 \&\fBindex()\fR .IP "\(bu" 4 iterator_function($funcion_ref) .IP "\(bu" 4 set_filter(@filter_list) .IP "\(bu" 4 add_filter(@filter_list) .IP "\(bu" 4 iterate($function_ref) .SH "SEE ALSO" .IX Header "SEE ALSO" XML::LibXML::Node, XML::NodeFilter .SH "AUTHOR" .IX Header "AUTHOR" Christian Glahn, .SH "COPYRIGHT" .IX Header "COPYRIGHT" (c) 2002\-2007, Christian Glahn. All rights reserved. .PP This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "SUPPORT" .IX Header "SUPPORT" .SS "Websites" .IX Subsection "Websites" The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources. .IP "\(bu" 4 MetaCPAN .Sp A modern, open-source \s-1CPAN\s0 search engine, useful to view \s-1POD\s0 in \s-1HTML\s0 format. .Sp .IP "\(bu" 4 \&\s-1RT: CPAN\s0's Bug Tracker .Sp The \s-1RT\s0 ( Request Tracker ) website is the default bug/issue tracking system for \s-1CPAN.\s0 .Sp .IP "\(bu" 4 \&\s-1CPANTS\s0 .Sp The \s-1CPANTS\s0 is a website that analyzes the Kwalitee ( code metrics ) of a distribution. .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Testers .Sp The \s-1CPAN\s0 Testers is a network of smoke testers who run automated tests on uploaded \s-1CPAN\s0 distributions. .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Testers Matrix .Sp The \s-1CPAN\s0 Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms. .Sp .IP "\(bu" 4 \&\s-1CPAN\s0 Testers Dependencies .Sp The \s-1CPAN\s0 Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution. .Sp .SS "Bugs / Feature Requests" .IX Subsection "Bugs / Feature Requests" Please report any bugs or feature requests by email to \f(CW\*(C`bug\-xml\-libxml\-iterator at rt.cpan.org\*(C'\fR, or through the web interface at . You will be automatically notified of any progress on the request by the system. .SS "Source Code" .IX Subsection "Source Code" The code is open to the world, and available for you to hack on. Please feel free to browse it and play with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull from your repository :) .PP .PP .Vb 1 \& git clone git://github.com/shlomif/xml\-libxml\-iterator.git .Ve .SH "AUTHOR" .IX Header "AUTHOR" unknown .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests on the bugtracker website .PP When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2020 by unknown. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.