.\" Automatically generated by Pod::Man 4.10 (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 .\" ======================================================================== .\" .IX Title "Twig 3pm" .TH Twig 3pm "2019-03-31" "perl v5.28.1" "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::Twig \- A perl module for processing huge XML documents in tree mode. .SH "SYNOPSIS" .IX Header "SYNOPSIS" Note that this documentation is intended as a reference to the module. .PP Complete docs, including a tutorial, examples, an easier to use \s-1HTML\s0 version, a quick reference card and a \s-1FAQ\s0 are available at .PP Small documents (loaded in memory as a tree): .PP .Vb 4 \& my $twig=XML::Twig\->new(); # create the twig \& $twig\->parsefile( \*(Aqdoc.xml\*(Aq); # build it \& my_process( $twig); # use twig methods to process it \& $twig\->print; # output the twig .Ve .PP Huge documents (processed in combined stream/tree mode): .PP .Vb 10 \& # at most one div will be loaded in memory \& my $twig=XML::Twig\->new( \& twig_handlers => \& { title => sub { $_\->set_tag( \*(Aqh2\*(Aq) }, # change title tags to h2 \& # $_ is the current element \& para => sub { $_\->set_tag( \*(Aqp\*(Aq) }, # change para to p \& hidden => sub { $_\->delete; }, # remove hidden elements \& list => \e&my_list_process, # process list elements \& div => sub { $_[0]\->flush; }, # output and free memory \& }, \& pretty_print => \*(Aqindented\*(Aq, # output will be nicely formatted \& empty_tags => \*(Aqhtml\*(Aq, # outputs \& ); \& $twig\->parsefile( \*(Aqmy_big.xml\*(Aq); \& \& sub my_list_process \& { my( $twig, $list)= @_; \& # ... \& } .Ve .PP See XML::Twig 101 for other ways to use the module, as a filter for example. .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides a way to process \s-1XML\s0 documents. It is build on top of \f(CW\*(C`XML::Parser\*(C'\fR. .PP The module offers a tree interface to the document, while allowing you to output the parts of it that have been completely processed. .PP It allows minimal resource (\s-1CPU\s0 and memory) usage by building the tree only for the parts of the documents that need actual processing, through the use of the \f(CW\*(C`twig_roots \*(C'\fR and \&\f(CW\*(C`twig_print_outside_roots \*(C'\fR options. The \&\f(CW\*(C`finish \*(C'\fR and \f(CW\*(C`finish_print \*(C'\fR methods also help to increase performances. .PP XML::Twig tries to make simple things easy so it tries its best to takes care of a lot of the (usually) annoying (but sometimes necessary) features that come with \s-1XML\s0 and XML::Parser. .SH "TOOLS" .IX Header "TOOLS" XML::Twig comes with a few command-line utilities: .SS "xml_pp \- xml pretty-printer" .IX Subsection "xml_pp - xml pretty-printer" \&\s-1XML\s0 pretty printer using XML::Twig .SS "xml_grep \- grep \s-1XML\s0 files looking for specific elements" .IX Subsection "xml_grep - grep XML files looking for specific elements" \&\f(CW\*(C`xml_grep\*(C'\fR does a grep on \s-1XML\s0 files. Instead of using regular expressions it uses XPath expressions (in fact the subset of XPath supported by XML::Twig). .SS "xml_split \- cut a big \s-1XML\s0 file into smaller chunks" .IX Subsection "xml_split - cut a big XML file into smaller chunks" \&\f(CW\*(C`xml_split\*(C'\fR takes a (presumably big) \s-1XML\s0 file and split it in several smaller files, based on various criteria (level in the tree, size or an XPath expression) .SS "xml_merge \- merge back \s-1XML\s0 files split with xml_split" .IX Subsection "xml_merge - merge back XML files split with xml_split" \&\f(CW\*(C`xml_merge\*(C'\fR takes several xml files that have been split using \f(CW\*(C`xml_split\*(C'\fR and recreates a single file. .SS "xml_spellcheck \- spellcheck \s-1XML\s0 files" .IX Subsection "xml_spellcheck - spellcheck XML files" \&\f(CW\*(C`xml_spellcheck\*(C'\fR lets you spell check the content of an \s-1XML\s0 file. It extracts the text (the content of elements and optionally of attributes), call a spell checker on it and then recreates the \s-1XML\s0 document. .SH "XML::Twig 101" .IX Header "XML::Twig 101" XML::Twig can be used either on \*(L"small\*(R" \s-1XML\s0 documents (that fit in memory) or on huge ones, by processing parts of the document and outputting or discarding them once they are processed. .SS "Loading an \s-1XML\s0 document and processing it" .IX Subsection "Loading an XML document and processing it" .Vb 10 \& my $t= XML::Twig\->new(); \& $t\->parse( \*(Aqtitlep 1p 2\*(Aq); \& my $root= $t\->root; \& $root\->set_tag( \*(Aqhtml\*(Aq); # change doc to html \& $title= $root\->first_child( \*(Aqtitle\*(Aq); # get the title \& $title\->set_tag( \*(Aqh1\*(Aq); # turn it into h1 \& my @para= $root\->children( \*(Aqpara\*(Aq); # get the para children \& foreach my $para (@para) \& { $para\->set_tag( \*(Aqp\*(Aq); } # turn them into p \& $t\->print; # output the document .Ve .PP Other useful methods include: .PP att: \f(CW\*(C`$elt\->{\*(Aqatt\*(Aq}\->{\*(Aqfoo\*(Aq}\*(C'\fR return the \f(CW\*(C`foo\*(C'\fR attribute for an element, .PP set_att : \f(CW\*(C`$elt\->set_att( foo => "bar")\*(C'\fR sets the \f(CW\*(C`foo\*(C'\fR attribute to the \f(CW\*(C`bar\*(C'\fR value, .PP next_sibling: \f(CW\*(C`$elt\->{next_sibling}\*(C'\fR return the next sibling in the document (in the example \f(CW\*(C`$title\->{next_sibling}\*(C'\fR is the first \&\f(CW\*(C`para\*(C'\fR, you can also (and actually should) use \&\f(CW\*(C`$elt\->next_sibling( \*(Aqpara\*(Aq)\*(C'\fR to get it .PP The document can also be transformed through the use of the cut, copy, paste and move methods: \&\f(CW\*(C`$title\->cut; $title\->paste( after => $p);\*(C'\fR for example .PP And much, much more, see XML::Twig::Elt. .SS "Processing an \s-1XML\s0 document chunk by chunk" .IX Subsection "Processing an XML document chunk by chunk" One of the strengths of XML::Twig is that it let you work with files that do not fit in memory (\s-1BTW\s0 storing an \s-1XML\s0 document in memory as a tree is quite memory-expensive, the expansion factor being often around 10). .PP To do this you can define handlers, that will be called once a specific element has been completely parsed. In these handlers you can access the element and process it as you see fit, using the navigation and the cut-n-paste methods, plus lots of convenient ones like \f(CW\*(C`prefix \*(C'\fR. Once the element is completely processed you can then \f(CW\*(C`flush \*(C'\fR it, which will output it and free the memory. You can also \f(CW\*(C`purge \*(C'\fR it if you don't need to output it (if you are just extracting some data from the document for example). The handler will be called again once the next relevant element has been parsed. .PP .Vb 6 \& my $t= XML::Twig\->new( twig_handlers => \& { section => \e§ion, \& para => sub { $_\->set_tag( \*(Aqp\*(Aq); } \& }, \& ); \& $t\->parsefile( \*(Aqdoc.xml\*(Aq); \& \& # the handler is called once a section is completely parsed, ie when \& # the end tag for section is found, it receives the twig itself and \& # the element (including all its sub\-elements) as arguments \& sub section \& { my( $t, $section)= @_; # arguments for all twig_handlers \& $section\->set_tag( \*(Aqdiv\*(Aq); # change the tag name \& # let\*(Aqs use the attribute nb as a prefix to the title \& my $title= $section\->first_child( \*(Aqtitle\*(Aq); # find the title \& my $nb= $title\->{\*(Aqatt\*(Aq}\->{\*(Aqnb\*(Aq}; # get the attribute \& $title\->prefix( "$nb \- "); # easy isn\*(Aqt it? \& $section\->flush; # outputs the section and frees memory \& } .Ve .PP There is of course more to it: you can trigger handlers on more elaborate conditions than just the name of the element, \f(CW\*(C`section/title\*(C'\fR for example. .PP .Vb 4 \& my $t= XML::Twig\->new( twig_handlers => \& { \*(Aqsection/title\*(Aq => sub { $_\->print } } \& ) \& \->parsefile( \*(Aqdoc.xml\*(Aq); .Ve .PP Here \f(CW\*(C`sub { $_\->print }\*(C'\fR simply prints the current element (\f(CW$_\fR is aliased to the element in the handler). .PP You can also trigger a handler on a test on an attribute: .PP .Vb 4 \& my $t= XML::Twig\->new( twig_handlers => \& { \*(Aqsection[@level="1"]\*(Aq => sub { $_\->print } } \& ); \& \->parsefile( \*(Aqdoc.xml\*(Aq); .Ve .PP You can also use \f(CW\*(C`start_tag_handlers \*(C'\fR to process an element as soon as the start tag is found. Besides \f(CW\*(C`prefix \*(C'\fR you can also use \f(CW\*(C`suffix \*(C'\fR, .SS "Processing just parts of an \s-1XML\s0 document" .IX Subsection "Processing just parts of an XML document" The twig_roots mode builds only the required sub-trees from the document Anything outside of the twig roots will just be ignored: .PP .Vb 7 \& my $t= XML::Twig\->new( \& # the twig will include just the root and selected titles \& twig_roots => { \*(Aqsection/title\*(Aq => \e&print_n_purge, \& \*(Aqannex/title\*(Aq => \e&print_n_purge \& } \& ); \& $t\->parsefile( \*(Aqdoc.xml\*(Aq); \& \& sub print_n_purge \& { my( $t, $elt)= @_; \& print $elt\->text; # print the text (including sub\-element texts) \& $t\->purge; # frees the memory \& } .Ve .PP You can use that mode when you want to process parts of a documents but are not interested in the rest and you don't want to pay the price, either in time or memory, to build the tree for the it. .SS "Building an \s-1XML\s0 filter" .IX Subsection "Building an XML filter" You can combine the \f(CW\*(C`twig_roots\*(C'\fR and the \f(CW\*(C`twig_print_outside_roots\*(C'\fR options to build filters, which let you modify selected elements and will output the rest of the document as is. .PP This would convert prices in $ to prices in Euro in a document: .PP .Vb 5 \& my $t= XML::Twig\->new( \& twig_roots => { \*(Aqprice\*(Aq => \e&convert, }, # process prices \& twig_print_outside_roots => 1, # print the rest \& ); \& $t\->parsefile( \*(Aqdoc.xml\*(Aq); \& \& sub convert \& { my( $t, $price)= @_; \& my $currency= $price\->{\*(Aqatt\*(Aq}\->{\*(Aqcurrency\*(Aq}; # get the currency \& if( $currency eq \*(AqUSD\*(Aq) \& { $usd_price= $price\->text; # get the price \& # %rate is just a conversion table \& my $euro_price= $usd_price * $rate{usd2euro}; \& $price\->set_text( $euro_price); # set the new price \& $price\->set_att( currency => \*(AqEUR\*(Aq); # don\*(Aqt forget this! \& } \& $price\->print; # output the price \& } .Ve .SS "XML::Twig and various versions of Perl, XML::Parser and expat:" .IX Subsection "XML::Twig and various versions of Perl, XML::Parser and expat:" XML::Twig is a lot more sensitive to variations in versions of perl, XML::Parser and expat than to the \s-1OS,\s0 so this should cover some reasonable configurations. .PP The \*(L"recommended configuration\*(R" is perl 5.8.3+ (for good Unicode support), XML::Parser 2.31+ and expat 1.95.5+ .PP See for the \&\s-1CPAN\s0 testers reports on XML::Twig, which list all tested configurations. .PP An Atom feed of the \s-1CPAN\s0 Testers results is available at .PP Finally: .IP "XML::Twig does \fB\s-1NOT\s0\fR work with expat 1.95.4" 4 .IX Item "XML::Twig does NOT work with expat 1.95.4" .PD 0 .IP "XML::Twig only works with XML::Parser 2.27 in perl 5.6.*" 4 .IX Item "XML::Twig only works with XML::Parser 2.27 in perl 5.6.*" .PD Note that I can't compile XML::Parser 2.27 anymore, so I can't guarantee that it still works .IP "XML::Parser 2.28 does not really work" 4 .IX Item "XML::Parser 2.28 does not really work" .PP When in doubt, upgrade expat, XML::Parser and Scalar::Util .PP Finally, for some optional features, XML::Twig depends on some additional modules. The complete list, which depends somewhat on the version of Perl that you are running, is given by running \f(CW\*(C`t/zz_dump_config.t\*(C'\fR .SH "Simplifying XML processing" .IX Header "Simplifying XML processing" .IP "Whitespaces" 4 .IX Item "Whitespaces" Whitespaces that look non-significant are discarded, this behaviour can be controlled using the \f(CW\*(C`keep_spaces \*(C'\fR, \&\f(CW\*(C`keep_spaces_in \*(C'\fR and \&\f(CW\*(C`discard_spaces_in \*(C'\fR options. .IP "Encoding" 4 .IX Item "Encoding" You can specify that you want the output in the same encoding as the input (provided you have valid \s-1XML,\s0 which means you have to specify the encoding either in the document or when you create the Twig object) using the \&\f(CW\*(C`keep_encoding \*(C'\fR option .Sp You can also use \f(CW\*(C`output_encoding\*(C'\fR to convert the internal \s-1UTF\-8\s0 format to the required encoding. .IP "Comments and Processing Instructions (\s-1PI\s0)" 4 .IX Item "Comments and Processing Instructions (PI)" Comments and \s-1PI\s0's can be hidden from the processing, but still appear in the output (they are carried by the \*(L"real\*(R" element closer to them) .IP "Pretty Printing" 4 .IX Item "Pretty Printing" XML::Twig can output the document pretty printed so it is easier to read for us humans. .IP "Surviving an untimely death" 4 .IX Item "Surviving an untimely death" \&\s-1XML\s0 parsers are supposed to react violently when fed improper \s-1XML.\s0 XML::Parser just dies. .Sp XML::Twig provides the \f(CW\*(C`safe_parse \*(C'\fR and the \&\f(CW\*(C`safe_parsefile \*(C'\fR methods which wrap the parse in an eval and return either the parsed twig or 0 in case of failure. .IP "Private attributes" 4 .IX Item "Private attributes" Attributes with a name starting with # (illegal in \s-1XML\s0) will not be output, so you can safely use them to store temporary values during processing. Note that you can store anything in a private attribute, not just text, it's just a regular Perl variable, so a reference to an object or a huge data structure is perfectly fine. .SH "CLASSES" .IX Header "CLASSES" XML::Twig uses a very limited number of classes. The ones you are most likely to use are \f(CW\*(C`XML::Twig\*(C'\fR of course, which represents a complete \s-1XML\s0 document, including the document itself (the root of the document itself is \f(CW\*(C`root\*(C'\fR), its handlers, its input or output filters... The other main class is \f(CW\*(C`XML::Twig::Elt\*(C'\fR, which models an \s-1XML\s0 element. Element here has a very wide definition: it can be a regular element, or but also text, with an element \f(CW\*(C`tag\*(C'\fR of \f(CW\*(C`#PCDATA\*(C'\fR (or \f(CW\*(C`#CDATA\*(C'\fR), an entity (tag is \&\f(CW\*(C`#ENT\*(C'\fR), a Processing Instruction (\f(CW\*(C`#PI\*(C'\fR), a comment (\f(CW\*(C`#COMMENT\*(C'\fR). .PP Those are the 2 commonly used classes. .PP You might want to look the \f(CW\*(C`elt_class\*(C'\fR option if you want to subclass \f(CW\*(C`XML::Twig::Elt\*(C'\fR. .PP Attributes are just attached to their parent element, they are not objects per se. (Please use the provided methods \f(CW\*(C`att\*(C'\fR and \f(CW\*(C`set_att\*(C'\fR to access them, if you access them as a hash, then your code becomes implementation dependent and might break in the future). .PP Other classes that are seldom used are \f(CW\*(C`XML::Twig::Entity_list\*(C'\fR and \f(CW\*(C`XML::Twig::Entity\*(C'\fR. .PP If you use \f(CW\*(C`XML::Twig::XPath\*(C'\fR instead of \f(CW\*(C`XML::Twig\*(C'\fR, elements are then created as \&\f(CW\*(C`XML::Twig::XPath::Elt\*(C'\fR .SH "METHODS" .IX Header "METHODS" .SS "XML::Twig" .IX Subsection "XML::Twig" A twig is a subclass of XML::Parser, so all XML::Parser methods can be called on a twig object, including parse and parsefile. \&\f(CW\*(C`setHandlers\*(C'\fR on the other hand cannot be used, see \f(CW\*(C`BUGS \*(C'\fR .IP "new" 4 .IX Item "new" This is a class method, the constructor for XML::Twig. Options are passed as keyword value pairs. Recognized options are the same as XML::Parser, plus some (in fact a lot!) XML::Twig specifics. .Sp New Options: .RS 4 .IP "twig_handlers" 4 .IX Item "twig_handlers" This argument consists of a hash \f(CW\*(C`{ expression =\*(C'\fR \e&handler}> where expression is a an \fIXPath-like expression\fR (+ some others). .Sp XPath expressions are limited to using the child and descendant axis (indeed you can't specify an axis), and predicates cannot be nested. You can use the \f(CW\*(C`string\*(C'\fR, or \f(CW\*(C`string()\*(C'\fR function (except in \f(CW\*(C`twig_roots\*(C'\fR triggers). .Sp Additionally you can use regexps (/ delimited) to match attribute and string values. .Sp Examples: .Sp .Vb 8 \& foo \& foo/bar \& foo//bar \& /foo/bar \& /foo//bar \& /foo/bar[@att1 = "val1" and @att2 = "val2"]/baz[@a >= 1] \& foo[string()=~ /^duh!+/] \& /foo[string(bar)=~ /\ed+/]/baz[@att != 3] .Ve .Sp #CDATA can be used to call a handler for a \s-1CDATA\s0 section. #COMMENT can be used to call a handler for comments .Sp Some additional (non-XPath) expressions are also provided for convenience: .RS 4 .IP "processing instructions" 4 .IX Item "processing instructions" \&\f(CW\*(Aq?\*(Aq\fR or \f(CW\*(Aq#PI\*(Aq\fR triggers the handler for any processing instruction, and \f(CW\*(Aq?\*(Aq\fR or \f(CW\*(Aq#PI \*(Aq\fR triggers a handler for processing instruction with the given target( ex: \f(CW\*(Aq#PI xml\-stylesheet\*(Aq\fR). .IP "level()" 4 .IX Item "level()" Triggers the handler on any element at that level in the tree (root is level 1) .IP "_all_" 4 .IX Item "_all_" Triggers the handler for \fBall\fR elements in the tree .IP "_default_" 4 .IX Item "_default_" Triggers the handler for each element that does \s-1NOT\s0 have any other handler. .RE .RS 4 .Sp Expressions are evaluated against the input document. Which means that even if you have changed the tag of an element (changing the tag of a parent element from a handler for example) the change will not impact the expression evaluation. There is an exception to this: \*(L"private\*(R" attributes (which name start with a '#', and can only be created during the parsing, as they are not valid \s-1XML\s0) are checked against the current twig. .Sp Handlers are triggered in fixed order, sorted by their type (xpath expressions first, then regexps, then level), then by whether they specify a full path (starting at the root element) or not, then by number of steps in the expression , then number of predicates, then number of tests in predicates. Handlers where the last step does not specify a step (\f(CW\*(C`foo/bar/*\*(C'\fR) are triggered after other XPath handlers. Finally \f(CW\*(C`_all_\*(C'\fR handlers are triggered last. .Sp \&\fBImportant\fR: once a handler has been triggered if it returns 0 then no other handler is called, except a \f(CW\*(C`_all_\*(C'\fR handler which will be called anyway. .Sp If a handler returns a true value and other handlers apply, then the next applicable handler will be called. Repeat, rinse, lather..; The exception to that rule is when the \f(CW\*(C`do_not_chain_handlers\*(C'\fR option is set, in which case only the first handler will be called. .Sp Note that it might be a good idea to explicitly return a short true value (like 1) from handlers: this ensures that other applicable handlers are called even if the last statement for the handler happens to evaluate to false. This might also speedup the code by avoiding the result of the last statement of the code to be copied and passed to the code managing handlers. It can really pay to have 1 instead of a long string returned. .Sp When the closing tag for an element is parsed the corresponding handler is called, with 2 arguments: the twig and the \f(CW\*(C`Element \*(C'\fR. The twig includes the document tree that has been built so far, the element is the complete sub-tree for the element. The fact that the handler is called only when the closing tag for the element is found means that handlers for inner elements are called before handlers for outer elements. .Sp \&\f(CW$_\fR is also set to the element, so it is easy to write inline handlers like .Sp .Vb 1 \& para => sub { $_\->set_tag( \*(Aqp\*(Aq); } .Ve .Sp Text is stored in elements whose tag name is #PCDATA (due to mixed content, text and sub-element in an element there is no way to store the text as just an attribute of the enclosing element). .Sp \&\fBWarning\fR: if you have used purge or flush on the twig the element might not be complete, some of its children might have been entirely flushed or purged, and the start tag might even have been printed (by \f(CW\*(C`flush\*(C'\fR) already, so changing its tag might not give the expected result. .RE .IP "twig_roots" 4 .IX Item "twig_roots" This argument let's you build the tree only for those elements you are interested in. .Sp .Vb 4 \& Example: my $t= XML::Twig\->new( twig_roots => { title => 1, subtitle => 1}); \& $t\->parsefile( file); \& my $t= XML::Twig\->new( twig_roots => { \*(Aqsection/title\*(Aq => 1}); \& $t\->parsefile( file); .Ve .Sp return a twig containing a document including only \f(CW\*(C`title\*(C'\fR and \f(CW\*(C`subtitle\*(C'\fR elements, as children of the root element. .Sp You can use \fIgeneric_attribute_condition\fR, \fIattribute_condition\fR, \&\fIfull_path\fR, \fIpartial_path\fR, \fItag\fR, \fItag_regexp\fR, \fI_default_\fR and \&\fI_all_\fR to trigger the building of the twig. \&\fIstring_condition\fR and \fIregexp_condition\fR cannot be used as the content of the element, and the string, have not yet been parsed when the condition is checked. .Sp \&\fB\s-1WARNING\s0\fR: path are checked for the document. Even if the \f(CW\*(C`twig_roots\*(C'\fR option is used they will be checked against the full document tree, not the virtual tree created by XML::Twig .Sp \&\fB\s-1WARNING\s0\fR: twig_roots elements should \s-1NOT\s0 be nested, that would hopelessly confuse XML::Twig ;\-\-( .Sp Note: you can set handlers (twig_handlers) using twig_roots Example: my \f(CW$t\fR= XML::Twig\->new( twig_roots => { title => sub { \f(CW$_\fR[1]\->print;}, subtitle => \e&process_subtitle } ); \f(CW$t\fR\->parsefile( file); .IP "twig_print_outside_roots" 4 .IX Item "twig_print_outside_roots" To be used in conjunction with the \f(CW\*(C`twig_roots\*(C'\fR argument. When set to a true value this will print the document outside of the \f(CW\*(C`twig_roots\*(C'\fR elements. .Sp .Vb 12 \& Example: my $t= XML::Twig\->new( twig_roots => { title => \e&number_title }, \& twig_print_outside_roots => 1, \& ); \& $t\->parsefile( file); \& { my $nb; \& sub number_title \& { my( $twig, $title); \& $nb++; \& $title\->prefix( "$nb "); \& $title\->print; \& } \& } .Ve .Sp This example prints the document outside of the title element, calls \&\f(CW\*(C`number_title\*(C'\fR for each \f(CW\*(C`title\*(C'\fR element, prints it, and then resumes printing the document. The twig is built only for the \f(CW\*(C`title\*(C'\fR elements. .Sp If the value is a reference to a file handle then the document outside the \&\f(CW\*(C`twig_roots\*(C'\fR elements will be output to this file handle: .Sp .Vb 5 \& open( my $out, \*(Aq>\*(Aq, \*(Aqout_file.xml\*(Aq) or die "cannot open out file.xml out_file:$!"; \& my $t= XML::Twig\->new( twig_roots => { title => \e&number_title }, \& # default output to $out \& twig_print_outside_roots => $out, \& ); \& \& { my $nb; \& sub number_title \& { my( $twig, $title); \& $nb++; \& $title\->prefix( "$nb "); \& $title\->print( $out); # you have to print to \e*OUT here \& } \& } .Ve .IP "start_tag_handlers" 4 .IX Item "start_tag_handlers" A hash \f(CW\*(C`{ expression =\*(C'\fR \e&handler}>. Sets element handlers that are called when the element is open (at the end of the XML::Parser \f(CW\*(C`Start\*(C'\fR handler). The handlers are called with 2 params: the twig and the element. The element is empty at that point, its attributes are created though. .Sp You can use \fIgeneric_attribute_condition\fR, \fIattribute_condition\fR, \&\fIfull_path\fR, \fIpartial_path\fR, \fItag\fR, \fItag_regexp\fR, \fI_default_\fR and \fI_all_\fR to trigger the handler. .Sp \&\fIstring_condition\fR and \fIregexp_condition\fR cannot be used as the content of the element, and the string, have not yet been parsed when the condition is checked. .Sp The main uses for those handlers are to change the tag name (you might have to do it as soon as you find the open tag if you plan to \f(CW\*(C`flush\*(C'\fR the twig at some point in the element, and to create temporary attributes that will be used when processing sub-element with \f(CW\*(C`twig_hanlders\*(C'\fR. .Sp You should also use it to change tags if you use \f(CW\*(C`flush\*(C'\fR. If you change the tag in a regular \f(CW\*(C`twig_handler\*(C'\fR then the start tag might already have been flushed. .Sp \&\fBNote\fR: \f(CW\*(C`start_tag\*(C'\fR handlers can be called outside of \f(CW\*(C`twig_roots\*(C'\fR if this argument is used, in this case handlers are called with the following arguments: \&\f(CW$t\fR (the twig), \f(CW$tag\fR (the tag of the element) and \f(CW%att\fR (a hash of the attributes of the element). .Sp If the \f(CW\*(C`twig_print_outside_roots\*(C'\fR argument is also used, if the last handler called returns a \f(CW\*(C`true\*(C'\fR value, then the start tag will be output as it appeared in the original document, if the handler returns a \f(CW\*(C`false\*(C'\fR value then the start tag will \fBnot\fR be printed (so you can print a modified string yourself for example). .Sp Note that you can use the ignore method in \f(CW\*(C`start_tag_handlers\*(C'\fR (and only there). .IP "end_tag_handlers" 4 .IX Item "end_tag_handlers" A hash \f(CW\*(C`{ expression =\*(C'\fR \e&handler}>. Sets element handlers that are called when the element is closed (at the end of the XML::Parser \f(CW\*(C`End\*(C'\fR handler). The handlers are called with 2 params: the twig and the tag of the element. .Sp \&\fItwig_handlers\fR are called when an element is completely parsed, so why have this redundant option? There is only one use for \f(CW\*(C`end_tag_handlers\*(C'\fR: when using the \f(CW\*(C`twig_roots\*(C'\fR option, to trigger a handler for an element \fBoutside\fR the roots. It is for example very useful to number titles in a document using nested sections: .Sp .Vb 11 \& my @no= (0); \& my $no; \& my $t= XML::Twig\->new( \& start_tag_handlers => \& { section => sub { $no[$#no]++; $no= join \*(Aq.\*(Aq, @no; push @no, 0; } }, \& twig_roots => \& { title => sub { $_[1]\->prefix( $no); $_[1]\->print; } }, \& end_tag_handlers => { section => sub { pop @no; } }, \& twig_print_outside_roots => 1 \& ); \& $t\->parsefile( $file); .Ve .Sp Using the \f(CW\*(C`end_tag_handlers\*(C'\fR argument without \f(CW\*(C`twig_roots\*(C'\fR will result in an error. .IP "do_not_chain_handlers" 4 .IX Item "do_not_chain_handlers" If this option is set to a true value, then only one handler will be called for each element, even if several satisfy the condition .Sp Note that the \f(CW\*(C`_all_\*(C'\fR handler will still be called regardless .IP "ignore_elts" 4 .IX Item "ignore_elts" This option lets you ignore elements when building the twig. This is useful in cases where you cannot use \f(CW\*(C`twig_roots\*(C'\fR to ignore elements, for example if the element to ignore is a sibling of elements you are interested in. .Sp Example: .Sp .Vb 2 \& my $twig= XML::Twig\->new( ignore_elts => { elt => \*(Aqdiscard\*(Aq }); \& $twig\->parsefile( \*(Aqdoc.xml\*(Aq); .Ve .Sp This will build the complete twig for the document, except that all \f(CW\*(C`elt\*(C'\fR elements (and their children) will be left out. .Sp The keys in the hash are triggers, limited to the same subset as \&\f(CW\*(C`start_tag_handlers\*(C'\fR. The values can be \f(CW\*(C`discard\*(C'\fR, to discard the element, \f(CW\*(C`print\*(C'\fR, to output the element as-is, \f(CW\*(C`string\*(C'\fR to store the text of the ignored element(s), including markup, in a field of the twig: \f(CW\*(C`$t\->{twig_buffered_string}\*(C'\fR or a reference to a scalar, in which case the text of the ignored element(s), including markup, will be stored in the scalar. Any other value will be treated as \f(CW\*(C`discard\*(C'\fR. .IP "char_handler" 4 .IX Item "char_handler" A reference to a subroutine that will be called every time \f(CW\*(C`PCDATA\*(C'\fR is found. .Sp The subroutine receives the string as argument, and returns the modified string: .Sp .Vb 6 \& # we want all strings in upper case \& sub my_char_handler \& { my( $text)= @_; \& $text= uc( $text); \& return $text; \& } .Ve .IP "elt_class" 4 .IX Item "elt_class" The name of a class used to store elements. this class should inherit from \&\f(CW\*(C`XML::Twig::Elt\*(C'\fR (and by default it is \f(CW\*(C`XML::Twig::Elt\*(C'\fR). This option is used to subclass the element class and extend it with new methods. .Sp This option is needed because during the parsing of the \s-1XML,\s0 elements are created by \f(CW\*(C`XML::Twig\*(C'\fR, without any control from the user code. .IP "keep_atts_order" 4 .IX Item "keep_atts_order" Setting this option to a true value causes the attribute hash to be tied to a \f(CW\*(C`Tie::IxHash\*(C'\fR object. This means that \f(CW\*(C`Tie::IxHash\*(C'\fR needs to be installed for this option to be available. It also means that the hash keeps its order, so you will get the attributes in order. This allows outputting the attributes in the same order as they were in the original document. .IP "keep_encoding" 4 .IX Item "keep_encoding" This is a (slightly?) evil option: if the \s-1XML\s0 document is not \s-1UTF\-8\s0 encoded and you want to keep it that way, then setting keep_encoding will use the\f(CW\*(C`Expat\*(C'\fR original_string method for character, thus keeping the original encoding, as well as the original entities in the strings. .Sp See the \f(CW\*(C`t/test6.t\*(C'\fR test file to see what results you can expect from the various encoding options. .Sp \&\fB\s-1WARNING\s0\fR: if the original encoding is multi-byte then attribute parsing will be \s-1EXTREMELY\s0 unsafe under any Perl before 5.6, as it uses regular expressions which do not deal properly with multi-byte characters. You can specify an alternate function to parse the start tags with the \f(CW\*(C`parse_start_tag\*(C'\fR option (see below) .Sp \&\fB\s-1WARNING\s0\fR: this option is \s-1NOT\s0 used when parsing with the non-blocking parser (\f(CW\*(C`parse_start\*(C'\fR, \f(CW\*(C`parse_more\*(C'\fR, parse_done methods) which you probably should not use with XML::Twig anyway as they are totally untested! .IP "output_encoding" 4 .IX Item "output_encoding" This option generates an output_filter using \f(CW\*(C`Encode\*(C'\fR, \f(CW\*(C`Text::Iconv\*(C'\fR or \&\f(CW\*(C`Unicode::Map8\*(C'\fR and \f(CW\*(C`Unicode::Strings\*(C'\fR, and sets the encoding in the \s-1XML\s0 declaration. This is the easiest way to deal with encodings, if you need more sophisticated features, look at \f(CW\*(C`output_filter\*(C'\fR below .IP "output_filter" 4 .IX Item "output_filter" This option is used to convert the character encoding of the output document. It is passed either a string corresponding to a predefined filter or a subroutine reference. The filter will be called every time a document or element is processed by the \*(L"print\*(R" functions (\f(CW\*(C`print\*(C'\fR, \f(CW\*(C`sprint\*(C'\fR, \f(CW\*(C`flush\*(C'\fR). .Sp Pre-defined filters: .RS 4 .IP "latin1" 4 .IX Item "latin1" uses either \f(CW\*(C`Encode\*(C'\fR, \f(CW\*(C`Text::Iconv\*(C'\fR or \f(CW\*(C`Unicode::Map8\*(C'\fR and \f(CW\*(C`Unicode::String\*(C'\fR or a regexp (which works only with XML::Parser 2.27), in this order, to convert all characters to \s-1ISO\-8859\-15\s0 (usually latin1 is synonym to \s-1ISO\-8859\-1,\s0 but in practice it seems that \s-1ISO\-8859\-15,\s0 which includes the euro sign, is more useful and probably what most people want). .IP "html" 4 .IX Item "html" does the same conversion as \f(CW\*(C`latin1\*(C'\fR, plus encodes entities using \&\f(CW\*(C`HTML::Entities\*(C'\fR (oddly enough you will need to have HTML::Entities installed for it to be available). This should only be used if the tags and attribute names themselves are in US-ASCII, or they will be converted and the output will not be valid \s-1XML\s0 any more .IP "safe" 4 .IX Item "safe" converts the output to \s-1ASCII\s0 (\s-1US\s0) only plus \fIcharacter entities\fR (\f(CW\*(C`&#nnn;\*(C'\fR) this should be used only if the tags and attribute names themselves are in US-ASCII, or they will be converted and the output will not be valid \s-1XML\s0 any more .IP "safe_hex" 4 .IX Item "safe_hex" same as \f(CW\*(C`safe\*(C'\fR except that the character entities are in hex (\f(CW\*(C`&#xnnn;\*(C'\fR) .IP "encode_convert ($encoding)" 4 .IX Item "encode_convert ($encoding)" Return a subref that can be used to convert utf8 strings to \f(CW$encoding\fR). Uses \f(CW\*(C`Encode\*(C'\fR. .Sp .Vb 2 \& my $conv = XML::Twig::encode_convert( \*(Aqlatin1\*(Aq); \& my $t = XML::Twig\->new(output_filter => $conv); .Ve .IP "iconv_convert ($encoding)" 4 .IX Item "iconv_convert ($encoding)" this function is used to create a filter subroutine that will be used to convert the characters to the target encoding using \f(CW\*(C`Text::Iconv\*(C'\fR (which needs to be installed, look at the documentation for the module and for the \&\f(CW\*(C`iconv\*(C'\fR library to find out which encodings are available on your system) .Sp .Vb 2 \& my $conv = XML::Twig::iconv_convert( \*(Aqlatin1\*(Aq); \& my $t = XML::Twig\->new(output_filter => $conv); .Ve .IP "unicode_convert ($encoding)" 4 .IX Item "unicode_convert ($encoding)" this function is used to create a filter subroutine that will be used to convert the characters to the target encoding using \f(CW\*(C`Unicode::Strings\*(C'\fR and \f(CW\*(C`Unicode::Map8\*(C'\fR (which need to be installed, look at the documentation for the modules to find out which encodings are available on your system) .Sp .Vb 2 \& my $conv = XML::Twig::unicode_convert( \*(Aqlatin1\*(Aq); \& my $t = XML::Twig\->new(output_filter => $conv); .Ve .RE .RS 4 .Sp The \f(CW\*(C`text\*(C'\fR and \f(CW\*(C`att\*(C'\fR methods do not use the filter, so their result are always in unicode. .Sp Those predeclared filters are based on subroutines that can be used by themselves (as \f(CW\*(C`XML::Twig::foo\*(C'\fR). .IP "html_encode ($string)" 4 .IX Item "html_encode ($string)" Use \f(CW\*(C`HTML::Entities\*(C'\fR to encode a utf8 string .IP "safe_encode ($string)" 4 .IX Item "safe_encode ($string)" Use either a regexp (perl < 5.8) or \f(CW\*(C`Encode\*(C'\fR to encode non-ascii characters in the string in \f(CW\*(C`&#;\*(C'\fR format .IP "safe_encode_hex ($string)" 4 .IX Item "safe_encode_hex ($string)" Use either a regexp (perl < 5.8) or \f(CW\*(C`Encode\*(C'\fR to encode non-ascii characters in the string in \f(CW\*(C`&#x;\*(C'\fR format .IP "regexp2latin1 ($string)" 4 .IX Item "regexp2latin1 ($string)" Use a regexp to encode a utf8 string into latin 1 (\s-1ISO\-8859\-1\s0). Does not work with Perl 5.8.0! .RE .RS 4 .RE .IP "output_text_filter" 4 .IX Item "output_text_filter" same as output_filter, except it doesn't apply to the brackets and quotes around attribute values. This is useful for all filters that could change the tagging, basically anything that does not just change the encoding of the output. \f(CW\*(C`html\*(C'\fR, \f(CW\*(C`safe\*(C'\fR and \f(CW\*(C`safe_hex\*(C'\fR are better used with this option. .IP "input_filter" 4 .IX Item "input_filter" This option is similar to \f(CW\*(C`output_filter\*(C'\fR except the filter is applied to the characters before they are stored in the twig, at parsing time. .IP "remove_cdata" 4 .IX Item "remove_cdata" Setting this option to a true value will force the twig to output \s-1CDATA\s0 sections as regular (escaped) \s-1PCDATA\s0 .IP "parse_start_tag" 4 .IX Item "parse_start_tag" If you use the \f(CW\*(C`keep_encoding\*(C'\fR option then this option can be used to replace the default parsing function. You should provide a coderef (a reference to a subroutine) as the argument, this subroutine takes the original tag (given by XML::Parser::Expat \f(CW\*(C`original_string()\*(C'\fR method) and returns a tag and the attributes in a hash (or in a list attribute_name/attribute value). .IP "expand_external_ents" 4 .IX Item "expand_external_ents" When this option is used external entities (that are defined) are expanded when the document is output using \*(L"print\*(R" functions such as \f(CW\*(C`print \*(C'\fR, \&\f(CW\*(C`sprint \*(C'\fR, \f(CW\*(C`flush \*(C'\fR and \f(CW\*(C`xml_string \*(C'\fR. Note that in the twig the entity will be stored as an element with a tag '\f(CW\*(C`#ENT\*(C'\fR', the entity will not be expanded there, so you might want to process the entities before outputting it. .Sp If an external entity is not available, then the parse will fail. .Sp A special case is when the value of this option is \-1. In that case a missing entity will not cause the parser to die, but its \f(CW\*(C`name\*(C'\fR, \f(CW\*(C`sysid\*(C'\fR and \f(CW\*(C`pubid\*(C'\fR will be stored in the twig as \f(CW\*(C`$twig\->{twig_missing_system_entities}\*(C'\fR (a reference to an array of hashes { name => , sysid => , pubid => }). Yes, this is a bit of a hack, but it's useful in some cases. .Sp \&\fB\s-1WARNING\s0\fR: setting expand_external_ents to 0 or \-1 currently doesn't work as expected; cf. . To completelty turn off expanding external entities use \f(CW\*(C`no_xxe\*(C'\fR. .IP "no_xxe" 4 .IX Item "no_xxe" If this argument is set to a true value, expanding of external entities is turned off. .IP "load_DTD" 4 .IX Item "load_DTD" If this argument is set to a true value, \f(CW\*(C`parse\*(C'\fR or \f(CW\*(C`parsefile\*(C'\fR on the twig will load the \s-1DTD\s0 information. This information can then be accessed through the twig, in a \f(CW\*(C`DTD_handler\*(C'\fR for example. This will load even an external \s-1DTD.\s0 .Sp Default and fixed values for attributes will also be filled, based on the \s-1DTD.\s0 .Sp Note that to do this the module will generate a temporary file in the current directory. If this is a problem let me know and I will add an option to specify an alternate directory. .Sp See \*(L"\s-1DTD\s0 Handling\*(R" for more information .IP "DTD_base " 4 .IX Item "DTD_base " If the \s-1DTD\s0 is in a different directory, looks for it there, useful to make up somewhat for the lack of catalog suport in \f(CW\*(C`expat\*(C'\fR. You still need a \s-1SYSTEM\s0 declaration .IP "DTD_handler" 4 .IX Item "DTD_handler" Set a handler that will be called once the doctype (and the \s-1DTD\s0) have been loaded, with 2 arguments, the twig and the \s-1DTD.\s0 .IP "no_prolog" 4 .IX Item "no_prolog" Does not output a prolog (\s-1XML\s0 declaration and \s-1DTD\s0) .IP "id" 4 .IX Item "id" This optional argument gives the name of an attribute that can be used as an \s-1ID\s0 in the document. Elements whose \s-1ID\s0 is known can be accessed through the elt_id method. id defaults to 'id'. See \f(CW\*(C`BUGS \*(C'\fR .IP "discard_spaces" 4 .IX Item "discard_spaces" If this optional argument is set to a true value then spaces are discarded when they look non-significant: strings containing only spaces and at least one line feed are discarded. This argument is set to true by default. .Sp The exact algorithm to drop spaces is: strings including only spaces (perl \es) and at least one \en right before an open or close tag are dropped. .IP "discard_all_spaces" 4 .IX Item "discard_all_spaces" If this argument is set to a true value, spaces are discarded more aggressively than with \f(CW\*(C`discard_spaces\*(C'\fR: strings not including a \en are also dropped. This option is appropriate for data-oriented \s-1XML.\s0 .IP "keep_spaces" 4 .IX Item "keep_spaces" If this optional argument is set to a true value then all spaces in the document are kept, and stored as \f(CW\*(C`PCDATA\*(C'\fR. .Sp \&\fBWarning\fR: adding this option can result in changes in the twig generated: space that was previously discarded might end up in a new text element. see the difference by calling the following code with 0 and 1 as arguments: .Sp .Vb 1 \& perl \-MXML::Twig \-e\*(Aqprint XML::Twig\->new( keep_spaces => shift)\->parse( " \en")\->_dump\*(Aq .Ve .Sp \&\f(CW\*(C`keep_spaces\*(C'\fR and \f(CW\*(C`discard_spaces\*(C'\fR cannot be both set. .IP "discard_spaces_in" 4 .IX Item "discard_spaces_in" This argument sets \f(CW\*(C`keep_spaces\*(C'\fR to true but will cause the twig builder to discard spaces in the elements listed. .Sp The syntax for using this argument is: .Sp .Vb 1 \& XML::Twig\->new( discard_spaces_in => [ \*(Aqelt1\*(Aq, \*(Aqelt2\*(Aq]); .Ve .IP "keep_spaces_in" 4 .IX Item "keep_spaces_in" This argument sets \f(CW\*(C`discard_spaces\*(C'\fR to true but will cause the twig builder to keep spaces in the elements listed. .Sp The syntax for using this argument is: .Sp .Vb 1 \& XML::Twig\->new( keep_spaces_in => [ \*(Aqelt1\*(Aq, \*(Aqelt2\*(Aq]); .Ve .Sp \&\fBWarning\fR: adding this option can result in changes in the twig generated: space that was previously discarded might end up in a new text element. .IP "pretty_print" 4 .IX Item "pretty_print" Set the pretty print method, amongst '\f(CW\*(C`none\*(C'\fR' (default), '\f(CW\*(C`nsgmls\*(C'\fR', \&'\f(CW\*(C`nice\*(C'\fR', '\f(CW\*(C`indented\*(C'\fR', '\f(CW\*(C`indented_c\*(C'\fR', '\f(CW\*(C`indented_a\*(C'\fR', \&'\f(CW\*(C`indented_close_tag\*(C'\fR', '\f(CW\*(C`cvs\*(C'\fR', '\f(CW\*(C`wrapped\*(C'\fR', '\f(CW\*(C`record\*(C'\fR' and '\f(CW\*(C`record_c\*(C'\fR' .Sp pretty_print formats: .RS 4 .IP "none" 4 .IX Item "none" The document is output as one ling string, with no line breaks except those found within text elements .IP "nsgmls" 4 .IX Item "nsgmls" Line breaks are inserted in safe places: that is within tags, between a tag and an attribute, between attributes and before the > at the end of a tag. .Sp This is quite ugly but better than \f(CW\*(C`none\*(C'\fR, and it is very safe, the document will still be valid (conforming to its \s-1DTD\s0). .Sp This is how the \s-1SGML\s0 parser \f(CW\*(C`sgmls\*(C'\fR splits documents, hence the name. .IP "nice" 4 .IX Item "nice" This option inserts line breaks before any tag that does not contain text (so element with textual content are not broken as the \en is the significant). .Sp \&\fB\s-1WARNING\s0\fR: this option leaves the document well-formed but might make it invalid (not conformant to its \s-1DTD\s0). If you have elements declared as .Sp .Vb 1 \& .Ve .Sp then a \f(CW\*(C`foo\*(C'\fR element including a \f(CW\*(C`bar\*(C'\fR one will be printed as .Sp .Vb 3 \& \& bar is just pcdata \& .Ve .Sp This is invalid, as the parser will take the line break after the \f(CW\*(C`foo\*(C'\fR tag as a sign that the element contains \s-1PCDATA,\s0 it will then die when it finds the \&\f(CW\*(C`bar\*(C'\fR tag. This may or may not be important for you, but be aware of it! .IP "indented" 4 .IX Item "indented" Same as \f(CW\*(C`nice\*(C'\fR (and with the same warning) but indents elements according to their level .IP "indented_c" 4 .IX Item "indented_c" Same as \f(CW\*(C`indented\*(C'\fR but a little more compact: the closing tags are on the same line as the preceding text .IP "indented_close_tag" 4 .IX Item "indented_close_tag" Same as \f(CW\*(C`indented\*(C'\fR except that the closing tag is also indented, to line up with the tags within the element .IP "idented_a" 4 .IX Item "idented_a" This formats \s-1XML\s0 files in a line-oriented version control friendly way. The format is described in (that's an Oracle document with an insanely long \s-1URL\s0). .Sp Note that to be totaly conformant to the \*(L"spec\*(R", the order of attributes should not be changed, so if they are not already in alphabetical order you will need to use the \f(CW\*(C`keep_atts_order\*(C'\fR option. .IP "cvs" 4 .IX Item "cvs" Same as \f(CW\*(C`idented_a\*(C'\fR. .IP "wrapped" 4 .IX Item "wrapped" Same as \f(CW\*(C`indented_c\*(C'\fR but lines are wrapped using Text::Wrap::wrap. The default length for lines is the default for \f(CW$Text::Wrap::columns\fR, and can be changed by changing that variable. .IP "record" 4 .IX Item "record" This is a record-oriented pretty print, that display data in records, one field per line (which looks a \s-1LOT\s0 like \f(CW\*(C`indented\*(C'\fR) .IP "record_c" 4 .IX Item "record_c" Stands for record compact, one record per line .RE .RS 4 .RE .IP "empty_tags" 4 .IX Item "empty_tags" Set the empty tag display style ('\f(CW\*(C`normal\*(C'\fR', '\f(CW\*(C`html\*(C'\fR' or '\f(CW\*(C`expand\*(C'\fR'). .Sp \&\f(CW\*(C`normal\*(C'\fR outputs an empty tag '\f(CW\*(C`\*(C'\fR', \f(CW\*(C`html\*(C'\fR adds a space \&'\f(CW\*(C`\*(C'\fR' for elements that can be empty in \s-1XHTML\s0 and \f(CW\*(C`expand\*(C'\fR outputs \&'\f(CW\*(C`\*(C'\fR' .IP "quote" 4 .IX Item "quote" Set the quote character for attributes ('\f(CW\*(C`single\*(C'\fR' or '\f(CW\*(C`double\*(C'\fR'). .IP "escape_gt" 4 .IX Item "escape_gt" By default XML::Twig does not escape the character > in its output, as it is not mandated by the \s-1XML\s0 spec. With this option on, > will be replaced by \f(CW\*(C`>\*(C'\fR .IP "comments" 4 .IX Item "comments" Set the way comments are processed: '\f(CW\*(C`drop\*(C'\fR' (default), '\f(CW\*(C`keep\*(C'\fR' or \&'\f(CW\*(C`process\*(C'\fR' .Sp Comments processing options: .RS 4 .IP "drop" 4 .IX Item "drop" drops the comments, they are not read, nor printed to the output .IP "keep" 4 .IX Item "keep" comments are loaded and will appear on the output, they are not accessible within the twig and will not interfere with processing though .Sp \&\fBNote\fR: comments in the middle of a text element such as .Sp .Vb 1 \&

text more text \-\->

.Ve .Sp are kept at their original position in the text. Using \*(L"print\*(R" methods like \f(CW\*(C`print\*(C'\fR or \f(CW\*(C`sprint\*(C'\fR will return the comments in the text. Using \f(CW\*(C`text\*(C'\fR or \f(CW\*(C`field\*(C'\fR on the other hand will not. .Sp Any use of \f(CW\*(C`set_pcdata\*(C'\fR on the \f(CW\*(C`#PCDATA\*(C'\fR element (directly or through other methods like \f(CW\*(C`set_content\*(C'\fR) will delete the comment(s). .IP "process" 4 .IX Item "process" comments are loaded in the twig and will be treated as regular elements (their \f(CW\*(C`tag\*(C'\fR is \f(CW\*(C`#COMMENT\*(C'\fR) this can interfere with processing if you expect \f(CW\*(C`$elt\->{first_child}\*(C'\fR to be an element but find a comment there. Validation will not protect you from this as comments can happen anywhere. You can use \f(CW\*(C`$elt\->first_child( \*(Aqtag\*(Aq)\*(C'\fR (which is a good habit anyway) to get where you want. .Sp Consider using \f(CW\*(C`process\*(C'\fR if you are outputting \s-1SAX\s0 events from XML::Twig. .RE .RS 4 .RE .IP "pi" 4 .IX Item "pi" Set the way processing instructions are processed: '\f(CW\*(C`drop\*(C'\fR', '\f(CW\*(C`keep\*(C'\fR' (default) or '\f(CW\*(C`process\*(C'\fR' .Sp Note that you can also set \s-1PI\s0 handlers in the \f(CW\*(C`twig_handlers\*(C'\fR option: .Sp .Vb 2 \& \*(Aq?\*(Aq => \e&handler \& \*(Aq?target\*(Aq => \e&handler 2 .Ve .Sp The handlers will be called with 2 parameters, the twig and the \s-1PI\s0 element if \&\f(CW\*(C`pi\*(C'\fR is set to \f(CW\*(C`process\*(C'\fR, and with 3, the twig, the target and the data if \&\f(CW\*(C`pi\*(C'\fR is set to \f(CW\*(C`keep\*(C'\fR. Of course they will not be called if \f(CW\*(C`pi\*(C'\fR is set to \&\f(CW\*(C`drop\*(C'\fR. .Sp If \f(CW\*(C`pi\*(C'\fR is set to \f(CW\*(C`keep\*(C'\fR the handler should return a string that will be used as-is as the \s-1PI\s0 text (it should look like "\f(CW\*(C` " or '' if you want to remove the \s-1PI\s0), .Sp Only one handler will be called, \f(CW\*(C`?target\*(C'\fR or \f(CW\*(C`?\*(C'\fR if no specific handler for that target is available. .IP "map_xmlns" 4 .IX Item "map_xmlns" This option is passed a hashref that maps uri's to prefixes. The prefixes in the document will be replaced by the ones in the map. The mapped prefixes can (actually have to) be used to trigger handlers, navigate or query the document. .Sp Here is an example: .Sp .Vb 10 \& my $t= XML::Twig\->new( map_xmlns => {\*(Aqhttp://www.w3.org/2000/svg\*(Aq => "svg"}, \& twig_handlers => \& { \*(Aqsvg:circle\*(Aq => sub { $_\->set_att( r => 20) } }, \& pretty_print => \*(Aqindented\*(Aq, \& ) \& \->parse( \*(Aq \& \& \*(Aq \& ) \& \->print; .Ve .Sp This will output: .Sp .Vb 3 \& \& \& .Ve .IP "keep_original_prefix" 4 .IX Item "keep_original_prefix" When used with \f(CW\*(C`map_xmlns\*(C'\fR this option will make \f(CW\*(C`XML::Twig\*(C'\fR use the original namespace prefixes when outputting a document. The mapped prefix will still be used for triggering handlers and in navigation and query methods. .Sp .Vb 11 \& my $t= XML::Twig\->new( map_xmlns => {\*(Aqhttp://www.w3.org/2000/svg\*(Aq => "svg"}, \& twig_handlers => \& { \*(Aqsvg:circle\*(Aq => sub { $_\->set_att( r => 20) } }, \& keep_original_prefix => 1, \& pretty_print => \*(Aqindented\*(Aq, \& ) \& \->parse( \*(Aq \& \& \*(Aq \& ) \& \->print; .Ve .Sp This will output: .Sp .Vb 3 \& \& \& .Ve .IP "original_uri ($prefix)" 4 .IX Item "original_uri ($prefix)" called within a handler, this will return the uri bound to the namespace prefix in the original document. .ie n .IP "index ($arrayref or $hashref)" 4 .el .IP "index ($arrayref or \f(CW$hashref\fR)" 4 .IX Item "index ($arrayref or $hashref)" This option creates lists of specific elements during the parsing of the \s-1XML.\s0 It takes a reference to either a list of triggering expressions or to a hash name => expression, and for each one generates the list of elements that match the expression. The list can be accessed through the \f(CW\*(C`index\*(C'\fR method. .Sp example: .Sp .Vb 6 \& # using an array ref \& my $t= XML::Twig\->new( index => [ \*(Aqdiv\*(Aq, \*(Aqtable\*(Aq ]) \& \->parsefile( "foo.xml"); \& my $divs= $t\->index( \*(Aqdiv\*(Aq); \& my $first_div= $divs\->[0]; \& my $last_table= $t\->index( table => \-1); \& \& # using a hashref to name the indexes \& my $t= XML::Twig\->new( index => { email => \*(Aqa[@href=~/^ \es*mailto:/]\*(Aq}) \& \->parsefile( "foo.xml"); \& my $last_emails= $t\->index( email => \-1); .Ve .Sp Note that the index is not maintained after the parsing. If elements are deleted, renamed or otherwise hurt during processing, the index is \s-1NOT\s0 updated. (changing the id element \s-1OTOH\s0 will update the index) .IP "att_accessors " 4 .IX Item "att_accessors " creates methods that give direct access to attribute: .Sp .Vb 4 \& my $t= XML::Twig\->new( att_accessors => [ \*(Aqhref\*(Aq, \*(Aqsrc\*(Aq]) \& \->parsefile( $file); \& my $first_href= $t\->first_elt( \*(Aqimg\*(Aq)\->src; # same as \->att( \*(Aqsrc\*(Aq) \& $t\->first_elt( \*(Aqimg\*(Aq)\->src( \*(Aqnew_logo.png\*(Aq) # changes the attribute value .Ve .IP "elt_accessors" 4 .IX Item "elt_accessors" creates methods that give direct access to the first child element (in scalar context) or the list of elements (in list context): .Sp the list of accessors to create can be given 1 2 different ways: in an array, or in a hash alias => expression my \f(CW$t\fR= XML::Twig\->new( elt_accessors => [ 'head']) \->parsefile( \f(CW$file\fR); my \f(CW$title_text\fR= \f(CW$t\fR\->root\->head\->field( 'title'); # same as \f(CW$title_text\fR= \f(CW$t\fR\->root\->first_child( 'head')\->field( 'title'); .Sp .Vb 5 \& my $t= XML::Twig\->new( elt_accessors => { warnings => \*(Aqp[@class="warning"]\*(Aq, d2 => \*(Aqdiv[2]\*(Aq}, ) \& \->parsefile( $file); \& my $body= $t\->first_elt( \*(Aqbody\*(Aq); \& my @warnings= $body\->warnings; # same as $body\->children( \*(Aqp[@class="warning"]\*(Aq); \& my $s2= $body\->d2; # same as $body\->first_child( \*(Aqdiv[2]\*(Aq) .Ve .IP "field_accessors" 4 .IX Item "field_accessors" creates methods that give direct access to the first child element text: .Sp .Vb 4 \& my $t= XML::Twig\->new( field_accessors => [ \*(Aqh1\*(Aq]) \& \->parsefile( $file); \& my $div_title_text= $t\->first_elt( \*(Aqdiv\*(Aq)\->title; \& # same as $title_text= $t\->first_elt( \*(Aqdiv\*(Aq)\->field( \*(Aqtitle\*(Aq); .Ve .IP "use_tidy" 4 .IX Item "use_tidy" set this option to use HTML::Tidy instead of HTML::TreeBuilder to convert \&\s-1HTML\s0 to \s-1XML. HTML,\s0 especially real (real \*(L"crap\*(R") \s-1HTML\s0 found in the wild, so depending on the data, one module or the other does a better job at the conversion. Also, HTML::Tidy can be a bit difficult to install, so XML::Twig offers both option. \s-1TIMTOWTDI\s0 .IP "output_html_doctype" 4 .IX Item "output_html_doctype" when using HTML::TreeBuilder to convert \s-1HTML,\s0 this option causes the \s-1DOCTYPE\s0 declaration to be output, which may be important for some legacy browsers. Without that option the \s-1DOCTYPE\s0 definition is \s-1NOT\s0 output. Also if the definition is completely wrong (ie not easily parsable), it is not output either. .RE .RS 4 .Sp \&\fBNote\fR: I _HATE_ the Java-like name of arguments used by most \s-1XML\s0 modules. So in pure \s-1TIMTOWTDI\s0 fashion all arguments can be written either as \&\f(CW\*(C`UglyJavaLikeName\*(C'\fR or as \f(CW\*(C`readable_perl_name\*(C'\fR: \f(CW\*(C`twig_print_outside_roots\*(C'\fR or \f(CW\*(C`TwigPrintOutsideRoots\*(C'\fR (or even \f(CW\*(C`twigPrintOutsideRoots\*(C'\fR {shudder}). XML::Twig normalizes them before processing them. .RE .ie n .IP "parse ( $source)" 4 .el .IP "parse ( \f(CW$source\fR)" 4 .IX Item "parse ( $source)" The \f(CW$source\fR parameter should either be a string containing the whole \s-1XML\s0 document, or it should be an open \f(CW\*(C`IO::Handle\*(C'\fR (aka a filehandle). .Sp A die call is thrown if a parse error occurs. Otherwise it will return the twig built by the parse. Use \f(CW\*(C`safe_parse\*(C'\fR if you want the parsing to return even when an error occurs. .Sp If this method is called as a class method (\f(CW\*(C`XML::Twig\->parse( $some_xml_or_html)\*(C'\fR) then an XML::Twig object is created, using the parameters except the last one (eg \&\f(CW\*(C`XML::Twig\->parse( pretty_print => \*(Aqindented\*(Aq, $some_xml_or_html)\*(C'\fR) and \f(CW\*(C`xparse\*(C'\fR is called on it. .Sp Note that when parsing a filehandle, the handle should \s-1NOT\s0 be open with an encoding (ie open with \f(CW\*(C`open( my $in, \*(Aq<\*(Aq, $filename)\*(C'\fR. The file will be parsed by \f(CW\*(C`expat\*(C'\fR, so specifying the encoding actually causes problems for the parser (as in: it can crash it, see https://rt.cpan.org/Ticket/Display.html?id=78877). For parsing a file it is actually recommended to use \f(CW\*(C`parsefile\*(C'\fR on the file name, instead of on the open file. .IP "parsestring" 4 .IX Item "parsestring" This is just an alias for \f(CW\*(C`parse\*(C'\fR for backwards compatibility. .IP "parsefile (\s-1FILE\s0 [, \s-1OPT\s0 => \s-1OPT_VALUE\s0 [...]])" 4 .IX Item "parsefile (FILE [, OPT => OPT_VALUE [...]])" Open \f(CW\*(C`FILE\*(C'\fR for reading, then call \f(CW\*(C`parse\*(C'\fR with the open handle. The file is closed no matter how \f(CW\*(C`parse\*(C'\fR returns. .Sp A \f(CW\*(C`die\*(C'\fR call is thrown if a parse error occurs. Otherwise it will return the twig built by the parse. Use \f(CW\*(C`safe_parsefile\*(C'\fR if you want the parsing to return even when an error occurs. .ie n .IP "parsefile_inplace ( $file, $optional_extension)" 4 .el .IP "parsefile_inplace ( \f(CW$file\fR, \f(CW$optional_extension\fR)" 4 .IX Item "parsefile_inplace ( $file, $optional_extension)" Parse and update a file \*(L"in place\*(R". It does this by creating a temp file, selecting it as the default for \fBprint()\fR statements (and methods), then parsing the input file. If the parsing is successful, then the temp file is moved to replace the input file. .Sp If an extension is given then the original file is backed-up (the rules for the extension are the same as the rule for the \-i option in perl). .ie n .IP "parsefile_html_inplace ( $file, $optional_extension)" 4 .el .IP "parsefile_html_inplace ( \f(CW$file\fR, \f(CW$optional_extension\fR)" 4 .IX Item "parsefile_html_inplace ( $file, $optional_extension)" Same as parsefile_inplace, except that it parses \s-1HTML\s0 instead of \s-1XML\s0 .ie n .IP "parseurl ($url $optional_user_agent)" 4 .el .IP "parseurl ($url \f(CW$optional_user_agent\fR)" 4 .IX Item "parseurl ($url $optional_user_agent)" Gets the data from \f(CW$url\fR and parse it. The data is piped to the parser in chunks the size of the XML::Parser::Expat buffer, so memory consumption and hopefully speed are optimal. .Sp Note that \f(CW\*(C`parseurl\*(C'\fR forks a child process that calls \f(CW\*(C`exit\*(C'\fR once the data has been retrieved, which can interfere with locks. If that's aproblem, see below: .Sp For most (read \*(L"small\*(R") \s-1XML\s0 it is probably as efficient (and easier to debug) to just \f(CW\*(C`get\*(C'\fR the \s-1XML\s0 file and then parse it as a string. .Sp .Vb 4 \& use XML::Twig; \& use LWP::Simple; \& my $twig= XML::Twig\->new(); \& $twig\->parse( LWP::Simple::get( $URL )); .Ve .Sp or more simply to call \f(CW\*(C`nparse\*(C'\fR .Sp .Vb 2 \& use XML::Twig; \& my $twig= XML::Twig\->nparse( $URL); .Ve .Sp If the \f(CW$optional_user_agent\fR argument is passed to the method then it is used, otherwise a new one is created. .IP "safe_parse ( \s-1SOURCE\s0 [, \s-1OPT\s0 => \s-1OPT_VALUE\s0 [...]])" 4 .IX Item "safe_parse ( SOURCE [, OPT => OPT_VALUE [...]])" This method is similar to \f(CW\*(C`parse\*(C'\fR except that it wraps the parsing in an \&\f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig). \f(CW$@\fR contains the error message on failure. .Sp Note that the parsing still stops as soon as an error is detected, there is no way to keep going after an error. .IP "safe_parsefile (\s-1FILE\s0 [, \s-1OPT\s0 => \s-1OPT_VALUE\s0 [...]])" 4 .IX Item "safe_parsefile (FILE [, OPT => OPT_VALUE [...]])" This method is similar to \f(CW\*(C`parsefile\*(C'\fR except that it wraps the parsing in an \&\f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig) . \f(CW$@\fR contains the error message on failure .Sp Note that the parsing still stops as soon as an error is detected, there is no way to keep going after an error. .ie n .IP "safe_parseurl ($url $optional_user_agent)" 4 .el .IP "safe_parseurl ($url \f(CW$optional_user_agent\fR)" 4 .IX Item "safe_parseurl ($url $optional_user_agent)" Same as \f(CW\*(C`parseurl\*(C'\fR except that it wraps the parsing in an \f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig) . \f(CW$@\fR contains the error message on failure .IP "parse_html ($string_or_fh)" 4 .IX Item "parse_html ($string_or_fh)" parse an \s-1HTML\s0 string or file handle (by converting it to \s-1XML\s0 using HTML::TreeBuilder, which needs to be available). .Sp This works nicely, but some information gets lost in the process: newlines are removed, and (at least on the version I use), comments get an extra \s-1CDATA\s0 section inside ( becomes \-\-> .IP "parsefile_html ($file)" 4 .IX Item "parsefile_html ($file)" parse an \s-1HTML\s0 file (by converting it to \s-1XML\s0 using HTML::TreeBuilder, which needs to be available, or HTML::Tidy if the \f(CW\*(C`use_tidy\*(C'\fR option was used). The file is loaded completely in memory and converted to \s-1XML\s0 before being parsed. .Sp this method is to be used with caution though, as it doesn't know about the file encoding, it is usually better to use \f(CW\*(C`parse_html\*(C'\fR, which gives you a chance to open the file with the proper encoding layer. .ie n .IP "parseurl_html ($url $optional_user_agent)" 4 .el .IP "parseurl_html ($url \f(CW$optional_user_agent\fR)" 4 .IX Item "parseurl_html ($url $optional_user_agent)" parse an \s-1URL\s0 as html the same way \f(CW\*(C`parse_html\*(C'\fR does .ie n .IP "safe_parseurl_html ($url $optional_user_agent)" 4 .el .IP "safe_parseurl_html ($url \f(CW$optional_user_agent\fR)" 4 .IX Item "safe_parseurl_html ($url $optional_user_agent)" Same as \f(CW\*(C`parseurl_html\*(C'\fR> except that it wraps the parsing in an \f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig) . \f(CW$@\fR contains the error message on failure .ie n .IP "safe_parsefile_html ($file $optional_user_agent)" 4 .el .IP "safe_parsefile_html ($file \f(CW$optional_user_agent\fR)" 4 .IX Item "safe_parsefile_html ($file $optional_user_agent)" Same as \f(CW\*(C`parsefile_html\*(C'\fR> except that it wraps the parsing in an \f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig) . \f(CW$@\fR contains the error message on failure .IP "safe_parse_html ($string_or_fh)" 4 .IX Item "safe_parse_html ($string_or_fh)" Same as \f(CW\*(C`parse_html\*(C'\fR except that it wraps the parsing in an \f(CW\*(C`eval\*(C'\fR block. It returns the twig on success and 0 on failure (the twig object also contains the parsed twig) . \f(CW$@\fR contains the error message on failure .IP "xparse ($thing_to_parse)" 4 .IX Item "xparse ($thing_to_parse)" parse the \f(CW$thing_to_parse\fR, whether it is a filehandle, a string, an \s-1HTML\s0 file, an \s-1HTML URL,\s0 an \s-1URL\s0 or a file. .Sp Note that this is mostly a convenience method for one-off scripts. For example files that end in '.htm' or '.html' are parsed first as \s-1XML,\s0 and if this fails as \s-1HTML.\s0 This is certainly not the most efficient way to do this in general. .ie n .IP "nparse ($optional_twig_options, $thing_to_parse)" 4 .el .IP "nparse ($optional_twig_options, \f(CW$thing_to_parse\fR)" 4 .IX Item "nparse ($optional_twig_options, $thing_to_parse)" create a twig with the \f(CW$optional_options\fR, and parse the \f(CW$thing_to_parse\fR, whether it is a filehandle, a string, an \s-1HTML\s0 file, an \s-1HTML URL,\s0 an \s-1URL\s0 or a file. .Sp Examples: .Sp .Vb 2 \& XML::Twig\->nparse( "file.xml"); \& XML::Twig\->nparse( error_context => 1, "file://file.xml"); .Ve .ie n .IP "nparse_pp ($optional_twig_options, $thing_to_parse)" 4 .el .IP "nparse_pp ($optional_twig_options, \f(CW$thing_to_parse\fR)" 4 .IX Item "nparse_pp ($optional_twig_options, $thing_to_parse)" same as \f(CW\*(C`nparse\*(C'\fR but also sets the \f(CW\*(C`pretty_print\*(C'\fR option to \f(CW\*(C`indented\*(C'\fR. .ie n .IP "nparse_e ($optional_twig_options, $thing_to_parse)" 4 .el .IP "nparse_e ($optional_twig_options, \f(CW$thing_to_parse\fR)" 4 .IX Item "nparse_e ($optional_twig_options, $thing_to_parse)" same as \f(CW\*(C`nparse\*(C'\fR but also sets the \f(CW\*(C`error_context\*(C'\fR option to 1. .ie n .IP "nparse_ppe ($optional_twig_options, $thing_to_parse)" 4 .el .IP "nparse_ppe ($optional_twig_options, \f(CW$thing_to_parse\fR)" 4 .IX Item "nparse_ppe ($optional_twig_options, $thing_to_parse)" same as \f(CW\*(C`nparse\*(C'\fR but also sets the \f(CW\*(C`pretty_print\*(C'\fR option to \f(CW\*(C`indented\*(C'\fR and the \f(CW\*(C`error_context\*(C'\fR option to 1. .IP "parser" 4 .IX Item "parser" This method returns the \f(CW\*(C`expat\*(C'\fR object (actually the XML::Parser::Expat object) used during parsing. It is useful for example to call XML::Parser::Expat methods on it. To get the line of a tag for example use \f(CW\*(C`$t\->parser\->current_line\*(C'\fR. .IP "setTwigHandlers ($handlers)" 4 .IX Item "setTwigHandlers ($handlers)" Set the twig_handlers. \f(CW$handlers\fR is a reference to a hash similar to the one in the \f(CW\*(C`twig_handlers\*(C'\fR option of new. All previous handlers are unset. The method returns the reference to the previous handlers. .ie n .IP "setTwigHandler ($exp $handler)" 4 .el .IP "setTwigHandler ($exp \f(CW$handler\fR)" 4 .IX Item "setTwigHandler ($exp $handler)" Set a single twig_handler for elements matching \f(CW$exp\fR. \f(CW$handler\fR is a reference to a subroutine. If the handler was previously set then the reference to the previous handler is returned. .IP "setStartTagHandlers ($handlers)" 4 .IX Item "setStartTagHandlers ($handlers)" Set the start_tag handlers. \f(CW$handlers\fR is a reference to a hash similar to the one in the \f(CW\*(C`start_tag_handlers\*(C'\fR option of new. All previous handlers are unset. The method returns the reference to the previous handlers. .ie n .IP "setStartTagHandler ($exp $handler)" 4 .el .IP "setStartTagHandler ($exp \f(CW$handler\fR)" 4 .IX Item "setStartTagHandler ($exp $handler)" Set a single start_tag handlers for elements matching \f(CW$exp\fR. \f(CW$handler\fR is a reference to a subroutine. If the handler was previously set then the reference to the previous handler is returned. .IP "setEndTagHandlers ($handlers)" 4 .IX Item "setEndTagHandlers ($handlers)" Set the end_tag handlers. \f(CW$handlers\fR is a reference to a hash similar to the one in the \f(CW\*(C`end_tag_handlers\*(C'\fR option of new. All previous handlers are unset. The method returns the reference to the previous handlers. .ie n .IP "setEndTagHandler ($exp $handler)" 4 .el .IP "setEndTagHandler ($exp \f(CW$handler\fR)" 4 .IX Item "setEndTagHandler ($exp $handler)" Set a single end_tag handlers for elements matching \f(CW$exp\fR. \f(CW$handler\fR is a reference to a subroutine. If the handler was previously set then the reference to the previous handler is returned. .IP "setTwigRoots ($handlers)" 4 .IX Item "setTwigRoots ($handlers)" Same as using the \f(CW\*(C`twig_roots\*(C'\fR option when creating the twig .ie n .IP "setCharHandler ($exp $handler)" 4 .el .IP "setCharHandler ($exp \f(CW$handler\fR)" 4 .IX Item "setCharHandler ($exp $handler)" Set a \f(CW\*(C`char_handler\*(C'\fR .IP "setIgnoreEltsHandler ($exp)" 4 .IX Item "setIgnoreEltsHandler ($exp)" Set a \f(CW\*(C`ignore_elt\*(C'\fR handler (elements that match \f(CW$exp\fR will be ignored .IP "setIgnoreEltsHandlers ($exp)" 4 .IX Item "setIgnoreEltsHandlers ($exp)" Set all \f(CW\*(C`ignore_elt\*(C'\fR handlers (previous handlers are replaced) .IP "dtd" 4 .IX Item "dtd" Return the dtd (an XML::Twig::DTD object) of a twig .IP "xmldecl" 4 .IX Item "xmldecl" Return the \s-1XML\s0 declaration for the document, or a default one if it doesn't have one .IP "doctype" 4 .IX Item "doctype" Return the doctype for the document .IP "doctype_name" 4 .IX Item "doctype_name" returns the doctype of the document from the doctype declaration .IP "system_id" 4 .IX Item "system_id" returns the system value of the \s-1DTD\s0 of the document from the doctype declaration .IP "public_id" 4 .IX Item "public_id" returns the public doctype of the document from the doctype declaration .IP "internal_subset" 4 .IX Item "internal_subset" returns the internal subset of the \s-1DTD\s0 .IP "dtd_text" 4 .IX Item "dtd_text" Return the \s-1DTD\s0 text .IP "dtd_print" 4 .IX Item "dtd_print" Print the \s-1DTD\s0 .IP "model ($tag)" 4 .IX Item "model ($tag)" Return the model (in the \s-1DTD\s0) for the element \f(CW$tag\fR .IP "root" 4 .IX Item "root" Return the root element of a twig .IP "set_root ($elt)" 4 .IX Item "set_root ($elt)" Set the root of a twig .IP "first_elt ($optional_condition)" 4 .IX Item "first_elt ($optional_condition)" Return the first element matching \f(CW$optional_condition\fR of a twig, if no condition is given then the root is returned .IP "last_elt ($optional_condition)" 4 .IX Item "last_elt ($optional_condition)" Return the last element matching \f(CW$optional_condition\fR of a twig, if no condition is given then the last element of the twig is returned .IP "elt_id ($id)" 4 .IX Item "elt_id ($id)" Return the element whose \f(CW\*(C`id\*(C'\fR attribute is \f(CW$id\fR .IP "getEltById" 4 .IX Item "getEltById" Same as \f(CW\*(C`elt_id\*(C'\fR .ie n .IP "index ($index_name, $optional_index)" 4 .el .IP "index ($index_name, \f(CW$optional_index\fR)" 4 .IX Item "index ($index_name, $optional_index)" If the \f(CW$optional_index\fR argument is present, return the corresponding element in the index (created using the \f(CW\*(C`index\*(C'\fR option for \f(CW\*(C`XML::Twig\-\*(C'\fRnew>) .Sp If the argument is not present, return an arrayref to the index .IP "normalize" 4 .IX Item "normalize" merge together all consecutive pcdata elements in the document (if for example you have turned some elements into pcdata using \f(CW\*(C`erase\*(C'\fR, this will give you a \*(L"clean\*(R" document in which there all text elements are as long as possible). .IP "encoding" 4 .IX Item "encoding" This method returns the encoding of the \s-1XML\s0 document, as defined by the \&\f(CW\*(C`encoding\*(C'\fR attribute in the \s-1XML\s0 declaration (ie it is \f(CW\*(C`undef\*(C'\fR if the attribute is not defined) .IP "set_encoding" 4 .IX Item "set_encoding" This method sets the value of the \f(CW\*(C`encoding\*(C'\fR attribute in the \s-1XML\s0 declaration. Note that if the document did not have a declaration it is generated (with an \s-1XML\s0 version of 1.0) .IP "xml_version" 4 .IX Item "xml_version" This method returns the \s-1XML\s0 version, as defined by the \f(CW\*(C`version\*(C'\fR attribute in the \s-1XML\s0 declaration (ie it is \f(CW\*(C`undef\*(C'\fR if the attribute is not defined) .IP "set_xml_version" 4 .IX Item "set_xml_version" This method sets the value of the \f(CW\*(C`version\*(C'\fR attribute in the \s-1XML\s0 declaration. If the declaration did not exist it is created. .IP "standalone" 4 .IX Item "standalone" This method returns the value of the \f(CW\*(C`standalone\*(C'\fR declaration for the document .IP "set_standalone" 4 .IX Item "set_standalone" This method sets the value of the \f(CW\*(C`standalone\*(C'\fR attribute in the \s-1XML\s0 declaration. Note that if the document did not have a declaration it is generated (with an \s-1XML\s0 version of 1.0) .IP "set_output_encoding" 4 .IX Item "set_output_encoding" Set the \f(CW\*(C`encoding\*(C'\fR \*(L"attribute\*(R" in the \s-1XML\s0 declaration .ie n .IP "set_doctype ($name, $system, $public, $internal)" 4 .el .IP "set_doctype ($name, \f(CW$system\fR, \f(CW$public\fR, \f(CW$internal\fR)" 4 .IX Item "set_doctype ($name, $system, $public, $internal)" Set the doctype of the element. If an argument is \f(CW\*(C`undef\*(C'\fR (or not present) then its former value is retained, if a false ('' or 0) value is passed then the former value is deleted; .IP "entity_list" 4 .IX Item "entity_list" Return the entity list of a twig .IP "entity_names" 4 .IX Item "entity_names" Return the list of all defined entities .IP "entity ($entity_name)" 4 .IX Item "entity ($entity_name)" Return the entity .ie n .IP "change_gi ($old_gi, $new_gi)" 4 .el .IP "change_gi ($old_gi, \f(CW$new_gi\fR)" 4 .IX Item "change_gi ($old_gi, $new_gi)" Performs a (very fast) global change. All elements \f(CW$old_gi\fR are now \&\f(CW$new_gi\fR. This is a bit dangerous though and should be avoided if < possible, as the new tag might be ignored in subsequent processing. .Sp See \f(CW\*(C`BUGS \*(C'\fR .ie n .IP "flush ($optional_filehandle, %options)" 4 .el .IP "flush ($optional_filehandle, \f(CW%options\fR)" 4 .IX Item "flush ($optional_filehandle, %options)" Flushes a twig up to (and including) the current element, then deletes all unnecessary elements from the tree that's kept in memory. \&\f(CW\*(C`flush\*(C'\fR keeps track of which elements need to be open/closed, so if you flush from handlers you don't have to worry about anything. Just keep flushing the twig every time you're done with a sub-tree and it will come out well-formed. After the whole parsing don't forget to\f(CW\*(C`flush\*(C'\fR one more time to print the end of the document. The doctype and entity declarations are also printed. .Sp flush take an optional filehandle as an argument. .Sp If you use \f(CW\*(C`flush\*(C'\fR at any point during parsing, the document will be flushed one last time at the end of the parsing, to the proper filehandle. .Sp options: use the \f(CW\*(C`update_DTD\*(C'\fR option if you have updated the (internal) \s-1DTD\s0 and/or the entity list and you want the updated \s-1DTD\s0 to be output .Sp The \f(CW\*(C`pretty_print\*(C'\fR option sets the pretty printing of the document. .Sp .Vb 3 \& Example: $t\->flush( Update_DTD => 1); \& $t\->flush( $filehandle, pretty_print => \*(Aqindented\*(Aq); \& $t\->flush( \e*FILE); .Ve .ie n .IP "flush_up_to ($elt, $optional_filehandle, %options)" 4 .el .IP "flush_up_to ($elt, \f(CW$optional_filehandle\fR, \f(CW%options\fR)" 4 .IX Item "flush_up_to ($elt, $optional_filehandle, %options)" Flushes up to the \f(CW$elt\fR element. This allows you to keep part of the tree in memory when you \f(CW\*(C`flush\*(C'\fR. .Sp options: see flush. .IP "purge" 4 .IX Item "purge" Does the same as a \f(CW\*(C`flush\*(C'\fR except it does not print the twig. It just deletes all elements that have been completely parsed so far. .IP "purge_up_to ($elt)" 4 .IX Item "purge_up_to ($elt)" Purges up to the \f(CW$elt\fR element. This allows you to keep part of the tree in memory when you \f(CW\*(C`purge\*(C'\fR. .ie n .IP "print ($optional_filehandle, %options)" 4 .el .IP "print ($optional_filehandle, \f(CW%options\fR)" 4 .IX Item "print ($optional_filehandle, %options)" Prints the whole document associated with the twig. To be used only \s-1AFTER\s0 the parse. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .ie n .IP "print_to_file ($filename, %options)" 4 .el .IP "print_to_file ($filename, \f(CW%options\fR)" 4 .IX Item "print_to_file ($filename, %options)" Prints the whole document associated with the twig to file \f(CW$filename\fR. To be used only \s-1AFTER\s0 the parse. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .ie n .IP "safe_print_to_file ($filename, %options)" 4 .el .IP "safe_print_to_file ($filename, \f(CW%options\fR)" 4 .IX Item "safe_print_to_file ($filename, %options)" Prints the whole document associated with the twig to file \f(CW$filename\fR. This variant, which probably only works on *nix prints to a temp file, then move the temp file to overwrite the original file. .Sp This is a bit safer when 2 processes an potentiallywrite the same file: only the last one will succeed, but the file won't be corruted. I often use this for cron jobs, so testing the code doesn't interfere with the cron job running at the same time. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .IP "sprint" 4 .IX Item "sprint" Return the text of the whole document associated with the twig. To be used only \&\s-1AFTER\s0 the parse. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .IP "trim" 4 .IX Item "trim" Trim the document: gets rid of initial and trailing spaces, and replaces multiple spaces by a single one. .IP "toSAX1 ($handler)" 4 .IX Item "toSAX1 ($handler)" Send \s-1SAX\s0 events for the twig to the \s-1SAX1\s0 handler \f(CW$handler\fR .IP "toSAX2 ($handler)" 4 .IX Item "toSAX2 ($handler)" Send \s-1SAX\s0 events for the twig to the \s-1SAX2\s0 handler \f(CW$handler\fR .IP "flush_toSAX1 ($handler)" 4 .IX Item "flush_toSAX1 ($handler)" Same as flush, except that \s-1SAX\s0 events are sent to the \s-1SAX1\s0 handler \&\f(CW$handler\fR instead of the twig being printed .IP "flush_toSAX2 ($handler)" 4 .IX Item "flush_toSAX2 ($handler)" Same as flush, except that \s-1SAX\s0 events are sent to the \s-1SAX2\s0 handler \&\f(CW$handler\fR instead of the twig being printed .IP "ignore" 4 .IX Item "ignore" This method should be called during parsing, usually in \f(CW\*(C`start_tag_handlers\*(C'\fR. It causes the element to be skipped during the parsing: the twig is not built for this element, it will not be accessible during parsing or after it. The element will not take up any memory and parsing will be faster. .Sp Note that this method can also be called on an element. If the element is a parent of the current element then this element will be ignored (the twig will not be built any more for it and what has already been built will be deleted). .IP "set_pretty_print ($style)" 4 .IX Item "set_pretty_print ($style)" Set the pretty print method, amongst '\f(CW\*(C`none\*(C'\fR' (default), '\f(CW\*(C`nsgmls\*(C'\fR', \&'\f(CW\*(C`nice\*(C'\fR', '\f(CW\*(C`indented\*(C'\fR', \f(CW\*(C`indented_c\*(C'\fR, '\f(CW\*(C`wrapped\*(C'\fR', '\f(CW\*(C`record\*(C'\fR' and \&'\f(CW\*(C`record_c\*(C'\fR' .Sp \&\fB\s-1WARNING:\s0\fR the pretty print style is a \fB\s-1GLOBAL\s0\fR variable, so once set it's applied to \fB\s-1ALL\s0\fR \f(CW\*(C`print\*(C'\fR's (and \f(CW\*(C`sprint\*(C'\fR's). Same goes if you use XML::Twig with \f(CW\*(C`mod_perl\*(C'\fR . This should not be a problem as the \s-1XML\s0 that's generated is valid anyway, and \s-1XML\s0 processors (as well as \s-1HTML\s0 processors, including browsers) should not care. Let me know if this is a big problem, but at the moment the performance/cleanliness trade-off clearly favors the global approach. .IP "set_empty_tag_style ($style)" 4 .IX Item "set_empty_tag_style ($style)" Set the empty tag display style ('\f(CW\*(C`normal\*(C'\fR', '\f(CW\*(C`html\*(C'\fR' or '\f(CW\*(C`expand\*(C'\fR'). As with \f(CW\*(C`set_pretty_print\*(C'\fR this sets a global flag. .Sp \&\f(CW\*(C`normal\*(C'\fR outputs an empty tag '\f(CW\*(C`\*(C'\fR', \f(CW\*(C`html\*(C'\fR adds a space \&'\f(CW\*(C`\*(C'\fR' for elements that can be empty in \s-1XHTML\s0 and \f(CW\*(C`expand\*(C'\fR outputs \&'\f(CW\*(C`\*(C'\fR' .IP "set_remove_cdata ($flag)" 4 .IX Item "set_remove_cdata ($flag)" set (or unset) the flag that forces the twig to output \s-1CDATA\s0 sections as regular (escaped) \s-1PCDATA\s0 .ie n .IP "print_prolog ($optional_filehandle, %options)" 4 .el .IP "print_prolog ($optional_filehandle, \f(CW%options\fR)" 4 .IX Item "print_prolog ($optional_filehandle, %options)" Prints the prolog (\s-1XML\s0 declaration + \s-1DTD +\s0 entity declarations) of a document. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .ie n .IP "prolog ($optional_filehandle, %options)" 4 .el .IP "prolog ($optional_filehandle, \f(CW%options\fR)" 4 .IX Item "prolog ($optional_filehandle, %options)" Return the prolog (\s-1XML\s0 declaration + \s-1DTD +\s0 entity declarations) of a document. .Sp options: see \f(CW\*(C`flush\*(C'\fR. .IP "finish" 4 .IX Item "finish" Call Expat \f(CW\*(C`finish\*(C'\fR method. Unsets all handlers (including internal ones that set context), but expat continues parsing to the end of the document or until it finds an error. It should finish up a lot faster than with the handlers set. .IP "finish_print" 4 .IX Item "finish_print" Stops twig processing, flush the twig and proceed to finish printing the document as fast as possible. Use this method when modifying a document and the modification is done. .IP "finish_now" 4 .IX Item "finish_now" Stops twig processing, does not finish parsing the document (which could actually be not well-formed after the point where \f(CW\*(C`finish_now\*(C'\fR is called). Execution resumes after the \f(CW\*(C`Lparse\*(C'\fR> or \f(CW\*(C`parsefile\*(C'\fR call. The content of the twig is what has been parsed so far (all open elements at the time \&\f(CW\*(C`finish_now\*(C'\fR is called are considered closed). .IP "set_expand_external_entities" 4 .IX Item "set_expand_external_entities" Same as using the \f(CW\*(C`expand_external_ents\*(C'\fR option when creating the twig .IP "set_input_filter" 4 .IX Item "set_input_filter" Same as using the \f(CW\*(C`input_filter\*(C'\fR option when creating the twig .IP "set_keep_atts_order" 4 .IX Item "set_keep_atts_order" Same as using the \f(CW\*(C`keep_atts_order\*(C'\fR option when creating the twig .IP "set_keep_encoding" 4 .IX Item "set_keep_encoding" Same as using the \f(CW\*(C`keep_encoding\*(C'\fR option when creating the twig .IP "escape_gt" 4 .IX Item "escape_gt" usually XML::Twig does not escape > in its output. Using this option makes it replace > by > .IP "do_not_escape_gt" 4 .IX Item "do_not_escape_gt" reverts XML::Twig behavior to its default of not escaping > in its output. .IP "set_output_filter" 4 .IX Item "set_output_filter" Same as using the \f(CW\*(C`output_filter\*(C'\fR option when creating the twig .IP "set_output_text_filter" 4 .IX Item "set_output_text_filter" Same as using the \f(CW\*(C`output_text_filter\*(C'\fR option when creating the twig .ie n .IP "add_stylesheet ($type, @options)" 4 .el .IP "add_stylesheet ($type, \f(CW@options\fR)" 4 .IX Item "add_stylesheet ($type, @options)" Adds an external stylesheet to an \s-1XML\s0 document. .Sp Supported types and options: .RS 4 .IP "xsl" 4 .IX Item "xsl" option: the url of the stylesheet .Sp Example: .Sp .Vb 1 \& $t\->add_stylesheet( xsl => "xsl_style.xsl"); .Ve .Sp will generate the following \s-1PI\s0 at the beginning of the document: .Sp .Vb 1 \& .Ve .IP "css" 4 .IX Item "css" option: the url of the stylesheet .IP "active_twig" 4 .IX Item "active_twig" a class method that returns the last processed twig, so you don't necessarily need the object to call methods on it. .RE .RS 4 .RE .IP "Methods inherited from XML::Parser::Expat" 4 .IX Item "Methods inherited from XML::Parser::Expat" A twig inherits all the relevant methods from XML::Parser::Expat. These methods can only be used during the parsing phase (they will generate a fatal error otherwise). .Sp Inherited methods are: .RS 4 .IP "depth" 4 .IX Item "depth" Returns the size of the context list. .IP "in_element" 4 .IX Item "in_element" Returns true if \s-1NAME\s0 is equal to the name of the innermost currently opened element. If namespace processing is being used and you want to check against a name that may be in a namespace, then use the generate_ns_name method to create the \s-1NAME\s0 argument. .IP "within_element" 4 .IX Item "within_element" Returns the number of times the given name appears in the context list. If namespace processing is being used and you want to check against a name that may be in a namespace, then use the generate_ns_name method to create the \s-1NAME\s0 argument. .IP "context" 4 .IX Item "context" Returns a list of element names that represent open elements, with the last one being the innermost. Inside start and end tag handlers, this will be the tag of the parent element. .IP "current_line" 4 .IX Item "current_line" Returns the line number of the current position of the parse. .IP "current_column" 4 .IX Item "current_column" Returns the column number of the current position of the parse. .IP "current_byte" 4 .IX Item "current_byte" Returns the current position of the parse. .IP "position_in_context" 4 .IX Item "position_in_context" Returns a string that shows the current parse position. \s-1LINES\s0 should be an integer >= 0 that represents the number of lines on either side of the current parse line to place into the returned string. .IP "base ([\s-1NEWBASE\s0])" 4 .IX Item "base ([NEWBASE])" Returns the current value of the base for resolving relative URIs. If \s-1NEWBASE\s0 is supplied, changes the base to that value. .IP "current_element" 4 .IX Item "current_element" Returns the name of the innermost currently opened element. Inside start or end handlers, returns the parent of the element associated with those tags. .IP "element_index" 4 .IX Item "element_index" Returns an integer that is the depth-first visit order of the current element. This will be zero outside of the root element. For example, this will return 1 when called from the start handler for the root element start tag. .IP "recognized_string" 4 .IX Item "recognized_string" Returns the string from the document that was recognized in order to call the current handler. For instance, when called from a start handler, it will give us the start-tag string. The string is encoded in \s-1UTF\-8.\s0 This method doesn't return a meaningful string inside declaration handlers. .IP "original_string" 4 .IX Item "original_string" Returns the verbatim string from the document that was recognized in order to call the current handler. The string is in the original document encoding. This method doesn't return a meaningful string inside declaration handlers. .IP "xpcroak" 4 .IX Item "xpcroak" Concatenate onto the given message the current line number within the \s-1XML\s0 document plus the message implied by ErrorContext. Then croak with the formed message. .IP "xpcarp" 4 .IX Item "xpcarp" Concatenate onto the given message the current line number within the \s-1XML\s0 document plus the message implied by ErrorContext. Then carp with the formed message. .IP "xml_escape(\s-1TEXT\s0 [, \s-1CHAR\s0 [, \s-1CHAR ...\s0]])" 4 .IX Item "xml_escape(TEXT [, CHAR [, CHAR ...]])" Returns \s-1TEXT\s0 with markup characters turned into character entities. Any additional characters provided as arguments are also turned into character references where found in \s-1TEXT.\s0 .Sp (this method is broken on some versions of expat/XML::Parser) .RE .RS 4 .RE .ie n .IP "path ( $optional_tag)" 4 .el .IP "path ( \f(CW$optional_tag\fR)" 4 .IX Item "path ( $optional_tag)" Return the element context in a form similar to XPath's short form: '\f(CW\*(C`/root/tag1/../tag\*(C'\fR' .ie n .IP "get_xpath ( $optional_array_ref, $xpath, $optional_offset)" 4 .el .IP "get_xpath ( \f(CW$optional_array_ref\fR, \f(CW$xpath\fR, \f(CW$optional_offset\fR)" 4 .IX Item "get_xpath ( $optional_array_ref, $xpath, $optional_offset)" Performs a \f(CW\*(C`get_xpath\*(C'\fR on the document root (see ) .Sp If the \f(CW$optional_array_ref\fR argument is used the array must contain elements. The \f(CW$xpath\fR expression is applied to each element in turn and the result is union of all results. This way a first query can be refined in further steps. .ie n .IP "find_nodes ( $optional_array_ref, $xpath, $optional_offset)" 4 .el .IP "find_nodes ( \f(CW$optional_array_ref\fR, \f(CW$xpath\fR, \f(CW$optional_offset\fR)" 4 .IX Item "find_nodes ( $optional_array_ref, $xpath, $optional_offset)" same as \f(CW\*(C`get_xpath\*(C'\fR .ie n .IP "findnodes ( $optional_array_ref, $xpath, $optional_offset)" 4 .el .IP "findnodes ( \f(CW$optional_array_ref\fR, \f(CW$xpath\fR, \f(CW$optional_offset\fR)" 4 .IX Item "findnodes ( $optional_array_ref, $xpath, $optional_offset)" same as \f(CW\*(C`get_xpath\*(C'\fR (similar to the XML::LibXML method) .ie n .IP "findvalue ( $optional_array_ref, $xpath, $optional_offset)" 4 .el .IP "findvalue ( \f(CW$optional_array_ref\fR, \f(CW$xpath\fR, \f(CW$optional_offset\fR)" 4 .IX Item "findvalue ( $optional_array_ref, $xpath, $optional_offset)" Return the \f(CW\*(C`join\*(C'\fR of all texts of the results of applying \f(CW\*(C`get_xpath\*(C'\fR to the node (similar to the XML::LibXML method) .ie n .IP "findvalues ( $optional_array_ref, $xpath, $optional_offset)" 4 .el .IP "findvalues ( \f(CW$optional_array_ref\fR, \f(CW$xpath\fR, \f(CW$optional_offset\fR)" 4 .IX Item "findvalues ( $optional_array_ref, $xpath, $optional_offset)" Return an array of all texts of the results of applying \f(CW\*(C`get_xpath\*(C'\fR to the node .ie n .IP "subs_text ($regexp, $replace)" 4 .el .IP "subs_text ($regexp, \f(CW$replace\fR)" 4 .IX Item "subs_text ($regexp, $replace)" subs_text does text substitution on the whole document, similar to perl's \&\f(CW\*(C` s///\*(C'\fR operator. .IP "dispose" 4 .IX Item "dispose" Useful only if you don't have \f(CW\*(C`Scalar::Util\*(C'\fR or \f(CW\*(C`WeakRef\*(C'\fR installed. .Sp Reclaims properly the memory used by an XML::Twig object. As the object has circular references it never goes out of scope, so if you want to parse lots of \s-1XML\s0 documents then the memory leak becomes a problem. Use \&\f(CW\*(C`$twig\->dispose\*(C'\fR to clear this problem. .IP "att_accessors (list_of_attribute_names)" 4 .IX Item "att_accessors (list_of_attribute_names)" A convenience method that creates l\-valued accessors for attributes. So \f(CW\*(C`$twig\->create_accessors( \*(Aqfoo\*(Aq)\*(C'\fR will create a \f(CW\*(C`foo\*(C'\fR method that can be called on elements: .Sp .Vb 2 \& $elt\->foo; # equivalent to $elt\->{\*(Aqatt\*(Aq}\->{\*(Aqfoo\*(Aq}; \& $elt\->foo( \*(Aqbar\*(Aq); # equivalent to $elt\->set_att( foo => \*(Aqbar\*(Aq); .Ve .Sp The methods are l\-valued only under those perl's that support this feature (5.6 and above) .IP "create_accessors (list_of_attribute_names)" 4 .IX Item "create_accessors (list_of_attribute_names)" Same as att_accessors .IP "elt_accessors (list_of_attribute_names)" 4 .IX Item "elt_accessors (list_of_attribute_names)" A convenience method that creates accessors for elements. So \f(CW\*(C`$twig\->create_accessors( \*(Aqfoo\*(Aq)\*(C'\fR will create a \f(CW\*(C`foo\*(C'\fR method that can be called on elements: .Sp .Vb 1 \& $elt\->foo; # equivalent to $elt\->first_child( \*(Aqfoo\*(Aq); .Ve .IP "field_accessors (list_of_attribute_names)" 4 .IX Item "field_accessors (list_of_attribute_names)" A convenience method that creates accessors for element values (\f(CW\*(C`field\*(C'\fR). So \f(CW\*(C`$twig\->create_accessors( \*(Aqfoo\*(Aq)\*(C'\fR will create a \f(CW\*(C`foo\*(C'\fR method that can be called on elements: .Sp .Vb 1 \& $elt\->foo; # equivalent to $elt\->field( \*(Aqfoo\*(Aq); .Ve .IP "set_do_not_escape_amp_in_atts" 4 .IX Item "set_do_not_escape_amp_in_atts" An evil method, that I only document because Test::Pod::Coverage complaints otherwise, but really, you don't want to know about it. .SS "XML::Twig::Elt" .IX Subsection "XML::Twig::Elt" .ie n .IP "new ($optional_tag, $optional_atts, @optional_content)" 4 .el .IP "new ($optional_tag, \f(CW$optional_atts\fR, \f(CW@optional_content\fR)" 4 .IX Item "new ($optional_tag, $optional_atts, @optional_content)" The \f(CW\*(C`tag\*(C'\fR is optional (but then you can't have a content ), the \f(CW$optional_atts\fR argument is a reference to a hash of attributes, the content can be just a string or a list of strings and element. A content of '\f(CW\*(C`#EMPTY\*(C'\fR' creates an empty element; .Sp .Vb 7 \& Examples: my $elt= XML::Twig::Elt\->new(); \& my $elt= XML::Twig::Elt\->new( para => { align => \*(Aqcenter\*(Aq }); \& my $elt= XML::Twig::Elt\->new( para => { align => \*(Aqcenter\*(Aq }, \*(Aqfoo\*(Aq); \& my $elt= XML::Twig::Elt\->new( br => \*(Aq#EMPTY\*(Aq); \& my $elt= XML::Twig::Elt\->new( \*(Aqpara\*(Aq); \& my $elt= XML::Twig::Elt\->new( para => \*(Aqthis is a para\*(Aq); \& my $elt= XML::Twig::Elt\->new( para => $elt3, \*(Aqanother para\*(Aq); .Ve .Sp The strings are not parsed, the element is not attached to any twig. .Sp \&\fB\s-1WARNING\s0\fR: if you rely on \s-1ID\s0's then you will have to set the id yourself. At this point the element does not belong to a twig yet, so the \s-1ID\s0 attribute is not known so it won't be stored in the \s-1ID\s0 list. .Sp Note that \f(CW\*(C`#COMMENT\*(C'\fR, \f(CW\*(C`#PCDATA\*(C'\fR or \f(CW\*(C`#CDATA\*(C'\fR are valid tag names, that will create text elements. .Sp To create an element \f(CW\*(C`foo\*(C'\fR containing a \s-1CDATA\s0 section: .Sp .Vb 2 \& my $foo= XML::Twig::Elt\->new( \*(Aq#CDATA\*(Aq => "content of the CDATA section") \& \->wrap_in( \*(Aqfoo\*(Aq); .Ve .Sp An attribute of '#CDATA', will create the content of the element as \s-1CDATA:\s0 .Sp .Vb 1 \& my $elt= XML::Twig::Elt\->new( \*(Aqp\*(Aq => { \*(Aq#CDATA\*(Aq => 1}, \*(Aqfoo < bar\*(Aq); .Ve .Sp creates an element .Sp .Vb 1 \&

.Ve .ie n .IP "parse ($string, %args)" 4 .el .IP "parse ($string, \f(CW%args\fR)" 4 .IX Item "parse ($string, %args)" Creates an element from an \s-1XML\s0 string. The string is actually parsed as a new twig, then the root of that twig is returned. The arguments in \f(CW%args\fR are passed to the twig. As always if the parse fails the parser will die, so use an eval if you want to trap syntax errors. .Sp As obviously the element does not exist beforehand this method has to be called on the class: .Sp .Vb 3 \& my $elt= parse XML::Twig::Elt( " string to parse, with \& , actually tons of \& h"); .Ve .IP "set_inner_xml ($string)" 4 .IX Item "set_inner_xml ($string)" Sets the content of the element to be the tree created from the string .IP "set_inner_html ($string)" 4 .IX Item "set_inner_html ($string)" Sets the content of the element, after parsing the string with an \s-1HTML\s0 parser (HTML::Parser) .IP "set_outer_xml ($string)" 4 .IX Item "set_outer_xml ($string)" Replaces the element with the tree created from the string .ie n .IP "print ($optional_filehandle, $optional_pretty_print_style)" 4 .el .IP "print ($optional_filehandle, \f(CW$optional_pretty_print_style\fR)" 4 .IX Item "print ($optional_filehandle, $optional_pretty_print_style)" Prints an entire element, including the tags, optionally to a \&\f(CW$optional_filehandle\fR, optionally with a \f(CW$pretty_print_style\fR. .Sp The print outputs \s-1XML\s0 data so base entities are escaped. .ie n .IP "print_to_file ($filename, %options)" 4 .el .IP "print_to_file ($filename, \f(CW%options\fR)" 4 .IX Item "print_to_file ($filename, %options)" Prints the element to file \f(CW$filename\fR. .Sp options: see \f(CW\*(C`flush\*(C'\fR. =item sprint ($elt, \f(CW$optional_no_enclosing_tag\fR) .Sp Return the xml string for an entire element, including the tags. If the optional second argument is true then only the string inside the element is returned (the start and end tag for \f(CW$elt\fR are not). The text is XML-escaped: base entities (& and < in text, & < and " in attribute values) are turned into entities. .IP "gi" 4 .IX Item "gi" Return the gi of the element (the gi is the \f(CW\*(C`generic identifier\*(C'\fR the tag name in \s-1SGML\s0 parlance). .Sp \&\f(CW\*(C`tag\*(C'\fR and \f(CW\*(C`name\*(C'\fR are synonyms of \f(CW\*(C`gi\*(C'\fR. .IP "tag" 4 .IX Item "tag" Same as \f(CW\*(C`gi\*(C'\fR .IP "name" 4 .IX Item "name" Same as \f(CW\*(C`tag\*(C'\fR .IP "set_gi ($tag)" 4 .IX Item "set_gi ($tag)" Set the gi (tag) of an element .IP "set_tag ($tag)" 4 .IX Item "set_tag ($tag)" Set the tag (=\f(CW\*(C`tag\*(C'\fR) of an element .IP "set_name ($name)" 4 .IX Item "set_name ($name)" Set the name (=\f(CW\*(C`tag\*(C'\fR) of an element .IP "root" 4 .IX Item "root" Return the root of the twig in which the element is contained. .IP "twig" 4 .IX Item "twig" Return the twig containing the element. .IP "parent ($optional_condition)" 4 .IX Item "parent ($optional_condition)" Return the parent of the element, or the first ancestor matching the \&\f(CW$optional_condition\fR .IP "first_child ($optional_condition)" 4 .IX Item "first_child ($optional_condition)" Return the first child of the element, or the first child matching the \&\f(CW$optional_condition\fR .IP "has_child ($optional_condition)" 4 .IX Item "has_child ($optional_condition)" Return the first child of the element, or the first child matching the \&\f(CW$optional_condition\fR (same as first_child) .IP "has_children ($optional_condition)" 4 .IX Item "has_children ($optional_condition)" Return the first child of the element, or the first child matching the \&\f(CW$optional_condition\fR (same as first_child) .IP "first_child_text ($optional_condition)" 4 .IX Item "first_child_text ($optional_condition)" Return the text of the first child of the element, or the first child matching the \f(CW$optional_condition\fR If there is no first_child then returns ''. This avoids getting the child, checking for its existence then getting the text for trivial cases. .Sp Similar methods are available for the other navigation methods: .RS 4 .IP "last_child_text" 4 .IX Item "last_child_text" .PD 0 .IP "prev_sibling_text" 4 .IX Item "prev_sibling_text" .IP "next_sibling_text" 4 .IX Item "next_sibling_text" .IP "prev_elt_text" 4 .IX Item "prev_elt_text" .IP "next_elt_text" 4 .IX Item "next_elt_text" .IP "child_text" 4 .IX Item "child_text" .IP "parent_text" 4 .IX Item "parent_text" .RE .RS 4 .PD .Sp All this methods also exist in \*(L"trimmed\*(R" variant: .IP "first_child_trimmed_text" 4 .IX Item "first_child_trimmed_text" .PD 0 .IP "last_child_trimmed_text" 4 .IX Item "last_child_trimmed_text" .IP "prev_sibling_trimmed_text" 4 .IX Item "prev_sibling_trimmed_text" .IP "next_sibling_trimmed_text" 4 .IX Item "next_sibling_trimmed_text" .IP "prev_elt_trimmed_text" 4 .IX Item "prev_elt_trimmed_text" .IP "next_elt_trimmed_text" 4 .IX Item "next_elt_trimmed_text" .IP "child_trimmed_text" 4 .IX Item "child_trimmed_text" .IP "parent_trimmed_text" 4 .IX Item "parent_trimmed_text" .RE .RS 4 .RE .IP "field ($condition)" 4 .IX Item "field ($condition)" .PD Same method as \f(CW\*(C`first_child_text\*(C'\fR with a different name .IP "fields ($condition_list)" 4 .IX Item "fields ($condition_list)" Return the list of field (text of first child matching the conditions), missing fields are returned as the empty string. .Sp Same method as \f(CW\*(C`first_child_text\*(C'\fR with a different name .IP "trimmed_field ($optional_condition)" 4 .IX Item "trimmed_field ($optional_condition)" Same method as \f(CW\*(C`first_child_trimmed_text\*(C'\fR with a different name .ie n .IP "set_field ($condition, $optional_atts, @list_of_elt_and_strings)" 4 .el .IP "set_field ($condition, \f(CW$optional_atts\fR, \f(CW@list_of_elt_and_strings\fR)" 4 .IX Item "set_field ($condition, $optional_atts, @list_of_elt_and_strings)" Set the content of the first child of the element that matches \&\f(CW$condition\fR, the rest of the arguments is the same as for \f(CW\*(C`set_content\*(C'\fR .Sp If no child matches \f(CW$condition\fR _and_ if \f(CW$condition\fR is a valid \&\s-1XML\s0 element name, then a new element by that name is created and inserted as the last child. .IP "first_child_matches ($optional_condition)" 4 .IX Item "first_child_matches ($optional_condition)" Return the element if the first child of the element (if it exists) passes the \f(CW$optional_condition\fR \f(CW\*(C`undef\*(C'\fR otherwise .Sp .Vb 1 \& if( $elt\->first_child_matches( \*(Aqtitle\*(Aq)) ... .Ve .Sp is equivalent to .Sp .Vb 1 \& if( $elt\->{first_child} && $elt\->{first_child}\->passes( \*(Aqtitle\*(Aq)) .Ve .Sp \&\f(CW\*(C`first_child_is\*(C'\fR is another name for this method .Sp Similar methods are available for the other navigation methods: .RS 4 .IP "last_child_matches" 4 .IX Item "last_child_matches" .PD 0 .IP "prev_sibling_matches" 4 .IX Item "prev_sibling_matches" .IP "next_sibling_matches" 4 .IX Item "next_sibling_matches" .IP "prev_elt_matches" 4 .IX Item "prev_elt_matches" .IP "next_elt_matches" 4 .IX Item "next_elt_matches" .IP "child_matches" 4 .IX Item "child_matches" .IP "parent_matches" 4 .IX Item "parent_matches" .RE .RS 4 .RE .IP "is_first_child ($optional_condition)" 4 .IX Item "is_first_child ($optional_condition)" .PD returns true (the element) if the element is the first child of its parent (optionally that satisfies the \f(CW$optional_condition\fR) .IP "is_last_child ($optional_condition)" 4 .IX Item "is_last_child ($optional_condition)" returns true (the element) if the element is the last child of its parent (optionally that satisfies the \f(CW$optional_condition\fR) .IP "prev_sibling ($optional_condition)" 4 .IX Item "prev_sibling ($optional_condition)" Return the previous sibling of the element, or the previous sibling matching \&\f(CW$optional_condition\fR .IP "next_sibling ($optional_condition)" 4 .IX Item "next_sibling ($optional_condition)" Return the next sibling of the element, or the first one matching \&\f(CW$optional_condition\fR. .ie n .IP "next_elt ($optional_elt, $optional_condition)" 4 .el .IP "next_elt ($optional_elt, \f(CW$optional_condition\fR)" 4 .IX Item "next_elt ($optional_elt, $optional_condition)" Return the next elt (optionally matching \f(CW$optional_condition\fR) of the element. This is defined as the next element which opens after the current element opens. Which usually means the first child of the element. Counter-intuitive as it might look this allows you to loop through the whole document by starting from the root. .Sp The \f(CW$optional_elt\fR is the root of a subtree. When the \f(CW\*(C`next_elt\*(C'\fR is out of the subtree then the method returns undef. You can then walk a sub-tree with: .Sp .Vb 4 \& my $elt= $subtree_root; \& while( $elt= $elt\->next_elt( $subtree_root)) \& { # insert processing code here \& } .Ve .IP "prev_elt ($optional_condition)" 4 .IX Item "prev_elt ($optional_condition)" Return the previous elt (optionally matching \f(CW$optional_condition\fR) of the element. This is the first element which opens before the current one. It is usually either the last descendant of the previous sibling or simply the parent .ie n .IP "next_n_elt ($offset, $optional_condition)" 4 .el .IP "next_n_elt ($offset, \f(CW$optional_condition\fR)" 4 .IX Item "next_n_elt ($offset, $optional_condition)" Return the \f(CW$offset\fR\-th element that matches the \f(CW$optional_condition\fR .IP "following_elt" 4 .IX Item "following_elt" Return the following element (as per the XPath following axis) .IP "preceding_elt" 4 .IX Item "preceding_elt" Return the preceding element (as per the XPath preceding axis) .IP "following_elts" 4 .IX Item "following_elts" Return the list of following elements (as per the XPath following axis) .IP "preceding_elts" 4 .IX Item "preceding_elts" Return the list of preceding elements (as per the XPath preceding axis) .IP "children ($optional_condition)" 4 .IX Item "children ($optional_condition)" Return the list of children (optionally which matches \f(CW$optional_condition\fR) of the element. The list is in document order. .IP "children_count ($optional_condition)" 4 .IX Item "children_count ($optional_condition)" Return the number of children of the element (optionally which matches \&\f(CW$optional_condition\fR) .IP "children_text ($optional_condition)" 4 .IX Item "children_text ($optional_condition)" In array context, returns an array containing the text of children of the element (optionally which matches \f(CW$optional_condition\fR) .Sp In scalar context, returns the concatenation of the text of children of the element .IP "children_trimmed_text ($optional_condition)" 4 .IX Item "children_trimmed_text ($optional_condition)" In array context, returns an array containing the trimmed text of children of the element (optionally which matches \f(CW$optional_condition\fR) .Sp In scalar context, returns the concatenation of the trimmed text of children of the element .IP "children_copy ($optional_condition)" 4 .IX Item "children_copy ($optional_condition)" Return a list of elements that are copies of the children of the element, optionally which matches \f(CW$optional_condition\fR .IP "descendants ($optional_condition)" 4 .IX Item "descendants ($optional_condition)" Return the list of all descendants (optionally which matches \&\f(CW$optional_condition\fR) of the element. This is the equivalent of the \&\f(CW\*(C`getElementsByTagName\*(C'\fR of the \s-1DOM\s0 (by the way, if you are really a \s-1DOM\s0 addict, you can use \f(CW\*(C`getElementsByTagName\*(C'\fR instead) .IP "getElementsByTagName ($optional_condition)" 4 .IX Item "getElementsByTagName ($optional_condition)" Same as \f(CW\*(C`descendants\*(C'\fR .IP "find_by_tag_name ($optional_condition)" 4 .IX Item "find_by_tag_name ($optional_condition)" Same as \f(CW\*(C`descendants\*(C'\fR .IP "descendants_or_self ($optional_condition)" 4 .IX Item "descendants_or_self ($optional_condition)" Same as \f(CW\*(C`descendants\*(C'\fR except that the element itself is included in the list if it matches the \f(CW$optional_condition\fR .IP "first_descendant ($optional_condition)" 4 .IX Item "first_descendant ($optional_condition)" Return the first descendant of the element that matches the condition .IP "last_descendant ($optional_condition)" 4 .IX Item "last_descendant ($optional_condition)" Return the last descendant of the element that matches the condition .IP "ancestors ($optional_condition)" 4 .IX Item "ancestors ($optional_condition)" Return the list of ancestors (optionally matching \f(CW$optional_condition\fR) of the element. The list is ordered from the innermost ancestor to the outermost one .Sp \&\s-1NOTE:\s0 the element itself is not part of the list, in order to include it you will have to use ancestors_or_self .IP "ancestors_or_self ($optional_condition)" 4 .IX Item "ancestors_or_self ($optional_condition)" Return the list of ancestors (optionally matching \f(CW$optional_condition\fR) of the element, including the element (if it matches the condition>). The list is ordered from the innermost ancestor to the outermost one .IP "passes ($condition)" 4 .IX Item "passes ($condition)" Return the element if it passes the \f(CW$condition\fR .IP "att ($att)" 4 .IX Item "att ($att)" Return the value of attribute \f(CW$att\fR or \f(CW\*(C`undef\*(C'\fR .IP "latt ($att)" 4 .IX Item "latt ($att)" Return the value of attribute \f(CW$att\fR or \f(CW\*(C`undef\*(C'\fR .Sp this method is an lvalue, so you can do \f(CW\*(C`$elt\->latt( \*(Aqfoo\*(Aq)= \*(Aqbar\*(Aq\*(C'\fR or \f(CW\*(C`$elt\->latt( \*(Aqfoo\*(Aq)++;\*(C'\fR .ie n .IP "set_att ($att, $att_value)" 4 .el .IP "set_att ($att, \f(CW$att_value\fR)" 4 .IX Item "set_att ($att, $att_value)" Set the attribute of the element to the given value .Sp You can actually set several attributes this way: .Sp .Vb 1 \& $elt\->set_att( att1 => "val1", att2 => "val2"); .Ve .IP "del_att ($att)" 4 .IX Item "del_att ($att)" Delete the attribute for the element .Sp You can actually delete several attributes at once: .Sp .Vb 1 \& $elt\->del_att( \*(Aqatt1\*(Aq, \*(Aqatt2\*(Aq, \*(Aqatt3\*(Aq); .Ve .IP "att_exists ($att)" 4 .IX Item "att_exists ($att)" Returns true if the attribute \f(CW$att\fR exists for the element, false otherwise .IP "cut" 4 .IX Item "cut" Cut the element from the tree. The element still exists, it can be copied or pasted somewhere else, it is just not attached to the tree anymore. .Sp Note that the \*(L"old\*(R" links to the parent, previous and next siblings can still be accessed using the former_* methods .IP "former_next_sibling" 4 .IX Item "former_next_sibling" Returns the former next sibling of a cut node (or undef if the node has not been cut) .Sp This makes it easier to write loops where you cut elements: .Sp .Vb 3 \& my $child= $parent\->first_child( \*(Aqachild\*(Aq); \& while( $child\->{\*(Aqatt\*(Aq}\->{\*(Aqcut\*(Aq}) \& { $child\->cut; $child= ($child\->{former} && $child\->{former}\->{next_sibling}); } .Ve .IP "former_prev_sibling" 4 .IX Item "former_prev_sibling" Returns the former previous sibling of a cut node (or undef if the node has not been cut) .IP "former_parent" 4 .IX Item "former_parent" Returns the former parent of a cut node (or undef if the node has not been cut) .IP "cut_children ($optional_condition)" 4 .IX Item "cut_children ($optional_condition)" Cut all the children of the element (or all of those which satisfy the \&\f(CW$optional_condition\fR). .Sp Return the list of children .IP "cut_descendants ($optional_condition)" 4 .IX Item "cut_descendants ($optional_condition)" Cut all the descendants of the element (or all of those which satisfy the \&\f(CW$optional_condition\fR). .Sp Return the list of descendants .IP "copy ($elt)" 4 .IX Item "copy ($elt)" Return a copy of the element. The copy is a \*(L"deep\*(R" copy: all sub-elements of the element are duplicated. .ie n .IP "paste ($optional_position, $ref)" 4 .el .IP "paste ($optional_position, \f(CW$ref\fR)" 4 .IX Item "paste ($optional_position, $ref)" Paste a (previously \f(CW\*(C`cut\*(C'\fR or newly generated) element. Die if the element already belongs to a tree. .Sp Note that the calling element is pasted: .Sp .Vb 2 \& $child\->paste( first_child => $existing_parent); \& $new_sibling\->paste( after => $this_sibling_is_already_in_the_tree); .Ve .Sp or .Sp .Vb 2 \& my $new_elt= XML::Twig::Elt\->new( tag => $content); \& $new_elt\->paste( $position => $existing_elt); .Ve .Sp Example: .Sp .Vb 8 \& my $t= XML::Twig\->new\->parse( \*(Aqdoc.xml\*(Aq) \& my $toc= $t\->root\->new( \*(Aqtoc\*(Aq); \& $toc\->paste( $t\->root); # $toc is pasted as first child of the root \& foreach my $title ($t\->findnodes( \*(Aq/doc/section/title\*(Aq)) \& { my $title_toc= $title\->copy; \& # paste $title_toc as the last child of toc \& $title_toc\->paste( last_child => $toc) \& } .Ve .Sp Position options: .RS 4 .IP "first_child (default)" 4 .IX Item "first_child (default)" The element is pasted as the first child of \f(CW$ref\fR .IP "last_child" 4 .IX Item "last_child" The element is pasted as the last child of \f(CW$ref\fR .IP "before" 4 .IX Item "before" The element is pasted before \f(CW$ref\fR, as its previous sibling. .IP "after" 4 .IX Item "after" The element is pasted after \f(CW$ref\fR, as its next sibling. .IP "within" 4 .IX Item "within" In this case an extra argument, \f(CW$offset\fR, should be supplied. The element will be pasted in the reference element (or in its first text child) at the given offset. To achieve this the reference element will be split at the offset. .RE .RS 4 .Sp Note that you can call directly the underlying method: .IP "paste_before" 4 .IX Item "paste_before" .PD 0 .IP "paste_after" 4 .IX Item "paste_after" .IP "paste_first_child" 4 .IX Item "paste_first_child" .IP "paste_last_child" 4 .IX Item "paste_last_child" .IP "paste_within" 4 .IX Item "paste_within" .RE .RS 4 .RE .ie n .IP "move ($optional_position, $ref)" 4 .el .IP "move ($optional_position, \f(CW$ref\fR)" 4 .IX Item "move ($optional_position, $ref)" .PD Move an element in the tree. This is just a \f(CW\*(C`cut\*(C'\fR then a \f(CW\*(C`paste\*(C'\fR. The syntax is the same as \f(CW\*(C`paste\*(C'\fR. .IP "replace ($ref)" 4 .IX Item "replace ($ref)" Replaces an element in the tree. Sometimes it is just not possible to\f(CW\*(C`cut\*(C'\fR an element then \f(CW\*(C`paste\*(C'\fR another in its place, so \f(CW\*(C`replace\*(C'\fR comes in handy. The calling element replaces \f(CW$ref\fR. .IP "replace_with (@elts)" 4 .IX Item "replace_with (@elts)" Replaces the calling element with one or more elements .IP "delete" 4 .IX Item "delete" Cut the element and frees the memory. .ie n .IP "prefix ($text, $optional_option)" 4 .el .IP "prefix ($text, \f(CW$optional_option\fR)" 4 .IX Item "prefix ($text, $optional_option)" Add a prefix to an element. If the element is a \f(CW\*(C`PCDATA\*(C'\fR element the text is added to the pcdata, if the elements first child is a \f(CW\*(C`PCDATA\*(C'\fR then the text is added to it's pcdata, otherwise a new \f(CW\*(C`PCDATA\*(C'\fR element is created and pasted as the first child of the element. .Sp If the option is \f(CW\*(C`asis\*(C'\fR then the prefix is added asis: it is created in a separate \f(CW\*(C`PCDATA\*(C'\fR element with an \f(CW\*(C`asis\*(C'\fR property. You can then write: .Sp .Vb 1 \& $elt1\->prefix( \*(Aq\*(Aq, \*(Aqasis\*(Aq); .Ve .Sp to create a \f(CW\*(C`\*(C'\fR in the output of \f(CW\*(C`print\*(C'\fR. .ie n .IP "suffix ($text, $optional_option)" 4 .el .IP "suffix ($text, \f(CW$optional_option\fR)" 4 .IX Item "suffix ($text, $optional_option)" Add a suffix to an element. If the element is a \f(CW\*(C`PCDATA\*(C'\fR element the text is added to the pcdata, if the elements last child is a \f(CW\*(C`PCDATA\*(C'\fR then the text is added to it's pcdata, otherwise a new \s-1PCDATA\s0 element is created and pasted as the last child of the element. .Sp If the option is \f(CW\*(C`asis\*(C'\fR then the suffix is added asis: it is created in a separate \f(CW\*(C`PCDATA\*(C'\fR element with an \f(CW\*(C`asis\*(C'\fR property. You can then write: .Sp .Vb 1 \& $elt2\->suffix( \*(Aq\*(Aq, \*(Aqasis\*(Aq); .Ve .IP "trim" 4 .IX Item "trim" Trim the element in-place: spaces at the beginning and at the end of the element are discarded and multiple spaces within the element (or its descendants) are replaced by a single space. .Sp Note that in some cases you can still end up with multiple spaces, if they are split between several elements: .Sp .Vb 1 \& text hah! yep .Ve .Sp gets trimmed to .Sp .Vb 1 \& text hah! yep .Ve .Sp This is somewhere in between a bug and a feature. .IP "normalize" 4 .IX Item "normalize" merge together all consecutive pcdata elements in the element (if for example you have turned some elements into pcdata using \f(CW\*(C`erase\*(C'\fR, this will give you a \*(L"clean\*(R" element in which there all text fragments are as long as possible). .IP "simplify (%options)" 4 .IX Item "simplify (%options)" Return a data structure suspiciously similar to XML::Simple's. Options are identical to XMLin options, see XML::Simple doc for more details (or use DATA::dumper or \s-1YAML\s0 to dump the data structure) .Sp \&\fBNote\fR: there is no magic here, if you write \&\f(CW\*(C`$twig\->parsefile( $file )\->simplify();\*(C'\fR then it will load the entire document in memory. I am afraid you will have to put some work into it to get just the bits you want and discard the rest. Look at the synopsis or the XML::Twig 101 section at the top of the docs for more information. .RS 4 .IP "content_key" 4 .IX Item "content_key" .PD 0 .IP "forcearray" 4 .IX Item "forcearray" .IP "keyattr" 4 .IX Item "keyattr" .IP "noattr" 4 .IX Item "noattr" .IP "normalize_space" 4 .IX Item "normalize_space" .PD aka normalise_space .IP "variables (%var_hash)" 4 .IX Item "variables (%var_hash)" \&\f(CW%var_hash\fR is a hash { name => value } .Sp This option allows variables in the \s-1XML\s0 to be expanded when the file is read. (there is no facility for putting the variable names back if you regenerate \s-1XML\s0 using XMLout). .Sp A 'variable' is any text of the form ${name} (or \f(CW$name\fR) which occurs in an attribute value or in the text content of an element. If 'name' matches a key in the supplied hashref, ${name} will be replaced with the corresponding value from the hashref. If no matching key is found, the variable will not be replaced. .IP "var_att ($attribute_name)" 4 .IX Item "var_att ($attribute_name)" This option gives the name of an attribute that will be used to create variables in the \s-1XML:\s0 .Sp .Vb 4 \& \&

/usr/local \& $prefix/bin \& .Ve .Sp use \f(CW\*(C`var => \*(Aqname\*(Aq\*(C'\fR to get \f(CW$prefix\fR replaced by /usr/local in the generated data structure .Sp By default variables are captured by the following regexp: /$(\ew+)/ .IP "var_regexp (regexp)" 4 .IX Item "var_regexp (regexp)" This option changes the regexp used to capture variables. The variable name should be in \f(CW$1\fR .IP "group_tags { grouping tag => grouped tag, grouping tag 2 => grouped tag 2...}" 4 .IX Item "group_tags { grouping tag => grouped tag, grouping tag 2 => grouped tag 2...}" Option used to simplify the structure: elements listed will not be used. Their children will be, they will be considered children of the element parent. .Sp If the element is: .Sp .Vb 11 \& \& localhost \& \& /home/mrodrigu/standards \& $base/tools \& \& \& \& \& \& .Ve .Sp Then calling simplify with \f(CW\*(C`group_tags => { dirs => \*(Aqdir\*(Aq, templates => \*(Aqtemplate\*(Aq}\*(C'\fR makes the data structure be exactly as if the start and end tags for \f(CW\*(C`dirs\*(C'\fR and \&\f(CW\*(C`templates\*(C'\fR were not there. .Sp A \s-1YAML\s0 dump of the structure .Sp .Vb 7 \& base: \*(Aq/home/mrodrigu/standards\*(Aq \& host: laptop.xmltwig.org \& server: localhost \& template: \& \- std_def.templ \& \- dummy.templ \& tools: \*(Aq$base/tools\*(Aq .Ve .RE .RS 4 .RE .IP "split_at ($offset)" 4 .IX Item "split_at ($offset)" Split a text (\f(CW\*(C`PCDATA\*(C'\fR or \f(CW\*(C`CDATA\*(C'\fR) element in 2 at \f(CW$offset\fR, the original element now holds the first part of the string and a new element holds the right part. The new element is returned .Sp If the element is not a text element then the first text child of the element is split .ie n .IP "split ( $optional_regexp, $tag1, $atts1, $tag2, $atts2...)" 4 .el .IP "split ( \f(CW$optional_regexp\fR, \f(CW$tag1\fR, \f(CW$atts1\fR, \f(CW$tag2\fR, \f(CW$atts2\fR...)" 4 .IX Item "split ( $optional_regexp, $tag1, $atts1, $tag2, $atts2...)" Split the text descendants of an element in place, the text is split using the \f(CW$regexp\fR, if the regexp includes () then the matched separators will be wrapped in elements. \f(CW$1\fR is wrapped in \f(CW$tag1\fR, with attributes \f(CW$atts1\fR if \&\f(CW$atts1\fR is given (as a hashref), \f(CW$2\fR is wrapped in \f(CW$tag2\fR... .Sp if \f(CW$elt\fR is \f(CW\*(C`

tati tata tutu tati titi tata tati tata

\*(C'\fR .Sp .Vb 1 \& $elt\->split( qr/(ta)ti/, \*(Aqfoo\*(Aq, {type => \*(Aqtoto\*(Aq} ) .Ve .Sp will change \f(CW$elt\fR to .Sp .Vb 2 \&

ta tata tutu ta \& titi tata ta tata

.Ve .Sp The regexp can be passed either as a string or as \f(CW\*(C`qr//\*(C'\fR (perl 5.005 and later), it defaults to \es+ just as the \f(CW\*(C`split\*(C'\fR built-in (but this would be quite a useless behaviour without the \f(CW$optional_tag\fR parameter) .Sp \&\f(CW$optional_tag\fR defaults to \s-1PCDATA\s0 or \s-1CDATA,\s0 depending on the initial element type .Sp The list of descendants is returned (including un-touched original elements and newly created ones) .ie n .IP "mark ( $regexp, $optional_tag, $optional_attribute_ref)" 4 .el .IP "mark ( \f(CW$regexp\fR, \f(CW$optional_tag\fR, \f(CW$optional_attribute_ref\fR)" 4 .IX Item "mark ( $regexp, $optional_tag, $optional_attribute_ref)" This method behaves exactly as split, except only the newly created elements are returned .ie n .IP "wrap_children ( $regexp_string, $tag, $optional_attribute_hashref)" 4 .el .IP "wrap_children ( \f(CW$regexp_string\fR, \f(CW$tag\fR, \f(CW$optional_attribute_hashref\fR)" 4 .IX Item "wrap_children ( $regexp_string, $tag, $optional_attribute_hashref)" Wrap the children of the element that match the regexp in an element \f(CW$tag\fR. If \f(CW$optional_attribute_hashref\fR is passed then the new element will have these attributes. .Sp The \f(CW$regexp_string\fR includes tags, within pointy brackets, as in \&\f(CW\*(C`<para>+\*(C'\fR and the usual Perl modifiers (+*?...). Tags can be further qualified with attributes: \&\f(CW\*(C`<para type="warning" classif="cosmic_secret">+\*(C'\fR. The values for attributes should be xml-escaped: \f(CW\*(C`<candy type="M&Ms">*\*(C'\fR (\f(CW\*(C`<\*(C'\fR, \f(CW\*(C`&\*(C'\fR \fB\f(CB\*(C`>\*(C'\fB\fR and \f(CW\*(C`"\*(C'\fR should be escaped). .Sp Note that elements might get extra \f(CW\*(C`id\*(C'\fR attributes in the process. See add_id. Use strip_att to remove unwanted id's. .Sp Here is an example: .Sp If the element \f(CW$elt\fR has the following content: .Sp .Vb 10 \& <elt> \& <p>para 1</p> \& <l_l1_1>list 1 item 1 para 1</l_l1_1> \& <l_l1>list 1 item 1 para 2</l_l1> \& <l_l1_n>list 1 item 2 para 1 (only para)</l_l1_n> \& <l_l1_n>list 1 item 3 para 1</l_l1_n> \& <l_l1>list 1 item 3 para 2</l_l1> \& <l_l1>list 1 item 3 para 3</l_l1> \& <l_l1_1>list 2 item 1 para 1</l_l1_1> \& <l_l1>list 2 item 1 para 2</l_l1> \& <l_l1_n>list 2 item 2 para 1 (only para)</l_l1_n> \& <l_l1_n>list 2 item 3 para 1</l_l1_n> \& <l_l1>list 2 item 3 para 2</l_l1> \& <l_l1>list 2 item 3 para 3</l_l1> \& </elt> .Ve .Sp Then the code .Sp .Vb 2 \& $elt\->wrap_children( q{<l_l1_1><l_l1>*} , li => { type => "ul1" }); \& $elt\->wrap_children( q{<l_l1_n><l_l1>*} , li => { type => "ul" }); \& \& $elt\->wrap_children( q{<li type="ul1"><li type="ul">+}, "ul"); \& $elt\->strip_att( \*(Aqid\*(Aq); \& $elt\->strip_att( \*(Aqtype\*(Aq); \& $elt\->print; .Ve .Sp will output: .Sp .Vb 10 \& <elt> \& <p>para 1</p> \& <ul> \& <li> \& <l_l1_1>list 1 item 1 para 1</l_l1_1> \& <l_l1>list 1 item 1 para 2</l_l1> \& </li> \& <li> \& <l_l1_n>list 1 item 2 para 1 (only para)</l_l1_n> \& </li> \& <li> \& <l_l1_n>list 1 item 3 para 1</l_l1_n> \& <l_l1>list 1 item 3 para 2</l_l1> \& <l_l1>list 1 item 3 para 3</l_l1> \& </li> \& </ul> \& <ul> \& <li> \& <l_l1_1>list 2 item 1 para 1</l_l1_1> \& <l_l1>list 2 item 1 para 2</l_l1> \& </li> \& <li> \& <l_l1_n>list 2 item 2 para 1 (only para)</l_l1_n> \& </li> \& <li> \& <l_l1_n>list 2 item 3 para 1</l_l1_n> \& <l_l1>list 2 item 3 para 2</l_l1> \& <l_l1>list 2 item 3 para 3</l_l1> \& </li> \& </ul> \& </elt> .Ve .ie n .IP "subs_text ($regexp, $replace)" 4 .el .IP "subs_text ($regexp, \f(CW$replace\fR)" 4 .IX Item "subs_text ($regexp, $replace)" subs_text does text substitution, similar to perl's \f(CW\*(C` s///\*(C'\fR operator. .Sp \&\f(CW$regexp\fR must be a perl regexp, created with the \f(CW\*(C`qr\*(C'\fR operator. .Sp \&\f(CW$replace\fR can include \f(CW\*(C`$1, $2\*(C'\fR... from the \f(CW$regexp\fR. It can also be used to create element and entities, by using \&\f(CW\*(C`&elt( tag => { att => val }, text)\*(C'\fR (similar syntax as \f(CW\*(C`new\*(C'\fR) and \&\f(CW\*(C`&ent( name)\*(C'\fR. .Sp Here is a rather complex example: .Sp .Vb 3 \& $elt\->subs_text( qr{(?<!do not )link to (http://([^\es,]*))}, \& \*(Aqsee &elt( a =>{ href => $1 }, $2)\*(Aq \& ); .Ve .Sp This will replace text like \fIlink to http://www.xmltwig.org\fR by \&\fIsee <a href=\*(L"www.xmltwig.org\*(R">www.xmltwig.org</a>\fR, but not \&\fIdo not link to...\fR .Sp Generating entities (here replacing spaces with  ): .Sp .Vb 1 \& $elt\->subs_text( qr{ }, \*(Aq&ent( " ")\*(Aq); .Ve .Sp or, using a variable: .Sp .Vb 2 \& my $ent=" "; \& $elt\->subs_text( qr{ }, "&ent( \*(Aq$ent\*(Aq)"); .Ve .Sp Note that the substitution is always global, as in using the \f(CW\*(C`g\*(C'\fR modifier in a perl substitution, and that it is performed on all text descendants of the element. .Sp \&\fBBug\fR: in the \f(CW$regexp\fR, you can only use \f(CW\*(C`\e1\*(C'\fR, \f(CW\*(C`\e2\*(C'\fR... if the replacement expression does not include elements or attributes. eg .Sp .Vb 2 \& $t\->subs_text( qr/((t[aiou])\e2)/, \*(Aq$2\*(Aq); # ok, replaces toto, tata, titi, tutu by to, ta, ti, tu \& $t\->subs_text( qr/((t[aiou])\e2)/, \*(Aq&elt(p => $1)\*(Aq ); # NOK, does not find toto... .Ve .IP "add_id ($optional_coderef)" 4 .IX Item "add_id ($optional_coderef)" Add an id to the element. .Sp The id is an attribute, \f(CW\*(C`id\*(C'\fR by default, see the \f(CW\*(C`id\*(C'\fR option for XML::Twig \&\f(CW\*(C`new\*(C'\fR to change it. Use an id starting with \f(CW\*(C`#\*(C'\fR to get an id that's not output by print, flush or sprint, yet that allows you to use the elt_id method to get the element easily. .Sp If the element already has an id, no new id is generated. .Sp By default the method create an id of the form \f(CW\*(C`twig_id_<nnnn>\*(C'\fR, where \f(CW\*(C`<nnnn>\*(C'\fR is a number, incremented each time the method is called successfully. .IP "set_id_seed ($prefix)" 4 .IX Item "set_id_seed ($prefix)" by default the id generated by \f(CW\*(C`add_id\*(C'\fR is \f(CW\*(C`twig_id_<nnnn>\*(C'\fR, \&\f(CW\*(C`set_id_seed\*(C'\fR changes the prefix to \f(CW$prefix\fR and resets the number to 1 .IP "strip_att ($att)" 4 .IX Item "strip_att ($att)" Remove the attribute \f(CW$att\fR from all descendants of the element (including the element) .Sp Return the element .ie n .IP "change_att_name ($old_name, $new_name)" 4 .el .IP "change_att_name ($old_name, \f(CW$new_name\fR)" 4 .IX Item "change_att_name ($old_name, $new_name)" Change the name of the attribute from \f(CW$old_name\fR to \f(CW$new_name\fR. If there is no attribute \f(CW$old_name\fR nothing happens. .IP "lc_attnames" 4 .IX Item "lc_attnames" Lower cases the name all the attributes of the element. .ie n .IP "sort_children_on_value( %options)" 4 .el .IP "sort_children_on_value( \f(CW%options\fR)" 4 .IX Item "sort_children_on_value( %options)" Sort the children of the element in place according to their text. All children are sorted. .Sp Return the element, with its children sorted. .Sp \&\f(CW%options\fR are .Sp .Vb 2 \& type : numeric | alpha (default: alpha) \& order : normal | reverse (default: normal) .Ve .Sp Return the element, with its children sorted .ie n .IP "sort_children_on_att ($att, %options)" 4 .el .IP "sort_children_on_att ($att, \f(CW%options\fR)" 4 .IX Item "sort_children_on_att ($att, %options)" Sort the children of the element in place according to attribute \f(CW$att\fR. \&\f(CW%options\fR are the same as for \f(CW\*(C`sort_children_on_value\*(C'\fR .Sp Return the element. .ie n .IP "sort_children_on_field ($tag, %options)" 4 .el .IP "sort_children_on_field ($tag, \f(CW%options\fR)" 4 .IX Item "sort_children_on_field ($tag, %options)" Sort the children of the element in place, according to the field \f(CW$tag\fR (the text of the first child of the child with this tag). \f(CW%options\fR are the same as for \f(CW\*(C`sort_children_on_value\*(C'\fR. .Sp Return the element, with its children sorted .ie n .IP "sort_children( $get_key, %options)" 4 .el .IP "sort_children( \f(CW$get_key\fR, \f(CW%options\fR)" 4 .IX Item "sort_children( $get_key, %options)" Sort the children of the element in place. The \f(CW$get_key\fR argument is a reference to a function that returns the sort key when passed an element. .Sp For example: .Sp .Vb 3 \& $elt\->sort_children( sub { $_[0]\->{\*(Aqatt\*(Aq}\->{"nb"} + $_[0]\->text }, \& type => \*(Aqnumeric\*(Aq, order => \*(Aqreverse\*(Aq \& ); .Ve .ie n .IP "field_to_att ($cond, $att)" 4 .el .IP "field_to_att ($cond, \f(CW$att\fR)" 4 .IX Item "field_to_att ($cond, $att)" Turn the text of the first sub-element matched by \f(CW$cond\fR into the value of attribute \f(CW$att\fR of the element. If \f(CW$att\fR is omitted then \f(CW$cond\fR is used as the name of the attribute, which makes sense only if \f(CW$cond\fR is a valid element (and attribute) name. .Sp The sub-element is then cut. .ie n .IP "att_to_field ($att, $tag)" 4 .el .IP "att_to_field ($att, \f(CW$tag\fR)" 4 .IX Item "att_to_field ($att, $tag)" Take the value of attribute \f(CW$att\fR and create a sub-element \f(CW$tag\fR as first child of the element. If \f(CW$tag\fR is omitted then \f(CW$att\fR is used as the name of the sub-element. .ie n .IP "get_xpath ($xpath, $optional_offset)" 4 .el .IP "get_xpath ($xpath, \f(CW$optional_offset\fR)" 4 .IX Item "get_xpath ($xpath, $optional_offset)" Return a list of elements satisfying the \f(CW$xpath\fR. \f(CW$xpath\fR is an XPATH-like expression. .Sp A subset of the \s-1XPATH\s0 abbreviated syntax is covered: .Sp .Vb 10 \& tag \& tag[1] (or any other positive number) \& tag[last()] \& tag[@att] (the attribute exists for the element) \& tag[@att="val"] \& tag[@att=~ /regexp/] \& tag[att1="val1" and att2="val2"] \& tag[att1="val1" or att2="val2"] \& tag[string()="toto"] (returns tag elements which text (as per the text method) \& is toto) \& tag[string()=~/regexp/] (returns tag elements which text (as per the text \& method) matches regexp) \& expressions can start with / (search starts at the document root) \& expressions can start with . (search starts at the current element) \& // can be used to get all descendants instead of just direct children \& * matches any tag .Ve .Sp So the following examples from the \&\fIXPath recommendation<http://www.w3.org/TR/xpath.html#path\-abbrev>\fR work: .Sp .Vb 10 \& para selects the para element children of the context node \& * selects all element children of the context node \& para[1] selects the first para child of the context node \& para[last()] selects the last para child of the context node \& */para selects all para grandchildren of the context node \& /doc/chapter[5]/section[2] selects the second section of the fifth chapter \& of the doc \& chapter//para selects the para element descendants of the chapter element \& children of the context node \& //para selects all the para descendants of the document root and thus selects \& all para elements in the same document as the context node \& //olist/item selects all the item elements in the same document as the \& context node that have an olist parent \& .//para selects the para element descendants of the context node \& .. selects the parent of the context node \& para[@type="warning"] selects all para children of the context node that have \& a type attribute with value warning \& employee[@secretary and @assistant] selects all the employee children of the \& context node that have both a secretary attribute and an assistant \& attribute .Ve .Sp The elements will be returned in the document order. .Sp If \f(CW$optional_offset\fR is used then only one element will be returned, the one with the appropriate offset in the list, starting at 0 .Sp Quoting and interpolating variables can be a pain when the Perl syntax and the \&\s-1XPATH\s0 syntax collide, so use alternate quoting mechanisms like q or qq (I like q{} and qq{} myself). .Sp Here are some more examples to get you started: .Sp .Vb 3 \& my $p1= "p1"; \& my $p2= "p2"; \& my @res= $t\->get_xpath( qq{p[string( "$p1") or string( "$p2")]}); \& \& my $a= "a1"; \& my @res= $t\->get_xpath( qq{//*[@att="$a"]}); \& \& my $val= "a1"; \& my $exp= qq{//p[ \e@att=\*(Aq$val\*(Aq]}; # you need to use \e@ or you will get a warning \& my @res= $t\->get_xpath( $exp); .Ve .Sp Note that the only supported regexps delimiters are / and that you must backslash all / in regexps \s-1AND\s0 in regular strings. .Sp XML::Twig does not provide natively full \s-1XPATH\s0 support, but you can use \&\f(CW\*(C`XML::Twig::XPath\*(C'\fR to get \f(CW\*(C`findnodes\*(C'\fR to use \f(CW\*(C`XML::XPath\*(C'\fR as the XPath engine, with full coverage of the spec. .Sp \&\f(CW\*(C`XML::Twig::XPath\*(C'\fR to get \f(CW\*(C`findnodes\*(C'\fR to use \f(CW\*(C`XML::XPath\*(C'\fR as the XPath engine, with full coverage of the spec. .IP "find_nodes" 4 .IX Item "find_nodes" same as\f(CW\*(C`get_xpath\*(C'\fR .IP "findnodes" 4 .IX Item "findnodes" same as \f(CW\*(C`get_xpath\*(C'\fR .ie n .IP "text @optional_options" 4 .el .IP "text \f(CW@optional_options\fR" 4 .IX Item "text @optional_options" Return a string consisting of all the \f(CW\*(C`PCDATA\*(C'\fR and \f(CW\*(C`CDATA\*(C'\fR in an element, without any tags. The text is not XML-escaped: base entities such as \f(CW\*(C`&\*(C'\fR and \f(CW\*(C`<\*(C'\fR are not escaped. .Sp The '\f(CW\*(C`no_recurse\*(C'\fR' option will only return the text of the element, not of any included sub-elements (same as \f(CW\*(C`text_only\*(C'\fR). .IP "text_only" 4 .IX Item "text_only" Same as \f(CW\*(C`text\*(C'\fR except that the text returned doesn't include the text of sub-elements. .IP "trimmed_text" 4 .IX Item "trimmed_text" Same as \f(CW\*(C`text\*(C'\fR except that the text is trimmed: leading and trailing spaces are discarded, consecutive spaces are collapsed .IP "set_text ($string)" 4 .IX Item "set_text ($string)" Set the text for the element: if the element is a \f(CW\*(C`PCDATA\*(C'\fR, just set its text, otherwise cut all the children of the element and create a single \&\f(CW\*(C`PCDATA\*(C'\fR child for it, which holds the text. .IP "merge ($elt2)" 4 .IX Item "merge ($elt2)" Move the content of \f(CW$elt2\fR within the element .ie n .IP "insert ($tag1, [$optional_atts1], $tag2, [$optional_atts2],...)" 4 .el .IP "insert ($tag1, [$optional_atts1], \f(CW$tag2\fR, [$optional_atts2],...)" 4 .IX Item "insert ($tag1, [$optional_atts1], $tag2, [$optional_atts2],...)" For each tag in the list inserts an element \f(CW$tag\fR as the only child of the element. The element gets the optional attributes in\f(CW\*(C`$optional_atts<n>.\*(C'\fR All children of the element are set as children of the new element. The upper level element is returned. .Sp .Vb 1 \& $p\->insert( table => { border=> 1}, \*(Aqtr\*(Aq, \*(Aqtd\*(Aq) .Ve .Sp put \f(CW$p\fR in a table with a visible border, a single \f(CW\*(C`tr\*(C'\fR and a single \f(CW\*(C`td\*(C'\fR and return the \f(CW\*(C`table\*(C'\fR element: .Sp .Vb 1 \& <p><table border="1"><tr><td>original content of p</td></tr></table></p> .Ve .IP "wrap_in (@tag)" 4 .IX Item "wrap_in (@tag)" Wrap elements in \f(CW@tag\fR as the successive ancestors of the element, returns the new element. \&\f(CW\*(C`$elt\->wrap_in( \*(Aqtd\*(Aq, \*(Aqtr\*(Aq, \*(Aqtable\*(Aq)\*(C'\fR wraps the element as a single cell in a table for example. .Sp Optionally each tag can be followed by a hashref of attributes, that will be set on the wrapping element: .Sp .Vb 1 \& $elt\->wrap_in( p => { class => "advisory" }, div => { class => "intro", id => "div_intro" }); .Ve .ie n .IP "insert_new_elt ($opt_position, $tag, $opt_atts_hashref, @opt_content)" 4 .el .IP "insert_new_elt ($opt_position, \f(CW$tag\fR, \f(CW$opt_atts_hashref\fR, \f(CW@opt_content\fR)" 4 .IX Item "insert_new_elt ($opt_position, $tag, $opt_atts_hashref, @opt_content)" Combines a \f(CW\*(C`new \*(C'\fR and a \f(CW\*(C`paste \*(C'\fR: creates a new element using \&\f(CW$tag\fR, \f(CW$opt_atts_hashref \fRand \f(CW@opt_content\fR which are arguments similar to those for \f(CW\*(C`new\*(C'\fR, then paste it, using \f(CW$opt_position\fR or \f(CW\*(Aqfirst_child\*(Aq\fR, relative to \f(CW$elt\fR. .Sp Return the newly created element .IP "erase" 4 .IX Item "erase" Erase the element: the element is deleted and all of its children are pasted in its place. .ie n .IP "set_content ( $optional_atts, @list_of_elt_and_strings) ( $optional_atts, '#EMPTY')" 4 .el .IP "set_content ( \f(CW$optional_atts\fR, \f(CW@list_of_elt_and_strings\fR) ( \f(CW$optional_atts\fR, '#EMPTY')" 4 .IX Item "set_content ( $optional_atts, @list_of_elt_and_strings) ( $optional_atts, '#EMPTY')" Set the content for the element, from a list of strings and elements. Cuts all the element children, then pastes the list elements as the children. This method will create a \f(CW\*(C`PCDATA\*(C'\fR element for any strings in the list. .Sp The \f(CW$optional_atts\fR argument is the ref of a hash of attributes. If this argument is used then the previous attributes are deleted, otherwise they are left untouched. .Sp \&\fB\s-1WARNING\s0\fR: if you rely on \s-1ID\s0's then you will have to set the id yourself. At this point the element does not belong to a twig yet, so the \s-1ID\s0 attribute is not known so it won't be stored in the \s-1ID\s0 list. .Sp A content of '\f(CW\*(C`#EMPTY\*(C'\fR' creates an empty element; .IP "namespace ($optional_prefix)" 4 .IX Item "namespace ($optional_prefix)" Return the \s-1URI\s0 of the namespace that \f(CW$optional_prefix\fR or the element name belongs to. If the name doesn't belong to any namespace, \f(CW\*(C`undef\*(C'\fR is returned. .IP "local_name" 4 .IX Item "local_name" Return the local name (without the prefix) for the element .IP "ns_prefix" 4 .IX Item "ns_prefix" Return the namespace prefix for the element .IP "current_ns_prefixes" 4 .IX Item "current_ns_prefixes" Return a list of namespace prefixes valid for the element. The order of the prefixes in the list has no meaning. If the default namespace is currently bound, '' appears in the list. .ie n .IP "inherit_att ($att, @optional_tag_list)" 4 .el .IP "inherit_att ($att, \f(CW@optional_tag_list\fR)" 4 .IX Item "inherit_att ($att, @optional_tag_list)" Return the value of an attribute inherited from parent tags. The value returned is found by looking for the attribute in the element then in turn in each of its ancestors. If the \f(CW@optional_tag_list\fR is supplied only those ancestors whose tag is in the list will be checked. .IP "all_children_are ($optional_condition)" 4 .IX Item "all_children_are ($optional_condition)" return 1 if all children of the element pass the \f(CW$optional_condition\fR, 0 otherwise .IP "level ($optional_condition)" 4 .IX Item "level ($optional_condition)" Return the depth of the element in the twig (root is 0). If \f(CW$optional_condition\fR is given then only ancestors that match the condition are counted. .Sp \&\fB\s-1WARNING\s0\fR: in a tree created using the \f(CW\*(C`twig_roots\*(C'\fR option this will not return the level in the document tree, level 0 will be the document root, level 1 will be the \f(CW\*(C`twig_roots\*(C'\fR elements. During the parsing (in a \f(CW\*(C`twig_handler\*(C'\fR) you can use the \f(CW\*(C`depth\*(C'\fR method on the twig object to get the real parsing depth. .IP "in ($potential_parent)" 4 .IX Item "in ($potential_parent)" Return true if the element is in the potential_parent (\f(CW$potential_parent\fR is an element) .ie n .IP "in_context ($cond, $optional_level)" 4 .el .IP "in_context ($cond, \f(CW$optional_level\fR)" 4 .IX Item "in_context ($cond, $optional_level)" Return true if the element is included in an element which passes \f(CW$cond\fR optionally within \f(CW$optional_level\fR levels. The returned value is the including element. .IP "pcdata" 4 .IX Item "pcdata" Return the text of a \f(CW\*(C`PCDATA\*(C'\fR element or \f(CW\*(C`undef\*(C'\fR if the element is not \&\f(CW\*(C`PCDATA\*(C'\fR. .IP "pcdata_xml_string" 4 .IX Item "pcdata_xml_string" Return the text of a \f(CW\*(C`PCDATA\*(C'\fR element or undef if the element is not \f(CW\*(C`PCDATA\*(C'\fR. The text is \*(L"XML-escaped\*(R" ('&' and '<' are replaced by '&' and '<') .IP "set_pcdata ($text)" 4 .IX Item "set_pcdata ($text)" Set the text of a \f(CW\*(C`PCDATA\*(C'\fR element. This method does not check that the element is indeed a \f(CW\*(C`PCDATA\*(C'\fR so usually you should use \f(CW\*(C`set_text\*(C'\fR instead. .IP "append_pcdata ($text)" 4 .IX Item "append_pcdata ($text)" Add the text at the end of a \f(CW\*(C`PCDATA\*(C'\fR element. .IP "is_cdata" 4 .IX Item "is_cdata" Return 1 if the element is a \f(CW\*(C`CDATA\*(C'\fR element, returns 0 otherwise. .IP "is_text" 4 .IX Item "is_text" Return 1 if the element is a \f(CW\*(C`CDATA\*(C'\fR or \f(CW\*(C`PCDATA\*(C'\fR element, returns 0 otherwise. .IP "cdata" 4 .IX Item "cdata" Return the text of a \f(CW\*(C`CDATA\*(C'\fR element or \f(CW\*(C`undef\*(C'\fR if the element is not \&\f(CW\*(C`CDATA\*(C'\fR. .IP "cdata_string" 4 .IX Item "cdata_string" Return the \s-1XML\s0 string of a \f(CW\*(C`CDATA\*(C'\fR element, including the opening and closing markers. .IP "set_cdata ($text)" 4 .IX Item "set_cdata ($text)" Set the text of a \f(CW\*(C`CDATA\*(C'\fR element. .IP "append_cdata ($text)" 4 .IX Item "append_cdata ($text)" Add the text at the end of a \f(CW\*(C`CDATA\*(C'\fR element. .IP "remove_cdata" 4 .IX Item "remove_cdata" Turns all \f(CW\*(C`CDATA\*(C'\fR sections in the element into regular \f(CW\*(C`PCDATA\*(C'\fR elements. This is useful when converting \s-1XML\s0 to \s-1HTML,\s0 as browsers do not support \s-1CDATA\s0 sections. .IP "extra_data" 4 .IX Item "extra_data" Return the extra_data (comments and \s-1PI\s0's) attached to an element .IP "set_extra_data ($extra_data)" 4 .IX Item "set_extra_data ($extra_data)" Set the extra_data (comments and \s-1PI\s0's) attached to an element .IP "append_extra_data ($extra_data)" 4 .IX Item "append_extra_data ($extra_data)" Append extra_data to the existing extra_data before the element (if no previous extra_data exists then it is created) .IP "set_asis" 4 .IX Item "set_asis" Set a property of the element that causes it to be output without being \s-1XML\s0 escaped by the print functions: if it contains \f(CW\*(C`a < b\*(C'\fR it will be output as such and not as \f(CW\*(C`a < b\*(C'\fR. This can be useful to create text elements that will be output as markup. Note that all \f(CW\*(C`PCDATA\*(C'\fR descendants of the element are also marked as having the property (they are the ones that are actually impacted by the change). .Sp If the element is a \f(CW\*(C`CDATA\*(C'\fR element it will also be output asis, without the \&\f(CW\*(C`CDATA\*(C'\fR markers. The same goes for any \f(CW\*(C`CDATA\*(C'\fR descendant of the element .IP "set_not_asis" 4 .IX Item "set_not_asis" Unsets the \f(CW\*(C`asis\*(C'\fR property for the element and its text descendants. .IP "is_asis" 4 .IX Item "is_asis" Return the \f(CW\*(C`asis\*(C'\fR property status of the element ( 1 or \f(CW\*(C`undef\*(C'\fR) .IP "closed" 4 .IX Item "closed" Return true if the element has been closed. Might be useful if you are somewhere in the tree, during the parse, and have no idea whether a parent element is completely loaded or not. .IP "get_type" 4 .IX Item "get_type" Return the type of the element: '\f(CW\*(C`#ELT\*(C'\fR' for \*(L"real\*(R" elements, or '\f(CW\*(C`#PCDATA\*(C'\fR', \&'\f(CW\*(C`#CDATA\*(C'\fR', '\f(CW\*(C`#COMMENT\*(C'\fR', '\f(CW\*(C`#ENT\*(C'\fR', '\f(CW\*(C`#PI\*(C'\fR' .IP "is_elt" 4 .IX Item "is_elt" Return the tag if the element is a \*(L"real\*(R" element, or 0 if it is \f(CW\*(C`PCDATA\*(C'\fR, \&\f(CW\*(C`CDATA\*(C'\fR... .IP "contains_only_text" 4 .IX Item "contains_only_text" Return 1 if the element does not contain any other \*(L"real\*(R" element .IP "contains_only ($exp)" 4 .IX Item "contains_only ($exp)" Return the list of children if all children of the element match the expression \f(CW$exp\fR .Sp .Vb 1 \& if( $para\->contains_only( \*(Aqtt\*(Aq)) { ... } .Ve .IP "contains_a_single ($exp)" 4 .IX Item "contains_a_single ($exp)" If the element contains a single child that matches the expression \f(CW$exp\fR returns that element. Otherwise returns 0. .IP "is_field" 4 .IX Item "is_field" same as \f(CW\*(C`contains_only_text\*(C'\fR .IP "is_pcdata" 4 .IX Item "is_pcdata" Return 1 if the element is a \f(CW\*(C`PCDATA\*(C'\fR element, returns 0 otherwise. .IP "is_ent" 4 .IX Item "is_ent" Return 1 if the element is an entity (an unexpanded entity) element, return 0 otherwise. .IP "is_empty" 4 .IX Item "is_empty" Return 1 if the element is empty, 0 otherwise .IP "set_empty" 4 .IX Item "set_empty" Flags the element as empty. No further check is made, so if the element is actually not empty the output will be messed. The only effect of this method is that the output will be \f(CW\*(C`<tag att="value""/>\*(C'\fR. .IP "set_not_empty" 4 .IX Item "set_not_empty" Flags the element as not empty. if it is actually empty then the element will be output as \f(CW\*(C`<tag att="value""></tag>\*(C'\fR .IP "is_pi" 4 .IX Item "is_pi" Return 1 if the element is a processing instruction (\f(CW\*(C`#PI\*(C'\fR) element, return 0 otherwise. .IP "target" 4 .IX Item "target" Return the target of a processing instruction .IP "set_target ($target)" 4 .IX Item "set_target ($target)" Set the target of a processing instruction .IP "data" 4 .IX Item "data" Return the data part of a processing instruction .IP "set_data ($data)" 4 .IX Item "set_data ($data)" Set the data of a processing instruction .ie n .IP "set_pi ($target, $data)" 4 .el .IP "set_pi ($target, \f(CW$data\fR)" 4 .IX Item "set_pi ($target, $data)" Set the target and data of a processing instruction .IP "pi_string" 4 .IX Item "pi_string" Return the string form of a processing instruction (\f(CW\*(C`<?target data?>\*(C'\fR) .IP "is_comment" 4 .IX Item "is_comment" Return 1 if the element is a comment (\f(CW\*(C`#COMMENT\*(C'\fR) element, return 0 otherwise. .IP "set_comment ($comment_text)" 4 .IX Item "set_comment ($comment_text)" Set the text for a comment .IP "comment" 4 .IX Item "comment" Return the content of a comment (just the text, not the \f(CW\*(C`<!\-\-\*(C'\fR and \f(CW\*(C`\-\->\*(C'\fR) .IP "comment_string" 4 .IX Item "comment_string" Return the \s-1XML\s0 string for a comment (\f(CW\*(C`<!\-\- comment \-\->\*(C'\fR) .Sp Note that an \s-1XML\s0 comment cannot start or end with a '\-', or include '\-\-' (http://www.w3.org/TR/2008/REC\-xml\-20081126/#sec\-comments), if that is the case (because you have created the comment yourself presumably, as it could not be in the input \s-1XML\s0), then a space will be inserted before an initial '\-', after a trailing one or between two '\-' in the comment (which could presumably mangle javascript \*(L"hidden\*(R" in an \s-1XHTML\s0 comment); .IP "set_ent ($entity)" 4 .IX Item "set_ent ($entity)" Set an (non-expanded) entity (\f(CW\*(C`#ENT\*(C'\fR). \f(CW$entity\fR) is the entity text (\f(CW\*(C`&ent;\*(C'\fR) .IP "ent" 4 .IX Item "ent" Return the entity for an entity (\f(CW\*(C`#ENT\*(C'\fR) element (\f(CW\*(C`&ent;\*(C'\fR) .IP "ent_name" 4 .IX Item "ent_name" Return the entity name for an entity (\f(CW\*(C`#ENT\*(C'\fR) element (\f(CW\*(C`ent\*(C'\fR) .IP "ent_string" 4 .IX Item "ent_string" Return the entity, either expanded if the expanded version is available, or non-expanded (\f(CW\*(C`&ent;\*(C'\fR) otherwise .ie n .IP "child ($offset, $optional_condition)" 4 .el .IP "child ($offset, \f(CW$optional_condition\fR)" 4 .IX Item "child ($offset, $optional_condition)" Return the \f(CW$offset\fR\-th child of the element, optionally the \f(CW$offset\fR\-th child that matches \f(CW$optional_condition\fR. The children are treated as a list, so \&\f(CW\*(C`$elt\->child( 0)\*(C'\fR is the first child, while \f(CW\*(C`$elt\->child( \-1)\*(C'\fR is the last child. .ie n .IP "child_text ($offset, $optional_condition)" 4 .el .IP "child_text ($offset, \f(CW$optional_condition\fR)" 4 .IX Item "child_text ($offset, $optional_condition)" Return the text of a child or \f(CW\*(C`undef\*(C'\fR if the sibling does not exist. Arguments are the same as child. .IP "last_child ($optional_condition)" 4 .IX Item "last_child ($optional_condition)" Return the last child of the element, or the last child matching \&\f(CW$optional_condition\fR (ie the last of the element children matching the condition). .IP "last_child_text ($optional_condition)" 4 .IX Item "last_child_text ($optional_condition)" Same as \f(CW\*(C`first_child_text\*(C'\fR but for the last child. .ie n .IP "sibling ($offset, $optional_condition)" 4 .el .IP "sibling ($offset, \f(CW$optional_condition\fR)" 4 .IX Item "sibling ($offset, $optional_condition)" Return the next or previous \f(CW$offset\fR\-th sibling of the element, or the \&\f(CW$offset\fR\-th one matching \f(CW$optional_condition\fR. If \f(CW$offset\fR is negative then a previous sibling is returned, if \f(CW$offset\fR is positive then a next sibling is returned. \f(CW\*(C`$offset=0\*(C'\fR returns the element if there is no condition or if the element matches the condition>, \f(CW\*(C`undef\*(C'\fR otherwise. .ie n .IP "sibling_text ($offset, $optional_condition)" 4 .el .IP "sibling_text ($offset, \f(CW$optional_condition\fR)" 4 .IX Item "sibling_text ($offset, $optional_condition)" Return the text of a sibling or \f(CW\*(C`undef\*(C'\fR if the sibling does not exist. Arguments are the same as \f(CW\*(C`sibling\*(C'\fR. .IP "prev_siblings ($optional_condition)" 4 .IX Item "prev_siblings ($optional_condition)" Return the list of previous siblings (optionally matching \f(CW$optional_condition\fR) for the element. The elements are ordered in document order. .IP "next_siblings ($optional_condition)" 4 .IX Item "next_siblings ($optional_condition)" Return the list of siblings (optionally matching \f(CW$optional_condition\fR) following the element. The elements are ordered in document order. .IP "siblings ($optional_condition)" 4 .IX Item "siblings ($optional_condition)" Return the list of siblings (optionally matching \f(CW$optional_condition\fR) of the element (excluding the element itself). The elements are ordered in document order. .IP "pos ($optional_condition)" 4 .IX Item "pos ($optional_condition)" Return the position of the element in the children list. The first child has a position of 1 (as in XPath). .Sp If the \f(CW$optional_condition\fR is given then only siblings that match the condition are counted. If the element itself does not match the condition then 0 is returned. .IP "atts" 4 .IX Item "atts" Return a hash ref containing the element attributes .ie n .IP "set_atts ({ att1=>$att1_val, att2=> $att2_val... })" 4 .el .IP "set_atts ({ att1=>$att1_val, att2=> \f(CW$att2_val\fR... })" 4 .IX Item "set_atts ({ att1=>$att1_val, att2=> $att2_val... })" Set the element attributes with the hash ref supplied as the argument. The previous attributes are lost (ie the attributes set by \f(CW\*(C`set_atts\*(C'\fR replace all of the attributes of the element). .Sp You can also pass a list instead of a hashref: \f(CW\*(C`$elt\->set_atts( att1 => \*(Aqval1\*(Aq,...)\*(C'\fR .IP "del_atts" 4 .IX Item "del_atts" Deletes all the element attributes. .IP "att_nb" 4 .IX Item "att_nb" Return the number of attributes for the element .IP "has_atts" 4 .IX Item "has_atts" Return true if the element has attributes (in fact return the number of attributes, thus being an alias to \f(CW\*(C`att_nb\*(C'\fR .IP "has_no_atts" 4 .IX Item "has_no_atts" Return true if the element has no attributes, false (0) otherwise .IP "att_names" 4 .IX Item "att_names" return a list of the attribute names for the element .ie n .IP "att_xml_string ($att, $options)" 4 .el .IP "att_xml_string ($att, \f(CW$options\fR)" 4 .IX Item "att_xml_string ($att, $options)" Return the attribute value, where '&', '<' and quote (" or the value of the quote option at twig creation) are XML-escaped. .Sp The options are passed as a hashref, setting \f(CW\*(C`escape_gt\*(C'\fR to a true value will also escape \&'>' ($elt( 'myatt', { escape_gt => 1 }); .IP "set_id ($id)" 4 .IX Item "set_id ($id)" Set the \f(CW\*(C`id\*(C'\fR attribute of the element to the value. See \f(CW\*(C`elt_id \*(C'\fR to change the id attribute name .IP "id" 4 .IX Item "id" Gets the id attribute value .IP "del_id ($id)" 4 .IX Item "del_id ($id)" Deletes the \f(CW\*(C`id\*(C'\fR attribute of the element and remove it from the id list for the document .IP "class" 4 .IX Item "class" Return the \f(CW\*(C`class\*(C'\fR attribute for the element (methods on the \f(CW\*(C`class\*(C'\fR attribute are quite convenient when dealing with \s-1XHTML,\s0 or plain \s-1XML\s0 that will eventually be displayed using \s-1CSS\s0) .IP "lclass" 4 .IX Item "lclass" same as class, except that this method is an lvalue, so you can do \f(CW\*(C`$elt\->lclass= "foo"\*(C'\fR .IP "set_class ($class)" 4 .IX Item "set_class ($class)" Set the \f(CW\*(C`class\*(C'\fR attribute for the element to \f(CW$class\fR .IP "add_class ($class)" 4 .IX Item "add_class ($class)" Add \f(CW$class\fR to the element \f(CW\*(C`class\*(C'\fR attribute: the new class is added only if it is not already present. .Sp Note that classes are then sorted alphabetically, so the \f(CW\*(C`class\*(C'\fR attribute can be changed even if the class is already there .IP "remove_class ($class)" 4 .IX Item "remove_class ($class)" Remove \f(CW$class\fR from the element \f(CW\*(C`class\*(C'\fR attribute. .Sp Note that classes are then sorted alphabetically, so the \f(CW\*(C`class\*(C'\fR attribute can be changed even if the class is already there .IP "add_to_class ($class)" 4 .IX Item "add_to_class ($class)" alias for add_class .IP "att_to_class ($att)" 4 .IX Item "att_to_class ($att)" Set the \f(CW\*(C`class\*(C'\fR attribute to the value of attribute \f(CW$att\fR .IP "add_att_to_class ($att)" 4 .IX Item "add_att_to_class ($att)" Add the value of attribute \f(CW$att\fR to the \f(CW\*(C`class\*(C'\fR attribute of the element .IP "move_att_to_class ($att)" 4 .IX Item "move_att_to_class ($att)" Add the value of attribute \f(CW$att\fR to the \f(CW\*(C`class\*(C'\fR attribute of the element and delete the attribute .IP "tag_to_class" 4 .IX Item "tag_to_class" Set the \f(CW\*(C`class\*(C'\fR attribute of the element to the element tag .IP "add_tag_to_class" 4 .IX Item "add_tag_to_class" Add the element tag to its \f(CW\*(C`class\*(C'\fR attribute .IP "set_tag_class ($new_tag)" 4 .IX Item "set_tag_class ($new_tag)" Add the element tag to its \f(CW\*(C`class\*(C'\fR attribute and sets the tag to \f(CW$new_tag\fR .IP "in_class ($class)" 4 .IX Item "in_class ($class)" Return true (\f(CW1\fR) if the element is in the class \f(CW$class\fR (if \f(CW$class\fR is one of the tokens in the element \f(CW\*(C`class\*(C'\fR attribute) .IP "tag_to_span" 4 .IX Item "tag_to_span" Change the element tag tp \f(CW\*(C`span\*(C'\fR and set its class to the old tag .IP "tag_to_div" 4 .IX Item "tag_to_div" Change the element tag tp \f(CW\*(C`div\*(C'\fR and set its class to the old tag .IP "\s-1DESTROY\s0" 4 .IX Item "DESTROY" Frees the element from memory. .IP "start_tag" 4 .IX Item "start_tag" Return the string for the start tag for the element, including the \f(CW\*(C`/>\*(C'\fR at the end of an empty element tag .IP "end_tag" 4 .IX Item "end_tag" Return the string for the end tag of an element. For an empty element, this returns the empty string (''). .ie n .IP "xml_string @optional_options" 4 .el .IP "xml_string \f(CW@optional_options\fR" 4 .IX Item "xml_string @optional_options" Equivalent to \f(CW\*(C`$elt\->sprint( 1)\*(C'\fR, returns the string for the entire element, excluding the element's tags (but nested element tags are present) .Sp The '\f(CW\*(C`no_recurse\*(C'\fR' option will only return the text of the element, not of any included sub-elements (same as \f(CW\*(C`xml_text_only\*(C'\fR). .IP "inner_xml" 4 .IX Item "inner_xml" Another synonym for xml_string .IP "outer_xml" 4 .IX Item "outer_xml" Another synonym for sprint .IP "xml_text" 4 .IX Item "xml_text" Return the text of the element, encoded (and processed by the current \&\f(CW\*(C`output_filter\*(C'\fR or \f(CW\*(C`output_encoding\*(C'\fR options, without any tag. .IP "xml_text_only" 4 .IX Item "xml_text_only" Same as \f(CW\*(C`xml_text\*(C'\fR except that the text returned doesn't include the text of sub-elements. .IP "set_pretty_print ($style)" 4 .IX Item "set_pretty_print ($style)" Set the pretty print method, amongst '\f(CW\*(C`none\*(C'\fR' (default), '\f(CW\*(C`nsgmls\*(C'\fR', \&'\f(CW\*(C`nice\*(C'\fR', '\f(CW\*(C`indented\*(C'\fR', '\f(CW\*(C`record\*(C'\fR' and '\f(CW\*(C`record_c\*(C'\fR' .Sp pretty_print styles: .RS 4 .IP "none" 4 .IX Item "none" the default, no \f(CW\*(C`\en\*(C'\fR is used .IP "nsgmls" 4 .IX Item "nsgmls" nsgmls style, with \f(CW\*(C`\en\*(C'\fR added within tags .IP "nice" 4 .IX Item "nice" adds \f(CW\*(C`\en\*(C'\fR wherever possible (\s-1NOT SAFE,\s0 can lead to invalid \s-1XML\s0) .IP "indented" 4 .IX Item "indented" same as \f(CW\*(C`nice\*(C'\fR plus indents elements (\s-1NOT SAFE,\s0 can lead to invalid \s-1XML\s0) .IP "record" 4 .IX Item "record" table-oriented pretty print, one field per line .IP "record_c" 4 .IX Item "record_c" table-oriented pretty print, more compact than \f(CW\*(C`record\*(C'\fR, one record per line .RE .RS 4 .RE .IP "set_empty_tag_style ($style)" 4 .IX Item "set_empty_tag_style ($style)" Set the method to output empty tags, amongst '\f(CW\*(C`normal\*(C'\fR' (default), '\f(CW\*(C`html\*(C'\fR', and '\f(CW\*(C`expand\*(C'\fR', .Sp \&\f(CW\*(C`normal\*(C'\fR outputs an empty tag '\f(CW\*(C`<tag/>\*(C'\fR', \f(CW\*(C`html\*(C'\fR adds a space \&'\f(CW\*(C`<tag />\*(C'\fR' for elements that can be empty in \s-1XHTML\s0 and \f(CW\*(C`expand\*(C'\fR outputs \&'\f(CW\*(C`<tag></tag>\*(C'\fR' .IP "set_remove_cdata ($flag)" 4 .IX Item "set_remove_cdata ($flag)" set (or unset) the flag that forces the twig to output \s-1CDATA\s0 sections as regular (escaped) \s-1PCDATA\s0 .IP "set_indent ($string)" 4 .IX Item "set_indent ($string)" Set the indentation for the indented pretty print style (default is 2 spaces) .IP "set_quote ($quote)" 4 .IX Item "set_quote ($quote)" Set the quotes used for attributes. can be '\f(CW\*(C`double\*(C'\fR' (default) or '\f(CW\*(C`single\*(C'\fR' .IP "cmp ($elt)" 4 .IX Item "cmp ($elt)" .Vb 1 \& Compare the order of the 2 elements in a twig. \& \& C<$a> is the <A>..</A> element, C<$b> is the <B>...</B> element \& \& document $a\->cmp( $b) \& <A> ... </A> ... <B> ... </B> \-1 \& <A> ... <B> ... </B> ... </A> \-1 \& <B> ... </B> ... <A> ... </A> 1 \& <B> ... <A> ... </A> ... </B> 1 \& $a == $b 0 \& $a and $b not in the same tree undef .Ve .IP "before ($elt)" 4 .IX Item "before ($elt)" Return 1 if \f(CW$elt\fR starts before the element, 0 otherwise. If the 2 elements are not in the same twig then return \f(CW\*(C`undef\*(C'\fR. .Sp .Vb 1 \& if( $a\->cmp( $b) == \-1) { return 1; } else { return 0; } .Ve .IP "after ($elt)" 4 .IX Item "after ($elt)" Return 1 if \f(CW$elt\fR starts after the element, 0 otherwise. If the 2 elements are not in the same twig then return \f(CW\*(C`undef\*(C'\fR. .Sp .Vb 1 \& if( $a\->cmp( $b) == \-1) { return 1; } else { return 0; } .Ve .IP "other comparison methods" 4 .IX Item "other comparison methods" .RS 4 .PD 0 .IP "lt" 4 .IX Item "lt" .IP "le" 4 .IX Item "le" .IP "gt" 4 .IX Item "gt" .IP "ge" 4 .IX Item "ge" .RE .RS 4 .RE .IP "path" 4 .IX Item "path" .PD Return the element context in a form similar to XPath's short form: '\f(CW\*(C`/root/tag1/../tag\*(C'\fR' .IP "xpath" 4 .IX Item "xpath" Return a unique XPath expression that can be used to find the element again. .Sp It looks like \f(CW\*(C`/doc/sect[3]/title\*(C'\fR: unique elements do not have an index, the others do. .IP "flush" 4 .IX Item "flush" flushes the twig up to the current element (strictly equivalent to \&\f(CW\*(C`$elt\->root\->flush\*(C'\fR) .IP "private methods" 4 .IX Item "private methods" Low-level methods on the twig: .RS 4 .IP "set_parent ($parent)" 4 .IX Item "set_parent ($parent)" .PD 0 .IP "set_first_child ($first_child)" 4 .IX Item "set_first_child ($first_child)" .IP "set_last_child ($last_child)" 4 .IX Item "set_last_child ($last_child)" .IP "set_prev_sibling ($prev_sibling)" 4 .IX Item "set_prev_sibling ($prev_sibling)" .IP "set_next_sibling ($next_sibling)" 4 .IX Item "set_next_sibling ($next_sibling)" .IP "set_twig_current" 4 .IX Item "set_twig_current" .IP "del_twig_current" 4 .IX Item "del_twig_current" .IP "twig_current" 4 .IX Item "twig_current" .IP "contains_text" 4 .IX Item "contains_text" .RE .RS 4 .PD .Sp Those methods should not be used, unless of course you find some creative and interesting, not to mention useful, ways to do it. .RE .SS "cond" .IX Subsection "cond" Most of the navigation functions accept a condition as an optional argument The first element (or all elements for \f(CW\*(C`children \*(C'\fR or \&\f(CW\*(C`ancestors \*(C'\fR) that passes the condition is returned. .PP The condition is a single step of an XPath expression using the XPath subset defined by \f(CW\*(C`get_xpath\*(C'\fR. Additional conditions are: .PP The condition can be .IP "#ELT" 4 .IX Item "#ELT" return a \*(L"real\*(R" element (not a \s-1PCDATA, CDATA,\s0 comment or pi element) .IP "#TEXT" 4 .IX Item "#TEXT" return a \s-1PCDATA\s0 or \s-1CDATA\s0 element .IP "regular expression" 4 .IX Item "regular expression" return an element whose tag matches the regexp. The regexp has to be created with \f(CW\*(C`qr//\*(C'\fR (hence this is available only on perl 5.005 and above) .IP "code reference" 4 .IX Item "code reference" applies the code, passing the current element as argument, if the code returns true then the element is returned, if it returns false then the code is applied to the next candidate. .SS "XML::Twig::XPath" .IX Subsection "XML::Twig::XPath" XML::Twig implements a subset of XPath through the \f(CW\*(C`get_xpath\*(C'\fR method. .PP If you want to use the whole XPath power, then you can use \f(CW\*(C`XML::Twig::XPath\*(C'\fR instead. In this case \f(CW\*(C`XML::Twig\*(C'\fR uses \f(CW\*(C`XML::XPath\*(C'\fR to execute XPath queries. You will of course need \f(CW\*(C`XML::XPath\*(C'\fR installed to be able to use \f(CW\*(C`XML::Twig::XPath\*(C'\fR. .PP See XML::XPath for more information. .PP The methods you can use are: .IP "findnodes ($path)" 4 .IX Item "findnodes ($path)" return a list of nodes found by \f(CW$path\fR. .IP "findnodes_as_string ($path)" 4 .IX Item "findnodes_as_string ($path)" return the nodes found reproduced as \s-1XML.\s0 The result is not guaranteed to be valid \s-1XML\s0 though. .IP "findvalue ($path)" 4 .IX Item "findvalue ($path)" return the concatenation of the text content of the result nodes .PP In order for \f(CW\*(C`XML::XPath\*(C'\fR to be used as the XPath engine the following methods are included in \f(CW\*(C`XML::Twig\*(C'\fR: .PP in XML::Twig .IP "getRootNode" 4 .IX Item "getRootNode" .PD 0 .IP "getParentNode" 4 .IX Item "getParentNode" .IP "getChildNodes" 4 .IX Item "getChildNodes" .PD .PP in XML::Twig::Elt .IP "string_value" 4 .IX Item "string_value" .PD 0 .IP "toString" 4 .IX Item "toString" .IP "getName" 4 .IX Item "getName" .IP "getRootNode" 4 .IX Item "getRootNode" .IP "getNextSibling" 4 .IX Item "getNextSibling" .IP "getPreviousSibling" 4 .IX Item "getPreviousSibling" .IP "isElementNode" 4 .IX Item "isElementNode" .IP "isTextNode" 4 .IX Item "isTextNode" .IP "isPI" 4 .IX Item "isPI" .IP "isPINode" 4 .IX Item "isPINode" .IP "isProcessingInstructionNode" 4 .IX Item "isProcessingInstructionNode" .IP "isComment" 4 .IX Item "isComment" .IP "isCommentNode" 4 .IX Item "isCommentNode" .IP "getTarget" 4 .IX Item "getTarget" .IP "getChildNodes" 4 .IX Item "getChildNodes" .IP "getElementById" 4 .IX Item "getElementById" .PD .SS "XML::Twig::XPath::Elt" .IX Subsection "XML::Twig::XPath::Elt" The methods you can use are the same as on \f(CW\*(C`XML::Twig::XPath\*(C'\fR elements: .IP "findnodes ($path)" 4 .IX Item "findnodes ($path)" return a list of nodes found by \f(CW$path\fR. .IP "findnodes_as_string ($path)" 4 .IX Item "findnodes_as_string ($path)" return the nodes found reproduced as \s-1XML.\s0 The result is not guaranteed to be valid \s-1XML\s0 though. .IP "findvalue ($path)" 4 .IX Item "findvalue ($path)" return the concatenation of the text content of the result nodes .SS "XML::Twig::Entity_list" .IX Subsection "XML::Twig::Entity_list" .IP "new" 4 .IX Item "new" Create an entity list. .IP "add ($ent)" 4 .IX Item "add ($ent)" Add an entity to an entity list. .ie n .IP "add_new_ent ($name, $val, $sysid, $pubid, $ndata, $param)" 4 .el .IP "add_new_ent ($name, \f(CW$val\fR, \f(CW$sysid\fR, \f(CW$pubid\fR, \f(CW$ndata\fR, \f(CW$param\fR)" 4 .IX Item "add_new_ent ($name, $val, $sysid, $pubid, $ndata, $param)" Create a new entity and add it to the entity list .ie n .IP "delete ($ent or $tag)." 4 .el .IP "delete ($ent or \f(CW$tag\fR)." 4 .IX Item "delete ($ent or $tag)." Delete an entity (defined by its name or by the Entity object) from the list. .IP "print ($optional_filehandle)" 4 .IX Item "print ($optional_filehandle)" Print the entity list. .IP "list" 4 .IX Item "list" Return the list as an array .SS "XML::Twig::Entity" .IX Subsection "XML::Twig::Entity" .ie n .IP "new ($name, $val, $sysid, $pubid, $ndata, $param)" 4 .el .IP "new ($name, \f(CW$val\fR, \f(CW$sysid\fR, \f(CW$pubid\fR, \f(CW$ndata\fR, \f(CW$param\fR)" 4 .IX Item "new ($name, $val, $sysid, $pubid, $ndata, $param)" Same arguments as the Entity handler for XML::Parser. .IP "print ($optional_filehandle)" 4 .IX Item "print ($optional_filehandle)" Print an entity declaration. .IP "name" 4 .IX Item "name" Return the name of the entity .IP "val" 4 .IX Item "val" Return the value of the entity .IP "sysid" 4 .IX Item "sysid" Return the system id for the entity (for \s-1NDATA\s0 entities) .IP "pubid" 4 .IX Item "pubid" Return the public id for the entity (for \s-1NDATA\s0 entities) .IP "ndata" 4 .IX Item "ndata" Return true if the entity is an \s-1NDATA\s0 entity .IP "param" 4 .IX Item "param" Return true if the entity is a parameter entity .IP "text" 4 .IX Item "text" Return the entity declaration text. .SH "EXAMPLES" .IX Header "EXAMPLES" Additional examples (and a complete tutorial) can be found on the \&\fIXML::Twig Page<http://www.xmltwig.org/xmltwig/>\fR .PP To figure out what flush does call the following script with an \&\s-1XML\s0 file and an element name as arguments .PP .Vb 1 \& use XML::Twig; \& \& my ($file, $elt)= @ARGV; \& my $t= XML::Twig\->new( twig_handlers => \& { $elt => sub {$_[0]\->flush; print "\en[flushed here]\en";} }); \& $t\->parsefile( $file, ErrorContext => 2); \& $t\->flush; \& print "\en"; .Ve .SH "NOTES" .IX Header "NOTES" .SS "Subclassing XML::Twig" .IX Subsection "Subclassing XML::Twig" Useful methods: .IP "elt_class" 4 .IX Item "elt_class" In order to subclass \f(CW\*(C`XML::Twig\*(C'\fR you will probably need to subclass also \&\f(CW\*(C`XML::Twig::Elt\*(C'\fR. Use the \f(CW\*(C`elt_class\*(C'\fR option when you create the \&\f(CW\*(C`XML::Twig\*(C'\fR object to get the elements created in a different class (which should be a subclass of \f(CW\*(C`XML::Twig::Elt\*(C'\fR. .IP "add_options" 4 .IX Item "add_options" If you inherit \f(CW\*(C`XML::Twig\*(C'\fR new method but want to add more options to it you can use this method to prevent XML::Twig to issue warnings for those additional options. .SS "\s-1DTD\s0 Handling" .IX Subsection "DTD Handling" There are 3 possibilities here. They are: .IP "No \s-1DTD\s0" 4 .IX Item "No DTD" No doctype, no \s-1DTD\s0 information, no entity information, the world is simple... .IP "Internal \s-1DTD\s0" 4 .IX Item "Internal DTD" The \s-1XML\s0 document includes an internal \s-1DTD,\s0 and maybe entity declarations. .Sp If you use the load_DTD option when creating the twig the \s-1DTD\s0 information and the entity declarations can be accessed. .Sp The \s-1DTD\s0 and the entity declarations will be \f(CW\*(C`flush\*(C'\fR'ed (or \f(CW\*(C`print\*(C'\fR'ed) either as is (if they have not been modified) or as reconstructed (poorly, comments are lost, order is not kept, due to it's content this \s-1DTD\s0 should not be viewed by anyone) if they have been modified. You can also modify them directly by changing the \f(CW\*(C`$twig\->{twig_doctype}\->{internal}\*(C'\fR field (straight from XML::Parser, see the \f(CW\*(C`Doctype\*(C'\fR handler doc) .IP "External \s-1DTD\s0" 4 .IX Item "External DTD" The \s-1XML\s0 document includes a reference to an external \s-1DTD,\s0 and maybe entity declarations. .Sp If you use the \f(CW\*(C`load_DTD\*(C'\fR when creating the twig the \s-1DTD\s0 information and the entity declarations can be accessed. The entity declarations will be \&\f(CW\*(C`flush\*(C'\fR'ed (or \f(CW\*(C`print\*(C'\fR'ed) either as is (if they have not been modified) or as reconstructed (badly, comments are lost, order is not kept). .Sp You can change the doctype through the \f(CW\*(C`$twig\->set_doctype\*(C'\fR method and print the dtd through the \f(CW\*(C`$twig\->dtd_text\*(C'\fR or \f(CW\*(C`$twig\->dtd_print\*(C'\fR methods. .Sp If you need to modify the entity list this is probably the easiest way to do it. .SS "Flush" .IX Subsection "Flush" Remember that element handlers are called when the element is \s-1CLOSED,\s0 so if you have handlers for nested elements the inner handlers will be called first. It makes it for example trickier than it would seem to number nested sections (or clauses, or divs), as the titles in the inner sections are handled before the outer sections. .SH "BUGS" .IX Header "BUGS" .IP "segfault during parsing" 4 .IX Item "segfault during parsing" This happens when parsing huge documents, or lots of small ones, with a version of Perl before 5.16. .Sp This is due to a bug in the way weak references are handled in Perl itself. .Sp The fix is either to upgrade to Perl 5.16 or later (\f(CW\*(C`perlbrew\*(C'\fR is a great tool to manage several installations of perl on the same machine). .Sp An other, \s-1NOT RECOMMENDED,\s0 way of fixing the problem, is to switch off weak references by writing \f(CW\*(C`XML::Twig::_set_weakrefs( 0);\*(C'\fR at the top of the code. This is totally unsupported, and may lead to other problems though, .IP "entity handling" 4 .IX Item "entity handling" Due to XML::Parser behaviour, non-base entities in attribute values disappear if they are not declared in the document: \&\f(CW\*(C`att="val&ent;"\*(C'\fR will be turned into \f(CW\*(C`att => val\*(C'\fR, unless you use the \&\f(CW\*(C`keep_encoding\*(C'\fR argument to \f(CW\*(C`XML::Twig\->new\*(C'\fR .IP "\s-1DTD\s0 handling" 4 .IX Item "DTD handling" The \s-1DTD\s0 handling methods are quite bugged. No one uses them and it seems very difficult to get them to work in all cases, including with several slightly incompatible versions of XML::Parser and of libexpat. .Sp Basically you can read the \s-1DTD,\s0 output it back properly, and update entities, but not much more. .Sp So use XML::Twig with standalone documents, or with documents referring to an external \s-1DTD,\s0 but don't expect it to properly parse and even output back the \&\s-1DTD.\s0 .IP "memory leak" 4 .IX Item "memory leak" If you use a \s-1REALLY\s0 old Perl (5.005!) and a lot of twigs you might find that you leak quite a lot of memory (about 2Ks per twig). You can use the \f(CW\*(C`dispose \*(C'\fR method to free that memory after you are done. .Sp If you create elements the same thing might happen, use the \f(CW\*(C`delete\*(C'\fR method to get rid of them. .Sp Alternatively installing the \f(CW\*(C`Scalar::Util\*(C'\fR (or \f(CW\*(C`WeakRef\*(C'\fR) module on a version of Perl that supports it (>5.6.0) will get rid of the memory leaks automagically. .IP "\s-1ID\s0 list" 4 .IX Item "ID list" The \s-1ID\s0 list is \s-1NOT\s0 updated when elements are cut or deleted. .IP "change_gi" 4 .IX Item "change_gi" This method will not function properly if you do: .Sp .Vb 3 \& $twig\->change_gi( $old1, $new); \& $twig\->change_gi( $old2, $new); \& $twig\->change_gi( $new, $even_newer); .Ve .IP "sanity check on XML::Parser method calls" 4 .IX Item "sanity check on XML::Parser method calls" XML::Twig should really prevent calls to some XML::Parser methods, especially the \f(CW\*(C`setHandlers\*(C'\fR method. .IP "pretty printing" 4 .IX Item "pretty printing" Pretty printing (at least using the '\f(CW\*(C`indented\*(C'\fR' style) is hard to get right! Only elements that belong to the document will be properly indented. Printing elements that do not belong to the twig makes it impossible for XML::Twig to figure out their depth, and thus their indentation level. .Sp Also there is an unavoidable bug when using \f(CW\*(C`flush\*(C'\fR and pretty printing for elements with mixed content that start with an embedded element: .Sp .Vb 1 \& <elt><b>b</b>toto<b>bold</b></elt> \& \& will be output as \& \& <elt> \& <b>b</b>toto<b>bold</b></elt> .Ve .Sp if you flush the twig when you find the \f(CW\*(C`<b>\*(C'\fR element .SH "Globals" .IX Header "Globals" These are the things that can mess up calling code, especially if threaded. They might also cause problem under mod_perl. .IP "Exported constants" 4 .IX Item "Exported constants" Whether you want them or not you get them! These are subroutines to use as constant when creating or testing elements .Sp .Vb 3 \& PCDATA return \*(Aq#PCDATA\*(Aq \& CDATA return \*(Aq#CDATA\*(Aq \& PI return \*(Aq#PI\*(Aq, I had the choice between PROC and PI :\-\-( .Ve .IP "Module scoped values: constants" 4 .IX Item "Module scoped values: constants" these should cause no trouble: .Sp .Vb 12 \& %base_ent= ( \*(Aq>\*(Aq => \*(Aq>\*(Aq, \& \*(Aq<\*(Aq => \*(Aq<\*(Aq, \& \*(Aq&\*(Aq => \*(Aq&\*(Aq, \& "\*(Aq" => \*(Aq'\*(Aq, \& \*(Aq"\*(Aq => \*(Aq"\*(Aq, \& ); \& CDATA_START = "<![CDATA["; \& CDATA_END = "]]>"; \& PI_START = "<?"; \& PI_END = "?>"; \& COMMENT_START = "<!\-\-"; \& COMMENT_END = "\-\->"; .Ve .Sp pretty print styles .Sp .Vb 1 \& ( $NSGMLS, $NICE, $INDENTED, $INDENTED_C, $WRAPPED, $RECORD1, $RECORD2)= (1..7); .Ve .Sp empty tag output style .Sp .Vb 1 \& ( $HTML, $EXPAND)= (1..2); .Ve .IP "Module scoped values: might be changed" 4 .IX Item "Module scoped values: might be changed" Most of these deal with pretty printing, so the worst that can happen is probably that \s-1XML\s0 output does not look right, but is still valid and processed identically by \s-1XML\s0 processors. .Sp \&\f(CW$empty_tag_style\fR can mess up \s-1HTML\s0 bowsers though and changing \f(CW$ID\fR would most likely create problems. .Sp .Vb 5 \& $pretty=0; # pretty print style \& $quote=\*(Aq"\*(Aq; # quote for attributes \& $INDENT= \*(Aq \*(Aq; # indent for indented pretty print \& $empty_tag_style= 0; # how to display empty tags \& $ID # attribute used as an id (\*(Aqid\*(Aq by default) .Ve .IP "Module scoped values: definitely changed" 4 .IX Item "Module scoped values: definitely changed" These 2 variables are used to replace tags by an index, thus saving some space when creating a twig. If they really cause you too much trouble, let me know, it is probably possible to create either a switch or at least a version of XML::Twig that does not perform this optimization. .Sp .Vb 2 \& %gi2index; # tag => index \& @index2gi; # list of tags .Ve .PP If you need to manipulate all those values, you can use the following methods on the XML::Twig object: .IP "global_state" 4 .IX Item "global_state" Return a hashref with all the global variables used by XML::Twig .Sp The hash has the following fields: \f(CW\*(C`pretty\*(C'\fR, \f(CW\*(C`quote\*(C'\fR, \f(CW\*(C`indent\*(C'\fR, \&\f(CW\*(C`empty_tag_style\*(C'\fR, \f(CW\*(C`keep_encoding\*(C'\fR, \f(CW\*(C`expand_external_entities\*(C'\fR, \&\f(CW\*(C`output_filter\*(C'\fR, \f(CW\*(C`output_text_filter\*(C'\fR, \f(CW\*(C`keep_atts_order\*(C'\fR .IP "set_global_state ($state)" 4 .IX Item "set_global_state ($state)" Set the global state, \f(CW$state\fR is a hashref .IP "save_global_state" 4 .IX Item "save_global_state" Save the current global state .IP "restore_global_state" 4 .IX Item "restore_global_state" Restore the previously saved (using \f(CW\*(C`Lsave_global_state\*(C'\fR> state .SH "TODO" .IX Header "TODO" .IP "\s-1SAX\s0 handlers" 4 .IX Item "SAX handlers" Allowing XML::Twig to work on top of any \s-1SAX\s0 parser .IP "multiple twigs are not well supported" 4 .IX Item "multiple twigs are not well supported" A number of twig features are just global at the moment. These include the \s-1ID\s0 list and the \*(L"tag pool\*(R" (if you use \f(CW\*(C`change_gi\*(C'\fR then you change the tag for \s-1ALL\s0 twigs). .Sp A future version will try to support this while trying not to be to hard on performance (at least when a single twig is used!). .SH "AUTHOR" .IX Header "AUTHOR" Michel Rodriguez <mirod@cpan.org> .SH "LICENSE" .IX Header "LICENSE" This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .PP Bug reports should be sent using: \&\fI\s-1RT\s0 <http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML\-Twig>\fR .PP Comments can be sent to mirod@cpan.org .PP The XML::Twig page is at <http://www.xmltwig.org/xmltwig/> It includes the development version of the module, a slightly better version of the documentation, examples, a tutorial and a: \&\fIProcessing \s-1XML\s0 efficiently with Perl and XML::Twig: <http://www.xmltwig.org/xmltwig/tutorial/index.html>\fR .SH "SEE ALSO" .IX Header "SEE ALSO" Complete docs, including a tutorial, examples, an easier to use \s-1HTML\s0 version of the docs, a quick reference card and a \s-1FAQ\s0 are available at <http://www.xmltwig.org/xmltwig/> .PP git repository at <http://github.com/mirod/xmltwig> .PP XML::Parser, XML::Parser::Expat, XML::XPath, Encode, Text::Iconv, Scalar::Utils .SS "Alternative Modules" .IX Subsection "Alternative Modules" XML::Twig is not the only XML::Processing module available on \s-1CPAN\s0 (far from it!). .PP The main alternative I would recommend is XML::LibXML. .PP Here is a quick comparison of the 2 modules: .PP XML::LibXML, actually \f(CW\*(C`libxml2\*(C'\fR on which it is based, sticks to the standards, and implements a good number of them in a rather strict way: \s-1XML,\s0 XPath, \s-1DOM,\s0 RelaxNG, I must be forgetting a couple (XInclude?). It is fast and rather frugal memory-wise. .PP XML::Twig is older: when I started writing it XML::Parser/expat was the only game in town. It implements \s-1XML\s0 and that's about it (plus a subset of XPath, and you can use XML::Twig::XPath if you have XML::XPathEngine installed for full support). It is slower and requires more memory for a full tree than XML::LibXML. On the plus side (yes, there is a plus side!) it lets you process a big document in chunks, and thus let you tackle documents that couldn't be loaded in memory by XML::LibXML, and it offers a lot (and I mean a \s-1LOT\s0!) of higher-level methods, for everything, from adding structure to \*(L"low-level\*(R" \s-1XML,\s0 to shortcuts for \s-1XHTML\s0 conversions and more. It also DWIMs quite a bit, getting comments and non-significant whitespaces out of the way but preserving them in the output for example. As it does not stick to the \s-1DOM,\s0 is also usually leads to shorter code than in XML::LibXML. .PP Beyond the pure features of the 2 modules, XML::LibXML seems to be preferred by \&\*(L"XML-purists\*(R", while XML::Twig seems to be more used by Perl Hackers who have to deal with \s-1XML.\s0 As you have noted, XML::Twig also comes with quite a lot of docs, but I am sure if you ask for help about XML::LibXML here or on Perlmonks you will get answers. .PP Note that it is actually quite hard for me to compare the 2 modules: on one hand I know XML::Twig inside-out and I can get it to do pretty much anything I need to (or I improve it ;\-\-), while I have a very basic knowledge of XML::LibXML. So feature-wise, I'd rather use XML::Twig ;\-\-). On the other hand, I am painfully aware of some of the deficiencies, potential bugs and plain ugly code that lurk in XML::Twig, even though you are unlikely to be affected by them (unless for example you need to change the \s-1DTD\s0 of a document programmatically), while I haven't looked much into XML::LibXML so it still looks shinny and clean to me. .PP That said, if you need to process a document that is too big to fit memory and XML::Twig is too slow for you, my reluctant advice would be to use \*(L"bare\*(R" XML::Parser. It won't be as easy to use as XML::Twig: basically with XML::Twig you trade some speed (depending on what you do from a factor 3 to... none) for ease-of-use, but it will be easier \s-1IMHO\s0 than using \s-1SAX\s0 (albeit not standard), and at this point a \s-1LOT\s0 faster (see the last test in <http://www.xmltwig.org/article/simple_benchmark/>).