Debian Codegraph @

docs/libxml.dbk

raw
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!-- -*- nxml -*- -->
3 <book lang="en-us">
4 <title>XML::LibXML</title>
5
6 <bookinfo>
7 <authorgroup>
8 <author>
9 <firstname>Matt</firstname>
10 <surname>Sergeant</surname>
11 </author>
12
13 <author>
14 <firstname>Christian</firstname>
15 <surname>Glahn</surname>
16 </author>
17
18 <author>
19 <firstname>Petr</firstname>
20 <surname>Pajas</surname>
21 </author>
22 </authorgroup>
23
24
25 <edition>2.0134</edition>
26 <copyright>
27 <year>2001-2007</year>
28 <holder>AxKit.com Ltd</holder>
29 </copyright>
30 <copyright>
31 <year>2002-2006</year>
32 <holder>Christian Glahn</holder>
33 </copyright>
34 <copyright>
35 <year>2006-2009</year>
36 <holder>Petr Pajas</holder>
37 </copyright>
38 </bookinfo>
39
40 <chapter id="README">
41 <title>Introduction</title>
42
43 <titleabbrev>README</titleabbrev>
44
45 <para>This module implements a Perl interface to the Gnome
46 libxml2 library which provides
47 interfaces for parsing and manipulating XML files. This
48 module allows Perl programmers to make use of the highly
49 capable validating XML parser and the high performance DOM
50 implementation.</para>
51
52 <sect1>
53 <title>Important Notes</title>
54
55 <para>XML::LibXML was almost entirely reimplemented between version 1.40 to version 1.49. This may cause problems on some production machines. With
56 version 1.50 a lot of compatibility fixes were applied, so programs written for XML::LibXML 1.40 or less should run with version 1.50 again.</para>
57 <para>In 1.59, a new callback API was introduced. This new API is not compatible with the previous one.
58 See XML::LibXML::InputCallback manual page for details.</para>
59 <para>In 1.61 the XML::LibXML::XPathContext module, previously distributed separately, was merged in.</para>
60 <para>An experimental support for Perl threads introduced in 1.66 has been replaced in 1.67.</para>
61 </sect1>
62
63 <sect1>
64 <title>Dependencies</title>
65
66 <para>Prior to installation you MUST have installed the libxml2 library. You can get the latest libxml2 version from</para>
67
68 <para>http://xmlsoft.org/</para>
69
70 <para>Without libxml2 installed this module will neither build nor run.</para>
71
72 <para>Also XML::LibXML requires the following packages:</para>
73
74 <itemizedlist>
75 <listitem>
76 <para>XML::SAX - base class for SAX parsers</para>
77 </listitem>
78
79 <listitem>
80 <para>XML::NamespaceSupport - namespace support for SAX parsers</para>
81 </listitem>
82
83 </itemizedlist>
84
85 <para>These packages are required. If one is missing some tests will fail.</para>
86
87 <para>Again, libxml2 is required to make XML::LibXML work. The library is not just required to build XML::LibXML, it has to be accessible during
88 run-time as well. Because of this you need to make sure libxml2 is installed properly. To test this, run the xmllint program on your system. xmllint
89 is shipped with libxml2 and therefore should be available.
90 For building the module you will also need the header file for libxml2, which in binary
91 (.rpm,.deb) etc. distributions usually dwell in a package named libxml2-devel or similar.</para>
92 </sect1>
93
94 <sect1>
95 <title>Installation</title>
96 <para>(These instructions are for UNIX and GNU/Linux systems. For MSWin32,
97 See Notes for Microsoft Windows below.)</para>
98 <para>To install XML::LibXML just follow the standard installation routine for Perl modules:</para>
99
100 <orderedlist>
101 <listitem>
102 <para>perl Makefile.PL</para>
103 </listitem>
104
105 <listitem>
106 <para>make</para>
107 </listitem>
108
109 <listitem>
110 <para>make test</para>
111 </listitem>
112
113 <listitem>
114 <para>make install # as superuser</para>
115 </listitem>
116 </orderedlist>
117
118 <para>Note that XML::LibXML is an XS based Perl extension and you need a C compiler
119 to build it.</para>
120 <para>Note also that you should rebuild XML::LibXML if you upgrade libxml2
121 in order to avoid problems with possible binary incompatibilities between releases of the library.</para>
122
123 <sect2>
124 <title>Notes on libxml2 versions</title>
125
126 <para>XML::LibXML requires at least
127 libxml2 2.6.16 to compile and pass all tests and
128 at least 2.6.21 is required for XML::LibXML::Reader.
129 For some older OS versions this means that an
130 update of the pre-built packages is required.</para>
131
132 <para>Although libxml2 claims binary compatibility between
133 its patch levels, it is a good idea to recompile XML::LibXML
134 and run its tests after an upgrade of libxml2.
135 </para>
136
137 <para>If your libxml2 installation is not within your $PATH,
138 you can pass the XMLPREFIX=$YOURLIBXMLPREFIX parameter to Makefile.PL
139 determining the correct libxml2 version in use. e.g.
140 </para>
141
142 <programlisting> perl Makefile.PL XMLPREFIX=/usr/brand-new </programlisting>
143
144 <para>will ask '/usr/brand-new/bin/xml2-config' about your real libxml2 configuration.</para>
145
146 <para>Try to avoid setting INC and LIBS directly on the
147 command-line, for if used, Makefile.PL does not check
148 the libxml2 version for compatibility with XML::LibXML.</para>
149 </sect2>
150
151 <sect2>
152 <title>Which version of libxml2 should be used?</title>
153
154 <para>XML::LibXML is tested against a couple versions of
155 libxml2 before it is released. Thus there are versions
156 of libxml2 that are known not to work properly with
157 XML::LibXML. The Makefile.PL keeps a blacklist of
158 the incompatible libxml2 versions.</para>
159
160 <para>If Makefile.PL detects one of the incompatible versions,
161 it notifies the user. It may still happen that
162 XML::LibXML builds and pass its tests with such
163 a version, but that does not mean everything
164 is OK. There will be no support at all for blacklisted versions!</para>
165
166 <para>As of XML::LibXML 1.61, only versions 2.6.16 and higher are supported.
167 XML::LibXML will probably not compile with earlier libxml2 versions than
168 2.5.6. Versions prior to 2.6.8 are known to be broken for various reasons,
169 versions prior to 2.1.16 exhibit problems with namespaced attributes
170 and do not therefore pass XML::LibXML regression tests.
171 </para>
172
173 <para>It may happen that an unsupported version of libxml2
174 passes all tests under certain conditions. This is no
175 reason to assume that it shall work without problems.
176 If Makefile.PL marks a version of libxml2 as incompatible or broken
177 it is done for a good reason.</para>
178 </sect2>
179
180 <sect2>
181 <title>Notes for Microsoft Windows</title>
182
183 <para>Thanks to Randy Kobes there is a pre-compiled PPM package available on</para>
184 <para>http://theoryx5.uwinnipeg.ca/ppmpackages/</para>
185
186 <para>Usually it takes a little time to build the package for the latest release.</para>
187 <para>If you want to build XML::LibXML on Windows from source, you can use
188 the following instructions contributed by Christopher J. Madsen:</para>
189
190 <para>These instructions assume that you already have your system set up to
191 compile modules that use C components.
192 </para>
193 <para>
194 First, get the libxml2 binaries from http://xmlsoft.org/sources/win32/
195 (currently also available at http://www.zlatkovic.com/pub/libxml/).
196 </para>
197 <para>
198 You need:
199 </para>
200 <programlisting> iconv-VERSION.win32.zip
201 libxml2-VERSION.win32.zip
202 zlib-VERSION.win32.zip</programlisting>
203 <para>Download the latest version of each. (Each package will probably have
204 a different version.) When you extract them, you'll get directories
205 named iconv-VERSION.win32, libxml2-VERSION.win32, and
206 zlib-VERSION.win32, each containing bin, lib, and include directories.</para>
207 <para>Combine all the bin, include, and lib directories under c:\Prog\LibXML.
208 (You can use any directory you prefer; just adjust the instructions
209 accordingly.)</para>
210 <para>Get the latest version of XML-LibXML from CPAN.
211 Extract it.</para>
212 <para>Issue these commands in the XML-LibXML-VERSION directory:</para>
213 <programlisting> perl Makefile.PL INC=-Ic:\Prog\LibXML\include LIBS=-Lc:\Prog\LibXML\lib
214 nmake
215 copy c:\Prog\LibXML\bin\*.dll blib\arch\auto\XML\LibXML
216 nmake test
217 nmake install</programlisting>
218 <para>(Note: Some systems use dmake instead of nmake.)</para>
219 <para>By copying the libxml2 DLLs to the arch directory, you help avoid
220 conflicts with other programs you may have installed that use other
221 (possibly incompatible) versions of those DLLs.</para>
222 </sect2>
223 <sect2>
224 <title>Notes for Mac OS X</title>
225
226 <para>Due refactoring the module, XML::LibXML will not
227 run with some earlier versions of Mac OS X. It appears that this is related
228 to special linker options for that OS prior to version
229 10.2.2. Since the developers do not have full access to this OS,
230 help/ patches from OS X gurus are highly
231 appreciated.</para>
232
233 <para>It is confirmed that XML::LibXML builds and runs
234 without problems since Mac OS X 10.2.6.</para>
235 </sect2>
236
237 <sect2>
238 <title>Notes for HPUX</title>
239
240 <para>XML::LibXML requires libxml2 2.6.16 or
241 later. There may not exist a usable binary
242 libxml2 package for HPUX and XML::LibXML. If
243 HPUX cc does not compile libxml2
244 correctly, you will be forced to recompile perl with
245 gcc (unless you have already done that).</para>
246
247 <para>Additionally I received the following Note from Rozi Kovesdi:</para>
248
249 <programlisting>Here is my report if someone else runs into the same problem:
250
251 Finally I am done with installing all the libraries and XML Perl
252 modules
253
254 The combination that worked best for me was:
255 gcc
256 GNU make
257
258 Most importantly - before trying to install Perl modules that depend on
259 libxml2:
260
261 must set SHLIB_PATH to include the path to libxml2 shared library
262
263 assuming that you used the default:
264
265 export SHLIB=/usr/local/lib
266
267 also, make sure that the config files have execute permission:
268
269 /usr/local/bin/xml2-config
270 /usr/local/bin/xslt-config
271
272 they did not have +x after they were installed by 'make install'
273 and it took me a while to realize that this was my problem
274
275 or one can use:
276
277 perl Makefile.PL LIBS='-L/path/to/lib' INC='-I/path/to/include'</programlisting>
278 </sect2>
279 </sect1>
280
281 <sect1>
282 <title>Contact</title>
283
284 <para>For bug reports, please use the CPAN request tracker on http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-LibXML</para>
285
286 <para>For suggestions etc. you may contact the maintainer directly at "pajas at ufal dot mff dot cuni dot cz", but in general, it is recommended to use the mailing list given below.
287 </para>
288
289 <para>For suggestions etc., and other issues
290 related to XML::LibXML you may use the perl XML mailing list
291 (<email>[email protected]</email>),
292 where most XML-related Perl modules are discussed.
293 In case of problems you should check the archives of that
294 list first. Many problems are already discussed there. You
295 can find the list's archives and subscription options at
296 http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/perl-xml</para>
297 </sect1>
298
299 <sect1>
300 <title>Package History</title>
301
302 <para>Version &lt; 0.98 were maintained by Matt Sergeant</para>
303
304 <para>0.98 &gt; Version &gt; 1.49 were maintained by Matt Sergeant and Christian Glahn</para>
305
306 <para>Versions &gt;= 1.49 are maintained by Christian Glahn</para>
307
308 <para>Versions &gt; 1.56 are co-maintained by Petr Pajas</para>
309
310 <para>Versions &gt;= 1.59 are provisionally maintained by Petr Pajas</para>
311 </sect1>
312
313 <sect1>
314 <title>Patches and Developer Version</title>
315
316 <para>As XML::LibXML is open source software, help and
317 patches are appreciated. If you find a bug in the current
318 release, make sure this bug still exists in the developer
319 version of XML::LibXML. This version can be cloned
320 from its Git repository. For more information about that,
321 see:</para>
322
323 <para>https://github.com/shlomif/perl-XML-LibXML</para>
324
325 <para>Please consider all regression tests as correct. If
326 any test fails it is most certainly related to a
327 bug.</para>
328
329 <para>If you find documentation bugs, please fix them in
330 the libxml.dbk file, stored in the docs directory.</para>
331 </sect1>
332
333 <sect1>
334 <title>Known Issues</title>
335
336 <para>The push-parser implementation causes memory leaks.</para>
337 </sect1>
338 </chapter>
339
340 <chapter id="LICENSE">
341 <title>License</title>
342
343 <titleabbrev>LICENSE</titleabbrev>
344
345 <para>This is free software, you may use it and distribute it under the same terms as Perl itself.</para>
346
347 <para>Copyright 2001-2003 AxKit.com Ltd., 2002-2006 Christian Glahn, 2006-2009 Petr Pajas</para>
348
349 <sect1>
350 <title>Disclaimer</title>
351
352 <para>THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL
353 BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE
354 IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A
355 PARTICULAR PURPOSE.</para>
356 </sect1>
357 </chapter>
358
359 <chapter id="XML-LibXML">
360 <title>Perl Binding for libxml2</title>
361
362 <titleabbrev>XML::LibXML</titleabbrev>
363
364 <sect1>
365 <title>Synopsis</title>
366
367 <programlisting>use XML::LibXML;
368 my $dom = XML::LibXML-&gt;load_xml(string => &lt;&lt;'EOT');
369 &lt;some-xml/&gt;
370 EOT</programlisting>
371 </sect1>
372
373 <sect1>
374 <title>Description</title>
375
376 <para>This module is an interface to libxml2, providing
377 XML and HTML parsers with DOM, SAX and XMLReader interfaces,
378 a large subset of DOM Layer 3 interface and
379 a XML::XPath-like interface to XPath API of libxml2.
380 The module is split into several packages which are not described in this section;
381 unless stated otherwise, you only need to <literal>use XML::LibXML;</literal>
382 in your programs.</para>
383
384 <para>For further information, please check the following documentation:</para>
385
386 <variablelist>
387 <varlistentry>
388 <term><xref linkend="XML-LibXML-Parser"/></term>
389
390 <listitem>
391 <para>Parsing XML files with XML::LibXML</para>
392 </listitem>
393 </varlistentry>
394
395 <varlistentry>
396 <term><xref linkend="XML-LibXML-DOM"/></term>
397
398 <listitem>
399 <para>XML::LibXML Document Object Model (DOM) Implementation</para>
400 </listitem>
401 </varlistentry>
402
403 <varlistentry>
404 <term><xref linkend="XML-LibXML-SAX"/></term>
405
406 <listitem>
407 <para>XML::LibXML direct SAX parser</para>
408 </listitem>
409 </varlistentry>
410
411 <varlistentry>
412 <term><xref linkend="XML-LibXML-Reader"/></term>
413
414 <listitem>
415 <para>Reading XML with a pull-parser</para>
416 </listitem>
417 </varlistentry>
418
419 <varlistentry>
420 <term><xref linkend="XML-LibXML-Dtd"/></term>
421
422 <listitem>
423 <para>XML::LibXML frontend for DTD validation</para>
424 </listitem>
425 </varlistentry>
426
427 <varlistentry>
428 <term><xref linkend="XML-LibXML-RelaxNG"/></term>
429
430 <listitem>
431 <para>XML::LibXML frontend for RelaxNG schema validation</para>
432 </listitem>
433 </varlistentry>
434
435 <varlistentry>
436 <term><xref linkend="XML-LibXML-Schema"/></term>
437
438 <listitem>
439 <para>XML::LibXML frontend for W3C Schema schema validation</para>
440 </listitem>
441 </varlistentry>
442
443 <varlistentry>
444 <term><xref linkend="XML-LibXML-XPathContext"/></term>
445 <listitem>
446 <para>API for evaluating XPath expressions with enhanced support
447 for the evaluation context</para>
448 </listitem>
449 </varlistentry>
450
451 <varlistentry>
452 <term><xref linkend="XML-LibXML-InputCallback"/></term>
453
454 <listitem>
455 <para>Implementing custom URI Resolver and input callbacks</para>
456 </listitem>
457 </varlistentry>
458
459 <varlistentry>
460 <term><xref linkend="XML-LibXML-Common"/></term>
461 <listitem>
462 <para>Common functions for XML::LibXML related Classes</para>
463 </listitem>
464 </varlistentry>
465 </variablelist>
466 <para>The nodes in the Document Object Model (DOM) are represented by the following classes
467 (most of which "inherit" from <xref linkend="XML-LibXML-Node"/>):</para>
468 <variablelist>
469 <varlistentry>
470 <term><xref linkend="XML-LibXML-Document"/></term>
471
472 <listitem>
473 <para>XML::LibXML class for DOM document nodes</para>
474 </listitem>
475 </varlistentry>
476
477 <varlistentry>
478 <term><xref linkend="XML-LibXML-Node"/></term>
479
480 <listitem>
481 <para>Abstract base class for XML::LibXML DOM nodes</para>
482 </listitem>
483 </varlistentry>
484
485 <varlistentry>
486 <term><xref linkend="XML-LibXML-Element"/></term>
487
488 <listitem>
489 <para>XML::LibXML class for DOM element nodes</para>
490 </listitem>
491 </varlistentry>
492
493 <varlistentry>
494 <term><xref linkend="XML-LibXML-Text"/></term>
495
496 <listitem>
497 <para>XML::LibXML class for DOM text nodes</para>
498 </listitem>
499 </varlistentry>
500
501 <varlistentry>
502 <term><xref linkend="XML-LibXML-Comment"/></term>
503
504 <listitem>
505 <para>XML::LibXML class for comment DOM nodes</para>
506 </listitem>
507 </varlistentry>
508
509 <varlistentry>
510 <term><xref linkend="XML-LibXML-CDATASection"/></term>
511
512 <listitem>
513 <para>XML::LibXML class for DOM CDATA sections</para>
514 </listitem>
515 </varlistentry>
516
517 <varlistentry>
518 <term><xref linkend="XML-LibXML-Attr"/></term>
519
520 <listitem>
521 <para>XML::LibXML DOM attribute class</para>
522 </listitem>
523 </varlistentry>
524
525 <varlistentry>
526 <term><xref linkend="XML-LibXML-DocumentFragment"/></term>
527
528 <listitem>
529 <para>XML::LibXML's DOM L2 Document Fragment implementation</para>
530 </listitem>
531 </varlistentry>
532
533 <varlistentry>
534 <term><xref linkend="XML-LibXML-Namespace"/></term>
535
536 <listitem>
537 <para>XML::LibXML DOM namespace nodes</para>
538 </listitem>
539 </varlistentry>
540
541 <varlistentry>
542 <term><xref linkend="XML-LibXML-PI"/></term>
543
544 <listitem>
545 <para>XML::LibXML DOM processing instruction nodes</para>
546 </listitem>
547 </varlistentry>
548
549 </variablelist>
550 </sect1>
551 <sect1>
552 <title>Encodings support in XML::LibXML</title>
553 <para>Recall that since version 5.6.1, Perl distinguishes between
554 character strings (internally encoded in UTF-8) and so
555 called binary data and, accordingly, applies either
556 character or byte semantics to them. A scalar
557 representing a character string is distinguished from
558 a byte string by special flag (UTF8). Please refer to <emphasis>perlunicode</emphasis> for details.
559 </para>
560 <para>
561 XML::LibXML's API is designed to deal with many
562 encodings of XML documents completely transparently, so
563 that the application using XML::LibXML can be completely
564 ignorant about the encoding of the XML documents it works with.
565 On the other hand, functions like <function>XML::LibXML::Document-&gt;setEncoding</function>
566 give the user control over the document encoding.
567 </para>
568 <para>
569 To ensure the aforementioned transparency and
570 uniformity, most functions of XML::LibXML that work with
571 in-memory trees accept and return data as character
572 strings (i.e. UTF-8 encoded with the UTF8 flag on)
573 regardless of the original document encoding; however,
574 the functions related to I/O operations (i.e. parsing
575 and saving) operate with binary data (in the original
576 document encoding) obeying the encoding declaration of
577 the XML documents.</para>
578 <para>Below we summarize basic rules and principles
579 regarding encoding:
580 </para>
581 <orderedlist>
582 <listitem><para>Do NOT apply any encoding-related PerlIO layers
583 (<literal>:utf8</literal> or <literal>:encoding(...)</literal>)
584 to file handles that are an input for the parses
585 or an output for a serializer of (full) XML documents.
586 This is because the conversion of the data to/from the internal character representation
587 is provided by libxml2 itself which must be able to enforce the encoding
588 specified by the <literal>&lt;?xml version="1.0" encoding="..."?&gt;</literal>
589 declaration. Here is an example to follow:
590 <programlisting>use XML::LibXML;
591 # load
592 open my $fh, '&lt;', 'file.xml';
593 binmode $fh; # drop all PerlIO layers possibly created by a <literal>use open</literal> pragma
594 $doc = XML::LibXML-&gt;load_xml(IO => $fh);
595
596 # save
597 open my $out, '&gt;', 'out.xml';
598 binmode $out; # as above
599 $doc-&gt;toFH($out);
600 # or
601 print {$out} $doc-&gt;toString();</programlisting>
602 </para>
603 </listitem>
604 <listitem>
605 <para>All functions working with DOM accept and return
606 character strings (UTF-8 encoded with UTF8 flag on). E.g.
607 <programlisting><![CDATA[
608 my $doc = XML::LibXML::Document->new('1.0',$some_encoding);
609 my $element = $doc->createElement($name);
610 $element->appendText($text);
611 $xml_fragment = $element->toString(); # returns a character string
612 $xml_document = $doc->toString(); # returns a byte string
613 ]]>
614 </programlisting>
615 where
616 <literal>$some_encoding</literal> is the document encoding
617 that will be used when saving the document,
618 and <literal>$name</literal> and <literal>$text</literal>
619 contain character strings (UTF-8 encoded with UTF8 flag on).
620 Note that the method <function>toString</function>
621 returns XML as a character string if applied to
622 other node than the Document node and
623 a byte string containing the appropriate
624 <programlisting>&lt;?xml version="1.0" encoding="..."?&gt;</programlisting>
625 declaration if applied to a <xref linkend="XML-LibXML-Document"/>.
626 </para>
627 </listitem>
628 <listitem>
629 <para>DOM methods also accept binary strings in the original encoding of the
630 document to which the node belongs (UTF-8 is assumed if the node is not
631 attached to any document). Exploiting this feature is NOT RECOMMENDED
632 since it is considered bad practice.
633 </para>
634 <programlisting><![CDATA[
635 my $doc = XML::LibXML::Document->new('1.0','iso-8859-2');
636 my $text = $doc->createTextNode($some_latin2_encoded_byte_string);
637 # WORKS, BUT NOT RECOMMENDED!
638 ]]>
639 </programlisting>
640 </listitem>
641 </orderedlist>
642 <para><emphasis>NOTE:</emphasis> libxml2 support for many
643 encodings is based on the iconv library. The actual list
644 of supported encodings may vary from platform to
645 platform. To test if your platform works correctly with
646 your language encoding, build a simple document in the
647 particular encoding and try to parse it with XML::LibXML
648 to see if the parser produces any errors. Occasional
649 crashes were reported on rare platforms that ship with a broken
650 version of iconv.</para>
651 </sect1>
652 <sect1>
653 <title>Thread Support</title>
654 <para>
655 XML::LibXML since 1.67 partially supports Perl threads
656 in Perl >= 5.8.8. XML::LibXML can be used with threads
657 in two ways:
658 </para>
659 <para>
660 By default, all
661 XML::LibXML classes use CLONE_SKIP class method
662 to prevent Perl from copying XML::LibXML::* objects
663 when a new thread is spawn.
664 In this mode, all XML::LibXML::* objects are thread specific.
665 This is the safest way
666 to work with XML::LibXML in threads.
667 </para>
668 <para>
669 Alternatively, one may use
670 </para>
671 <programlisting>use threads;
672 use XML::LibXML qw(:threads_shared);</programlisting>
673 <para>
674 to indicate, that
675 all XML::LibXML node and parser objects
676 should be shared between the main thread
677 and any thread spawn from there.
678 For example, in
679 </para>
680 <programlisting>my $doc = XML::LibXML->load_xml(location => $filename);
681 my $thr = threads->new(sub{
682 # code working with $doc
683 1;
684 });
685 $thr->join;
686 </programlisting>
687 <para>
688 the variable <literal>$doc</literal>
689 refers to the exact same XML::LibXML::Document
690 in the spawned thread as in the main thread.
691 </para>
692 <para>
693 Without using mutex locks,
694 parallel threads may read the same document
695 (i.e. any node that belongs to the document),
696 parse files, and modify different documents.
697 </para>
698 <para>
699 However, if there is a chance that
700 some of the threads will attempt to modify a document
701 (or even create
702 new nodes based on that document,
703 e.g. with <literal>$doc->createElement</literal>)
704 that other threads may be reading at the same time,
705 the user is responsible for creating a mutex lock
706 and using it in <emphasis>both</emphasis>
707 in the thread that modifies and
708 the thread that reads:
709 </para>
710 <programlisting>my $doc = XML::LibXML->load_xml(location => $filename);
711 my $mutex : shared;
712 my $thr = threads->new(sub{
713 lock $mutex;
714 my $el = $doc->createElement('foo');
715 # ...
716 1;
717 });
718 {
719 lock $mutex;
720 my $root = $doc->documentElement;
721 say $root->name;
722 }
723 $thr->join;
724 </programlisting>
725 <para>Note that libxml2 uses dictionaries to store short strings and
726 these dictionaries are kept on a document node. Without mutex locks, it
727 could happen in the previous example that the thread modifies the
728 dictionary while other threads attempt to read from it, which could
729 easily lead to a crash.</para>
730 </sect1>
731 <sect1>
732 <title>Version Information</title>
733
734 <para>Sometimes it is useful to figure out, for which
735 version XML::LibXML was compiled for. In most cases this
736 is for debugging or to check if a given installation meets
737 all functionality for the package. The functions
738 XML::LibXML::LIBXML_DOTTED_VERSION and
739 XML::LibXML::LIBXML_VERSION provide this version
740 information. Both functions simply pass through the values
741 of the similar named macros of libxml2.
742 Similarly, XML::LibXML::LIBXML_RUNTIME_VERSION returns
743 the version of the (usually dynamically) linked libxml2.
744 </para>
745
746 <variablelist>
747 <varlistentry>
748 <term>XML::LibXML::LIBXML_DOTTED_VERSION</term>
749
750 <listitem>
751 <funcsynopsis>
752 <funcsynopsisinfo>$Version_String = XML::LibXML::LIBXML_DOTTED_VERSION;</funcsynopsisinfo>
753 </funcsynopsis>
754
755 <para>Returns the version string of the
756 libxml2 version XML::LibXML was compiled
757 for. This will be "2.6.2" for "libxml2
758 2.6.2".</para>
759 </listitem>
760 </varlistentry>
761
762 <varlistentry>
763 <term>XML::LibXML::LIBXML_VERSION</term>
764
765 <listitem>
766 <funcsynopsis>
767 <funcsynopsisinfo>$Version_ID = XML::LibXML::LIBXML_VERSION;</funcsynopsisinfo>
768 </funcsynopsis>
769
770 <para>Returns the version id of the libxml2
771 version XML::LibXML was compiled for. This
772 will be "20602" for "libxml2 2.6.2". Don't mix
773 this version id with
774 $XML::LibXML::VERSION. The latter contains the
775 version of XML::LibXML itself while the first
776 contains the version of libxml2 XML::LibXML
777 was compiled for.</para>
778 </listitem>
779 </varlistentry>
780 <varlistentry>
781 <term>XML::LibXML::LIBXML_RUNTIME_VERSION</term>
782
783 <listitem>
784 <funcsynopsis>
785 <funcsynopsisinfo>$DLL_Version = XML::LibXML::LIBXML_RUNTIME_VERSION;</funcsynopsisinfo>
786 </funcsynopsis>
787
788 <para>Returns a version string of the libxml2
789 which is (usually dynamically) linked by
790 XML::LibXML. This will be "20602" for libxml2
791 released as "2.6.2" and something like
792 "20602-CVS2032" for a CVS build of
793 libxml2.</para>
794 <para>XML::LibXML issues a warning if the version
795 of libxml2 dynamically linked to it is less than the version of libxml2
796 which it was compiled against.
797 </para>
798 </listitem>
799 </varlistentry>
800
801 </variablelist>
802 </sect1>
803 <sect1>
804 <title>EXPORTS</title>
805 <para>
806 By default the module exports all constants and functions
807 listed in the :all tag, described below.
808 </para>
809 </sect1>
810 <sect1>
811 <title>EXPORT TAGS</title>
812 <variablelist>
813 <varlistentry>
814 <term><literal>:all</literal></term>
815 <listitem>
816 <para>Includes the tags <literal>:libxml</literal>, <literal>:encoding</literal>, and
817 <literal>:ns</literal> described below.</para>
818 </listitem>
819 </varlistentry>
820 <varlistentry>
821 <term><literal>:libxml</literal></term>
822 <listitem>
823 <para>Exports integer constants for DOM node types.</para>
824 <programlisting>XML_ELEMENT_NODE =&gt; 1
825 XML_ATTRIBUTE_NODE =&gt; 2
826 XML_TEXT_NODE =&gt; 3
827 XML_CDATA_SECTION_NODE =&gt; 4
828 XML_ENTITY_REF_NODE =&gt; 5
829 XML_ENTITY_NODE =&gt; 6
830 XML_PI_NODE =&gt; 7
831 XML_COMMENT_NODE =&gt; 8
832 XML_DOCUMENT_NODE =&gt; 9
833 XML_DOCUMENT_TYPE_NODE =&gt; 10
834 XML_DOCUMENT_FRAG_NODE =&gt; 11
835 XML_NOTATION_NODE =&gt; 12
836 XML_HTML_DOCUMENT_NODE =&gt; 13
837 XML_DTD_NODE =&gt; 14
838 XML_ELEMENT_DECL =&gt; 15
839 XML_ATTRIBUTE_DECL =&gt; 16
840 XML_ENTITY_DECL =&gt; 17
841 XML_NAMESPACE_DECL =&gt; 18
842 XML_XINCLUDE_START =&gt; 19
843 XML_XINCLUDE_END =&gt; 20</programlisting>
844 </listitem>
845 </varlistentry>
846 <varlistentry>
847 <term><literal>:encoding</literal></term>
848 <listitem>
849 <para>Exports two encoding conversion functions from XML::LibXML::Common.</para>
850 <programlisting>
851 encodeToUTF8()
852 decodeFromUTF8()
853 </programlisting>
854 </listitem>
855 </varlistentry>
856 <varlistentry>
857 <term><literal>:ns</literal></term>
858 <listitem>
859 <para>Exports two convenience constants: the implicit namespace of the
860 reserved <literal>xml:</literal> prefix,
861 and the implicit namespace for the reserved <literal>xmlns:</literal> prefix.</para>
862 <programlisting>
863 XML_XML_NS =&gt; 'http://www.w3.org/XML/1998/namespace'
864 XML_XMLNS_NS =&gt; 'http://www.w3.org/2000/xmlns/'
865 </programlisting>
866 </listitem>
867 </varlistentry>
868 </variablelist>
869 </sect1>
870 <sect1>
871 <title>Related Modules</title>
872
873 <para>The modules described in this section are not part of the XML::LibXML package itself. As they support some additional features, they are
874 mentioned here.</para>
875
876 <variablelist>
877 <varlistentry>
878 <term><olink targetdoc="XML::LibXSLT">XML::LibXSLT</olink></term>
879
880 <listitem>
881 <para>XSLT 1.0 Processor using libxslt and XML::LibXML</para>
882 </listitem>
883 </varlistentry>
884
885 <varlistentry>
886 <term><olink targetdoc="XML::LibXML::Iterator">XML::LibXML::Iterator</olink></term>
887
888 <listitem>
889 <para>XML::LibXML Implementation of the DOM Traversal Specification</para>
890 </listitem>
891 </varlistentry>
892
893 <varlistentry>
894 <term><olink targetdoc="XML::CompactTree::XS">XML::CompactTree::XS</olink></term>
895
896 <listitem>
897 <para>Uses XML::LibXML::Reader to very efficiently to parse XML document
898 or element into native Perl data structures, which are less flexible but
899 significantly faster to process then DOM.</para>
900 </listitem>
901 </varlistentry>
902
903 </variablelist>
904 </sect1>
905
906 <sect1>
907 <title>XML::LibXML and XML::GDOME</title>
908
909 <para>Note: <emphasis>THE FUNCTIONS DESCRIBED HERE ARE STILL EXPERIMENTAL</emphasis></para>
910
911 <para>Although both modules make use of libxml2's XML capabilities, the DOM implementation of both modules are not compatible. But still it is
912 possible to exchange nodes from one DOM to the other. The concept of this exchange is pretty similar to the function cloneNode(): The particular
913 node is copied on the low-level to the opposite DOM implementation.</para>
914
915 <para>Since the DOM implementations cannot coexist within one document, one is forced to copy each node that should be used. Because you are always
916 keeping two nodes this may cause quite an impact on a machines memory usage.</para>
917
918 <para>XML::LibXML provides two functions to export or import GDOME nodes: import_GDOME() and export_GDOME(). Both function have two parameters: the
919 node and a flag for recursive import. The flag works as in cloneNode().</para>
920
921 <para>The two functions allow one to export and import XML::GDOME nodes explicitly, however, XML::LibXML also allows the transparent import of
922 XML::GDOME nodes in functions such as appendChild(), insertAfter() and so on. While native nodes are automatically adopted in most functions
923 XML::GDOME nodes are always cloned in advance. Thus if the original node is modified after the operation, the node in the XML::LibXML document will
924 not have this information.</para>
925
926 <variablelist>
927 <varlistentry>
928 <term>import_GDOME</term>
929
930 <listitem>
931 <funcsynopsis>
932 <funcsynopsisinfo>$libxmlnode = XML::LibXML-&gt;import_GDOME( $node, $deep );</funcsynopsisinfo>
933 </funcsynopsis>
934
935 <para>This clones an XML::GDOME node to an XML::LibXML node explicitly.</para>
936 </listitem>
937 </varlistentry>
938
939 <varlistentry>
940 <term>export_GDOME</term>
941
942 <listitem>
943 <funcsynopsis>
944 <funcsynopsisinfo>$gdomenode = XML::LibXML-&gt;export_GDOME( $node, $deep );</funcsynopsisinfo>
945 </funcsynopsis>
946
947 <para>Allows one to clone an XML::LibXML node into an
948 XML::GDOME node.</para>
949
950 </listitem>
951 </varlistentry>
952 </variablelist>
953 </sect1>
954 <sect1>
955 <title>CONTACTS</title>
956
957 <para>For bug reports, please use the CPAN request tracker on http://rt.cpan.org/NoAuth/Bugs.html?Dist=XML-LibXML</para>
958 <para>For suggestions etc., and other issues
959 related to XML::LibXML you may use the perl XML mailing list
960 (<email>[email protected]</email>),
961 where most XML-related Perl modules are discussed.
962 In case of problems you should check the archives of that
963 list first. Many problems are already discussed there. You
964 can find the list's archives and subscription options at
965 <ulink url="http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/perl-xml">http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/perl-xml</ulink>.
966 </para>
967 </sect1>
968 </chapter>
969
970 <chapter id="XML-LibXML-Parser">
971 <title>Parsing XML Data with XML::LibXML</title>
972
973 <titleabbrev>XML::LibXML::Parser</titleabbrev>
974
975 <sect1>
976 <title>Synopsis</title>
977
978 <programlisting>use XML::LibXML '1.70';
979 <!--
980 my $dom = XML::LibXML-&gt;load_xml(
981 location => $file_or_url,
982 # or string => $xml_string,
983 # or IO => $perl_file_handle,
984 # ...parser options...
985 );
986
987 my $html_dom = XML::LibXML-&gt;load_html(
988 location => $file_or_url,
989 # or string => $html_string,
990 # or IO => $perl_file_handle,
991 # ...parser options...
992 );
993
994 my $parser = XML::LibXML-&gt;new(
995 # ... parser options ...
996 );
997
998 my $doc = $parser-&gt;parse_string(&lt;&lt;'EOT');
999 &lt;some-xml/&gt;
1000 EOT
1001 my $fdoc = $parser-&gt;parse_file( $xmlfile );
1002
1003 my $fhdoc = $parser-&gt;parse_fh( $xmlstream );
1004
1005 my $fragment = $parser-&gt;parse_xml_chunk( $xml_wb_chunk );
1006 --></programlisting>
1007 </sect1>
1008
1009 <sect1>
1010 <title>Parsing</title>
1011
1012 <para>An XML document is read into a data structure such as a DOM tree by a piece of software, called a parser. XML::LibXML currently provides four
1013 different parser interfaces:</para>
1014
1015 <itemizedlist>
1016 <listitem>
1017 <para>A DOM Pull-Parser</para>
1018 </listitem>
1019
1020 <listitem>
1021 <para>A DOM Push-Parser</para>
1022 </listitem>
1023
1024 <listitem>
1025 <para>A SAX Parser</para>
1026 </listitem>
1027
1028 <listitem>
1029 <para>A DOM based SAX Parser.</para>
1030 </listitem>
1031 </itemizedlist>
1032
1033 <sect2>
1034 <title>Creating a Parser Instance</title>
1035
1036 <para>XML::LibXML provides an OO interface to the libxml2 parser functions. Thus you have to create a parser instance before you can parse any
1037 XML data.</para>
1038
1039 <variablelist>
1040 <varlistentry>
1041 <term>new</term>
1042
1043 <listitem>
1044 <funcsynopsis role="synopsis">
1045 <funcsynopsisinfo># Parser constructor</funcsynopsisinfo>
1046 </funcsynopsis>
1047 <funcsynopsis>
1048 <funcsynopsisinfo>
1049 $parser = XML::LibXML-&gt;new();
1050 $parser = XML::LibXML-&gt;new(option=>value, ...);
1051 $parser = XML::LibXML-&gt;new({option=>value, ...});</funcsynopsisinfo>
1052 </funcsynopsis>
1053
1054 <para>Create a new XML and HTML parser instance.
1055 Each parser instance holds default
1056 values for various parser options.
1057 Optionally,
1058 one can pass a hash reference or
1059 a list of option => value pairs to
1060 set a different default set of options.
1061 Unless specified otherwise, the options
1062 <literal>load_ext_dtd</literal>, and
1063 <literal>expand_entities</literal> are set to 1.
1064 See <xref linkend="parser-options"/> for a list of libxml2 parser's options.
1065 </para>
1066 </listitem>
1067 </varlistentry>
1068 </variablelist>
1069 </sect2>
1070
1071 <sect2>
1072 <title>DOM Parser</title>
1073
1074 <para>One of the common parser interfaces of XML::LibXML is the DOM parser. This parser reads XML data into a DOM like data structure, so each
1075 tag can get accessed and transformed.</para>
1076
1077 <para>XML::LibXML's DOM parser is not only capable to parse XML data, but also (strict) HTML files. There are three ways to parse
1078 documents - as a string, as a Perl filehandle, or as a filename/URL. The return value from each is a <xref linkend="XML-LibXML-Document"/> object, which is a DOM
1079 object.</para>
1080
1081 <para>All of the functions listed below will throw an exception if the document is invalid. To prevent this causing your program exiting, wrap
1082 the call in an eval{} block</para>
1083
1084 <variablelist>
1085 <varlistentry>
1086 <term>load_xml</term>
1087 <listitem>
1088 <funcsynopsis role="synopsis">
1089 <funcsynopsisinfo>
1090 # Parsing XML</funcsynopsisinfo>
1091 </funcsynopsis>
1092 <funcsynopsis>
1093 <funcsynopsisinfo>
1094 $dom = XML::LibXML-&gt;load_xml(
1095 location => $file_or_url
1096 # parser options ...
1097 );
1098 $dom = XML::LibXML-&gt;load_xml(
1099 string => $xml_string
1100 # parser options ...
1101 );
1102 $dom = XML::LibXML-&gt;load_xml(
1103 string => (\$xml_string)
1104 # parser options ...
1105 );
1106 $dom = XML::LibXML-&gt;load_xml({
1107 IO => $perl_file_handle
1108 # parser options ...
1109 );
1110 $dom = $parser-&gt;load_xml(...);
1111 </funcsynopsisinfo>
1112 </funcsynopsis>
1113 <para>This function is available since XML::LibXML 1.70. It provides easy to use interface to the XML parser that parses
1114 given file (or URL), string, or input stream
1115 to a DOM tree. The arguments
1116 can be passed in a HASH reference
1117 or as name => value pairs.
1118 The function can be called
1119 as a class method or an object method.
1120 In both cases it internally creates a new
1121 parser instance passing
1122 the specified parser options;
1123 if called as an object method,
1124 it clones the original parser (preserving
1125 its settings) and additionally applies
1126 the specified options to the new parser.
1127 See the constructor <function>new</function>
1128 and <xref linkend="parser-options"/>
1129 for more information.
1130 </para>
1131 </listitem>
1132 </varlistentry>
1133 <varlistentry>
1134 <term>load_html</term>
1135 <listitem>
1136 <funcsynopsis role="synopsis">
1137 <funcsynopsisinfo># Parsing HTML</funcsynopsisinfo>
1138 </funcsynopsis>
1139 <funcsynopsis>
1140 <funcsynopsisinfo>
1141 $dom = XML::LibXML-&gt;load_html(...);
1142 $dom = $parser-&gt;load_html(...);
1143 </funcsynopsisinfo>
1144 </funcsynopsis>
1145 <para>This function is available since XML::LibXML 1.70. It has the same usage as <function>load_xml</function>,
1146 providing interface to the HTML parser.
1147 See <function>load_xml</function> for more information.
1148 </para>
1149 </listitem>
1150 </varlistentry>
1151 </variablelist>
1152
1153 <para>Parsing HTML may cause problems, especially if
1154 the ampersand ('&amp;') is used. This is a common
1155 problem if HTML code is parsed that contains links to
1156 CGI-scripts. Such links cause the parser to throw
1157 errors. In such cases libxml2 still parses the entire
1158 document as there was no error, but the error causes
1159 XML::LibXML to stop the parsing process. However, the
1160 document is not lost. Such HTML documents should be
1161 parsed using the <emphasis>recover</emphasis> flag. By
1162 default recovering is deactivated.</para>
1163
1164 <para>The functions described above are implemented to
1165 parse well formed documents. In some cases a program
1166 gets well balanced XML instead of well formed
1167 documents (e.g. an XML fragment from a database). With
1168 XML::LibXML it is not required to wrap such fragments
1169 in the code, because XML::LibXML is capable even to
1170 parse well balanced XML fragments.</para>
1171
1172 <variablelist>
1173 <varlistentry>
1174 <term>parse_balanced_chunk</term>
1175 <listitem>
1176 <funcsynopsis role="synopsis">
1177 <funcsynopsisinfo># Parsing well-balanced XML chunks
1178 </funcsynopsisinfo>
1179 </funcsynopsis>
1180 <funcsynopsis>
1181 <funcsynopsisinfo>$fragment = $parser-&gt;parse_balanced_chunk( $wbxmlstring, $encoding );</funcsynopsisinfo>
1182 </funcsynopsis>
1183
1184 <para>This function parses a well balanced XML string into a <xref linkend="XML-LibXML-DocumentFragment"/>. The first arguments contains the input string, the optional second argument can be used to specify character encoding of the input (UTF-8 is assumed by default).</para>
1185 </listitem>
1186 </varlistentry>
1187
1188 <varlistentry>
1189 <term>parse_xml_chunk</term>
1190
1191 <listitem>
1192 <para>This is the old name of parse_balanced_chunk(). Because it may causes confusion with the push parser interface, this function
1193 should not be used anymore.</para>
1194 </listitem>
1195 </varlistentry>
1196 </variablelist>
1197
1198 <para>By default XML::LibXML does not process XInclude tags
1199 within an XML Document (see options section below).
1200 XML::LibXML allows one to post-process a document to expand
1201 XInclude tags.</para>
1202
1203 <variablelist>
1204 <varlistentry>
1205 <term>process_xincludes</term>
1206 <listitem>
1207 <funcsynopsis role="synopsis">
1208 <funcsynopsisinfo>
1209 # Processing XInclude
1210 </funcsynopsisinfo>
1211 </funcsynopsis>
1212 <funcsynopsis>
1213 <funcsynopsisinfo>$parser-&gt;process_xincludes( $doc );</funcsynopsisinfo>
1214 </funcsynopsis>
1215
1216 <para>After a document is parsed into a DOM structure, you may want to expand the documents XInclude tags. This function processes
1217 the given document structure and expands all XInclude tags (or throws an error) by using the flags and callbacks of the given parser
1218 instance.</para>
1219
1220 <para>Note that the resulting Tree contains some extra nodes (of type XML_XINCLUDE_START and XML_XINCLUDE_END) after successfully
1221 processing the document. These nodes indicate where data was included into the original tree. if the document is serialized, these
1222 extra nodes will not show up.</para>
1223
1224 <para>Remember: A Document with processed XIncludes differs from the original document after serialization, because the original
1225 XInclude tags will not get restored!</para>
1226
1227 <para>If the parser flag "expand_xincludes" is set to 1, you need not to post process the parsed document.</para>
1228 </listitem>
1229 </varlistentry>
1230
1231 <varlistentry>
1232 <term>processXIncludes</term>
1233
1234 <listitem>
1235 <funcsynopsis>
1236 <funcsynopsisinfo>$parser-&gt;processXIncludes( $doc );</funcsynopsisinfo>
1237 </funcsynopsis>
1238
1239 <para>This is an alias to process_xincludes, but through a JAVA like function name.</para>
1240 </listitem>
1241 </varlistentry>
1242
1243 <varlistentry>
1244 <term>parse_file</term>
1245
1246 <listitem>
1247 <funcsynopsis role="synopsis">
1248 <funcsynopsisinfo>
1249 # Old-style parser interfaces
1250 </funcsynopsisinfo>
1251 </funcsynopsis>
1252 <funcsynopsis>
1253 <funcsynopsisinfo>$doc = $parser-&gt;parse_file( $xmlfilename );</funcsynopsisinfo>
1254 </funcsynopsis>
1255
1256 <para>This function parses an XML document from a file or network;
1257 $xmlfilename can be either a filename or an URL.
1258 Note that for parsing files, this function is the fastest choice,
1259 about 6-8 times faster then parse_fh().
1260 </para>
1261 </listitem>
1262 </varlistentry>
1263
1264 <varlistentry>
1265 <term>parse_fh</term>
1266
1267 <listitem>
1268 <funcsynopsis>
1269 <funcsynopsisinfo>$doc = $parser-&gt;parse_fh( $io_fh );</funcsynopsisinfo>
1270 </funcsynopsis>
1271
1272 <para>parse_fh() parses a IOREF or a subclass of IO::Handle.</para>
1273
1274 <para>Because the data comes from an open handle, libxml2's parser does not know about the base URI of the document. To set the
1275 base URI one should use parse_fh() as follows:</para>
1276
1277 <programlisting>my $doc = $parser-&gt;parse_fh( $io_fh, $baseuri );</programlisting>
1278 </listitem>
1279 </varlistentry>
1280
1281 <varlistentry>
1282 <term>parse_string</term>
1283
1284 <listitem>
1285 <funcsynopsis>
1286 <funcsynopsisinfo>$doc = $parser-&gt;parse_string( $xmlstring);</funcsynopsisinfo>
1287 </funcsynopsis>
1288
1289 <para>This function is similar to parse_fh(), but it parses an XML document that is available as a single string in memory, or alternatively as a reference to a scalar containing a string. Again,
1290 you can pass an optional base URI to the function.</para>
1291
1292 <programlisting>my $doc = $parser-&gt;parse_string( $xmlstring, $baseuri );
1293 my $doc = $parser-&gt;parse_string(\$xmlstring, $baseuri);
1294 </programlisting>
1295 </listitem>
1296 </varlistentry>
1297
1298 <varlistentry>
1299 <term>parse_html_file</term>
1300
1301 <listitem>
1302 <funcsynopsis>
1303 <funcsynopsisinfo>$doc = $parser-&gt;parse_html_file( $htmlfile, \%opts );</funcsynopsisinfo>
1304 </funcsynopsis>
1305
1306 <para>Similar to parse_file() but parses HTML (strict) documents;
1307 $htmlfile can be filename or URL.
1308 </para>
1309 <para>An optional second argument can be
1310 used to pass some options to the HTML
1311 parser as a HASH reference.
1312 See options labeled with HTML in <xref linkend="parser-options"/>.
1313 </para>
1314 </listitem>
1315 </varlistentry>
1316
1317 <varlistentry>
1318 <term>parse_html_fh</term>
1319
1320 <listitem>
1321 <funcsynopsis>
1322 <funcsynopsisinfo>$doc = $parser-&gt;parse_html_fh( $io_fh, \%opts );</funcsynopsisinfo>
1323 </funcsynopsis>
1324 <para>Similar to parse_fh() but parses HTML (strict) streams.</para>
1325 <para>
1326 An optional second argument can be used
1327 to pass some options to the HTML parser
1328 as a HASH reference.
1329 See options labeled with HTML in <xref linkend="parser-options"/>.
1330 </para>
1331 <para>
1332 Note: encoding option may
1333 not work correctly with this function
1334 in libxml2 &lt; 2.6.27 if the HTML file
1335 declares charset using a META tag.
1336 </para>
1337 </listitem>
1338 </varlistentry>
1339
1340 <varlistentry>
1341 <term>parse_html_string</term>
1342
1343 <listitem>
1344 <funcsynopsis>
1345 <funcsynopsisinfo>$doc = $parser-&gt;parse_html_string( $htmlstring, \%opts );</funcsynopsisinfo>
1346 </funcsynopsis>
1347 <para>Similar to parse_string() but parses HTML (strict) strings.</para>
1348 <para>An optional second argument can be used to pass some options to the
1349 HTML parser as a HASH reference.
1350 See options labeled with HTML in <xref linkend="parser-options"/>.
1351 </para>
1352 </listitem>
1353 </varlistentry>
1354 </variablelist>
1355 </sect2>
1356
1357 <sect2>
1358 <title>Push Parser</title>
1359
1360 <para>XML::LibXML provides a push parser interface. Rather than pulling the data from a given source the push parser waits for the data to be
1361 pushed into it.</para>
1362
1363 <para>This allows one to parse large documents without waiting for the parser to finish. The interface is especially useful if a program needs
1364 to pre-process the incoming pieces of XML (e.g. to detect document boundaries).</para>
1365
1366 <para>While XML::LibXML parse_*() functions force the data to be a well-formed XML, the push parser will take any arbitrary string that contains
1367 some XML data. The only requirement is that all the pushed strings are together a well formed document. With the push parser interface a
1368 program can interrupt the parsing process as required, where the parse_*() functions give not enough flexibility.</para>
1369
1370 <para>Different to the pull parser implemented in parse_fh() or parse_file(), the push parser is not able to find out about the documents end
1371 itself. Thus the calling program needs to indicate explicitly when the parsing is done.</para>
1372
1373 <para>In XML::LibXML this is done by a single function:</para>
1374
1375 <variablelist>
1376 <varlistentry>
1377 <term>parse_chunk</term>
1378
1379 <listitem>
1380 <funcsynopsis role="synopsis">
1381 <funcsynopsisinfo>
1382 # Push parser
1383 </funcsynopsisinfo>
1384 </funcsynopsis>
1385 <funcsynopsis>
1386 <funcsynopsisinfo>$parser-&gt;parse_chunk($string, $terminate);</funcsynopsisinfo>
1387 </funcsynopsis>
1388
1389 <para>parse_chunk() tries to parse a given chunk of data, which isn't necessarily well balanced data. The function takes two
1390 parameters: The chunk of data as a string and optional a termination flag. If the termination flag is set to a true value (e.g. 1),
1391 the parsing will be stopped and the resulting document will be returned as the following example describes:</para>
1392
1393 <programlisting>my $parser = XML::LibXML-&gt;new;
1394 for my $string ( "&lt;", "foo", ' bar="hello world"', "/&gt;") {
1395 $parser-&gt;parse_chunk( $string );
1396 }
1397 my $doc = $parser-&gt;parse_chunk("", 1); # terminate the parsing</programlisting>
1398 </listitem>
1399 </varlistentry>
1400 </variablelist>
1401
1402 <para>Internally XML::LibXML provides three functions that control the push parser process:</para>
1403
1404 <variablelist>
1405 <varlistentry>
1406 <term>init_push</term>
1407
1408 <listitem>
1409 <funcsynopsis>
1410 <funcsynopsisinfo>$parser-&gt;init_push();</funcsynopsisinfo>
1411 </funcsynopsis>
1412
1413 <para>Initializes the push parser.</para>
1414 </listitem>
1415 </varlistentry>
1416
1417 <varlistentry>
1418 <term>push</term>
1419
1420 <listitem>
1421 <funcsynopsis>
1422 <funcsynopsisinfo>$parser-&gt;push(@data);</funcsynopsisinfo>
1423 </funcsynopsis>
1424
1425 <para>This function pushes the data stored inside the array to libxml2's parser. Each entry in @data must be a normal scalar! This method can be called repeatedly.</para>
1426 </listitem>
1427 </varlistentry>
1428
1429 <varlistentry>
1430 <term>finish_push</term>
1431
1432 <listitem>
1433 <funcsynopsis>
1434 <funcsynopsisinfo>$doc = $parser-&gt;finish_push( $recover );</funcsynopsisinfo>
1435 </funcsynopsis>
1436
1437 <para>This function returns the result of the parsing process. If this function is called without a parameter it will complain about
1438 non well-formed documents. If $restore is 1, the push parser can be used to restore broken or non well formed (XML) documents as the
1439 following example shows:</para>
1440
1441 <programlisting>eval {
1442 $parser-&gt;push( "&lt;foo&gt;", "bar" );
1443 $doc = $parser-&gt;finish_push(); # will report broken XML
1444 };
1445 if ( $@ ) {
1446 # ...
1447 }</programlisting>
1448
1449 <para>This can be annoying if the closing tag is missed by accident. The following code will restore the document:</para>
1450
1451 <programlisting>eval {
1452 $parser-&gt;push( "&lt;foo&gt;", "bar" );
1453 $doc = $parser-&gt;finish_push(1); # will return the data parsed
1454 # unless an error happened
1455 };
1456
1457 print $doc-&gt;toString(); # returns "&lt;foo&gt;bar&lt;/foo&gt;"</programlisting>
1458
1459 <para>Of course finish_push() will return nothing if there was no data pushed to the parser before.</para>
1460 </listitem>
1461 </varlistentry>
1462 </variablelist>
1463 </sect2>
1464
1465 <sect2>
1466 <title>Pull Parser (Reader)</title>
1467 <para>XML::LibXML also provides a pull-parser interface similar to the XmlReader interface in .NET.
1468 This interface is almost streaming, and is usually faster and simpler to use than SAX.
1469 See <xref linkend="XML-LibXML-Reader"/>.</para>
1470 </sect2>
1471
1472 <sect2>
1473 <title>Direct SAX Parser</title>
1474 <para>XML::LibXML provides a direct SAX parser in the <xref linkend="XML-LibXML-SAX"/> module.</para>
1475 </sect2>
1476
1477 <sect2>
1478 <title>DOM based SAX Parser</title>
1479
1480 <para>XML::LibXML also provides a DOM based SAX parser. The SAX parser is defined in
1481 the module XML::LibXML::SAX::Parser. As it is not a stream based parser, it
1482 parses documents into a DOM and traverses the DOM tree instead.</para>
1483
1484 <para>The API of this parser is exactly the same as any other Perl SAX2 parser. See XML::SAX::Intro for details.</para>
1485
1486 <para>Aside from the regular parsing methods, you can access the DOM tree traverser directly, using the generate() method:</para>
1487
1488 <programlisting>my $doc = build_yourself_a_document();
1489 my $saxparser = $XML::LibXML::SAX::Parser-&gt;new( ... );
1490 $parser-&gt;generate( $doc );</programlisting>
1491
1492 <para>This is useful for serializing DOM trees, for example that you might have done prior processing on, or that you have as a result of XSLT
1493 processing.</para>
1494
1495 <para><emphasis>WARNING</emphasis></para>
1496
1497 <para>This is NOT a streaming SAX parser. As I said above, this parser reads the entire document into a DOM and serialises it. Some people
1498 couldn't read that in the paragraph above so I've added this warning. If you want a streaming SAX parser look at the <xref linkend="XML-LibXML-SAX"/> man page</para>
1499 </sect2>
1500 </sect1>
1501
1502 <sect1>
1503 <title>Serialization</title>
1504
1505 <para>XML::LibXML provides some functions to serialize nodes and documents. The serialization functions are described on the <xref linkend="XML-LibXML-Node"/>
1506 manpage or the <xref linkend="XML-LibXML-Document"/> manpage. XML::LibXML checks three global flags that alter the serialization process:</para>
1507
1508 <itemizedlist>
1509 <listitem>
1510 <para>skipXMLDeclaration</para>
1511 </listitem>
1512
1513 <listitem>
1514 <para>skipDTD</para>
1515 </listitem>
1516
1517 <listitem>
1518 <para>setTagCompression</para>
1519 </listitem>
1520 </itemizedlist>
1521
1522 <para>of that three functions only setTagCompression is available for all serialization functions.</para>
1523
1524 <para>Because XML::LibXML does these flags not itself, one has to define them locally as the following example shows:</para>
1525
1526 <programlisting>local $XML::LibXML::skipXMLDeclaration = 1;
1527 local $XML::LibXML::skipDTD = 1;
1528 local $XML::LibXML::setTagCompression = 1;</programlisting>
1529
1530 <para>If skipXMLDeclaration is defined and not '0', the XML declaration is omitted during serialization.</para>
1531
1532 <para>If skipDTD is defined and not '0', an existing DTD would not be serialized with the document.</para>
1533
1534 <para>If setTagCompression is defined and not '0' empty tags are displayed as open and closing tags rather than the shortcut. For example
1535 the empty tag <emphasis>foo</emphasis> will be rendered as <emphasis>&lt;foo&gt;&lt;/foo&gt;</emphasis> rather than <emphasis>&lt;foo/&gt;</emphasis>.</para>
1536 </sect1>
1537
1538 <sect1 id="parser-options">
1539 <title>Parser Options</title>
1540
1541 <para>Handling of libxml2 parser options has been unified and improved in XML::LibXML 1.70.
1542 You can now set default options for a particular parser instance by
1543 passing them to the constructor as <literal>XML::LibXML->new({name=>value, ...})</literal>
1544 or <literal>XML::LibXML->new(name=>value,...)</literal>.
1545 The options can be queried and changed using the following methods (pre-1.70 interfaces such as <function>$parser->load_ext_dtd(0)</function> also exist, see below):
1546 </para>
1547
1548 <variablelist>
1549 <varlistentry>
1550 <term>option_exists</term>
1551 <listitem>
1552 <funcsynopsis role="synopsis">
1553 <funcsynopsisinfo>
1554 # Set/query parser options
1555 </funcsynopsisinfo>
1556 </funcsynopsis>
1557 <funcsynopsis>
1558 <funcsynopsisinfo>$parser-&gt;option_exists($name);</funcsynopsisinfo>
1559 </funcsynopsis>
1560 <para>Returns 1 if the current XML::LibXML version supports
1561 the option <literal>$name</literal>, otherwise returns 0 (note that this does not necessarily mean that the option is supported
1562 by the underlying libxml2 library).</para>
1563 </listitem>
1564 </varlistentry>
1565 <varlistentry>
1566 <term>get_option</term>
1567 <listitem>
1568 <funcsynopsis>
1569 <funcsynopsisinfo>$parser-&gt;get_option($name);</funcsynopsisinfo>
1570 </funcsynopsis>
1571 <para>Returns the current value of the parser option <literal>$name</literal>.</para>
1572 </listitem>
1573 </varlistentry>
1574 <varlistentry>
1575 <term>set_option</term>
1576 <listitem>
1577 <funcsynopsis>
1578 <funcsynopsisinfo>$parser-&gt;set_option($name,$value);</funcsynopsisinfo>
1579 </funcsynopsis>
1580 <para>Sets option <literal>$name</literal> to value <literal>$value</literal>.</para>
1581 </listitem>
1582 </varlistentry>
1583 <varlistentry>
1584 <term>set_options</term>
1585 <listitem>
1586 <funcsynopsis>
1587 <funcsynopsisinfo>$parser-&gt;set_options({$name=>$value,...});</funcsynopsisinfo>
1588 </funcsynopsis>
1589 <para>Sets multiple parsing options at once.</para>
1590 </listitem>
1591 </varlistentry>
1592 </variablelist>
1593 <para>
1594 IMPORTANT NOTE: This documentation reflects the parser flags available in libxml2 2.7.3.
1595 Some options have no effect if an older version of libxml2 is used.
1596 </para>
1597 <para>Each of the flags listed below is labeled</para>
1598 <variablelist>
1599 <varlistentry>
1600 <term>/parser/</term>
1601 <listitem>
1602 <para>if it can be used with a <function>XML::LibXML</function>
1603 parser object (i.e. passed to <function>XML::LibXML->new</function>, <function>XML::LibXML->set_option</function>, etc.)
1604 </para>
1605 </listitem>
1606 </varlistentry>
1607 <varlistentry>
1608 <term>/html/</term>
1609 <listitem>
1610 <para>if it can be used passed to the <function>parse_html_*</function> methods</para>
1611 </listitem>
1612 </varlistentry>
1613 <varlistentry>
1614 <term>/reader/</term>
1615 <listitem>
1616 <para>if it can be used with the <function>XML::LibXML::Reader</function>.</para>
1617 </listitem>
1618 </varlistentry>
1619 </variablelist>
1620 <para>
1621 Unless specified otherwise, the default for boolean valued options is 0 (false).
1622 </para>
1623 <para>The available options are:</para>
1624 <variablelist>
1625 <varlistentry>
1626 <term>URI</term>
1627 <listitem>
1628 <para>/parser, html, reader/</para>
1629 <para>In case of parsing strings or file handles, XML::LibXML doesn't know about the base uri of the document. To make relative
1630 references such as XIncludes work, one has to set a base URI, that is then used for the parsed document.</para>
1631 </listitem>
1632 </varlistentry>
1633 <varlistentry>
1634 <term>line_numbers</term>
1635 <listitem>
1636 <para>/parser, html, reader/</para>
1637 <para>If this option is activated, libxml2 will store the line number of each element node in the parsed document.
1638 The line number can be obtained using the <function>line_number()</function> method
1639 of the <function>XML::LibXML::Node</function> class (for non-element nodes
1640 this may report the line number of the containing element).
1641 The line numbers are also used for reporting positions of validation errors.
1642 </para>
1643 <para>IMPORTANT:
1644 Due to limitations in the libxml2 library line numbers greater than
1645 65535 will be returned as 65535. Unfortunately, this is a long and sad story, please see
1646 <ulink url="http://bugzilla.gnome.org/show_bug.cgi?id=325533">http://bugzilla.gnome.org/show_bug.cgi?id=325533</ulink> for more details.
1647 </para>
1648 </listitem>
1649 </varlistentry>
1650 <varlistentry>
1651 <term>encoding</term>
1652 <listitem>
1653 <para>/html/</para>
1654 <para>character encoding of the input</para>
1655 </listitem>
1656 </varlistentry>
1657 <varlistentry>
1658 <term>recover</term>
1659 <listitem>
1660 <para>/parser, html, reader/</para>
1661 <para>recover from errors; possible values are 0, 1, and 2</para>
1662 <para>
1663 A true value turns on recovery mode which
1664 allows one to parse broken XML or HTML data.
1665 The recovery mode allows the parser to return
1666 the successfully parsed portion of the input document.
1667 This is useful for almost well-formed documents, where for example
1668 a closing tag is missing somewhere. Still,
1669 XML::LibXML will only parse until the first fatal (non-recoverable) error occurs,
1670 reporting recoverable parsing errors as warnings. To suppress
1671 even these warnings, use recover=>2.</para>
1672 <para>Note that validation is switched off automatically in recovery mode.</para>
1673 </listitem>
1674 </varlistentry>
1675 <varlistentry>
1676 <term>expand_entities</term>
1677 <listitem>
1678 <para>/parser, reader/</para>
1679 <para>substitute entities; possible values are 0 and 1; default is 1</para>
1680 <para>Note that although this flag disables entity substitution, it
1681 does not prevent the parser from loading external entities;
1682 when substitution of an external entity is disabled, the
1683 entity will be represented in the document tree by an XML_ENTITY_REF_NODE node
1684 whose subtree will be the content obtained by parsing the external resource;
1685 Although this nesting is visible from the DOM
1686 it is transparent to XPath data model, so it is possible to
1687 match nodes in an unexpanded entity by the same XPath expression
1688 as if the entity were expanded. See also ext_ent_handler.
1689 </para>
1690 </listitem>
1691 </varlistentry>
1692 <varlistentry>
1693 <term>ext_ent_handler</term>
1694 <listitem>
1695 <para>/parser/</para>
1696 <para>Provide a custom external entity handler
1697 to be used when expand_entities is set to 1.
1698 Possible value is a subroutine reference.
1699 </para>
1700 <para>This feature does not work properly in libxml2 &lt; 2.6.27!</para>
1701 <para>The subroutine provided is called whenever
1702 the parser needs to retrieve the content of an external entity.
1703 It is called with two arguments: the system ID (URI) and the public ID.
1704 The value returned by the subroutine is parsed as the content of the entity.
1705 </para>
1706 <para>This method can be used to completely disable entity loading,
1707 e.g. to prevent exploits of the type described at
1708 <ulink url="http://searchsecuritychannel.techtarget.com/generic/0,295582,sid97_gci1304703,00.html"/>,
1709 where a service is tricked to expose its private data
1710 by letting it parse a remote file (RSS feed) that contains an entity reference to a local
1711 file (e.g. <literal>/etc/fstab</literal>).
1712 </para>
1713 <para>A more granular solution to this problem, however, is
1714 provided by custom URL resolvers, as in
1715 <programlisting>
1716 my $c = XML::LibXML::InputCallback->new();
1717 sub match { # accept file:/ URIs except for XML catalogs in /etc/xml/
1718 my ($uri) = @_;
1719 return ($uri=~m{^file:/}
1720 and $uri !~ m{^file:///etc/xml/})
1721 ? 1 : 0;
1722 }
1723 $c->register_callbacks([ \&amp;match, sub{}, sub{}, sub{} ]);
1724 $parser->input_callbacks($c);
1725 </programlisting>
1726 </para>
1727 </listitem>
1728 </varlistentry>
1729 <varlistentry>
1730 <term>load_ext_dtd</term>
1731 <listitem>
1732 <para>/parser, reader/</para>
1733 <para>load the external DTD subset while parsing; possible values are 0 and 1. Unless specified,
1734 XML::LibXML sets this option to 1.</para>
1735 <para>This flag is also required for DTD Validation, to provide complete attribute,
1736 and to expand entities, regardless if the document has an internal subset.
1737 Thus switching off external DTD loading, will disable entity expansion,
1738 validation, and complete attributes on internal subsets as well.</para>
1739 </listitem>
1740 </varlistentry>
1741 <varlistentry>
1742 <term>complete_attributes</term>
1743 <listitem>
1744 <para>/parser, reader/</para>
1745 <para>create default DTD attributes; possible values are 0 and 1</para>
1746 </listitem>
1747 </varlistentry>
1748 <varlistentry>
1749 <term>validation</term>
1750 <listitem>
1751 <para>/parser, reader/</para>
1752 <para>validate with the DTD; possible values are 0 and 1</para>
1753 </listitem>
1754 </varlistentry>
1755 <varlistentry>
1756 <term>suppress_errors</term>
1757 <listitem>
1758 <para>/parser, html, reader/</para>
1759 <para>suppress error reports; possible values are 0 and 1</para>
1760 </listitem>
1761 </varlistentry>
1762 <varlistentry>
1763 <term>suppress_warnings</term>
1764 <listitem>
1765 <para>/parser, html, reader/</para>
1766 <para>suppress warning reports; possible values are 0 and 1</para>
1767 </listitem>
1768 </varlistentry>
1769 <varlistentry>
1770 <term>pedantic_parser</term>
1771 <listitem>
1772 <para>/parser, html, reader/</para>
1773 <para>pedantic error reporting; possible values are 0 and 1</para>
1774 </listitem>
1775 </varlistentry>
1776 <varlistentry>
1777 <term>no_blanks</term>
1778 <listitem>
1779 <para>/parser, html, reader/</para>
1780 <para>remove blank nodes; possible values are 0 and 1</para>
1781 </listitem>
1782 </varlistentry>
1783 <varlistentry>
1784 <term>no_defdtd</term>
1785 <listitem>
1786 <para>/html/</para>
1787 <para>do not add a default DOCTYPE; possible values are 0 and 1</para>
1788 <para>the default is (0) to add a DTD when the input html lacks one</para>
1789 </listitem>
1790 </varlistentry>
1791 <varlistentry>
1792 <term>expand_xinclude or xinclude</term>
1793 <listitem>
1794 <para>/parser, reader/</para>
1795 <para>Implement XInclude substitution; possible values are 0 and 1</para>
1796 <para>Expands XInclude tags immediately while parsing the document.
1797 Note that the parser will use the URI resolvers installed
1798 via <function>XML::LibXML::InputCallback</function> to parse the included document (if any).</para>
1799 </listitem>
1800 </varlistentry>
1801 <varlistentry>
1802 <term>no_xinclude_nodes</term>
1803 <listitem>
1804 <para>/parser, reader/</para>
1805 <para>do not generate XINCLUDE START/END nodes; possible values are 0 and 1</para>
1806 </listitem>
1807 </varlistentry>
1808 <varlistentry>
1809 <term>no_network</term>
1810 <listitem>
1811 <para>/parser, html, reader/</para>
1812 <para>Forbid network access; possible values are 0 and 1</para>
1813 <para>If set to true, all
1814 attempts to fetch non-local resources (such as
1815 DTD or external entities) will fail (unless
1816 custom callbacks are defined).</para>
1817 <para>It may be
1818 necessary to use the flag <literal>recover</literal> for
1819 processing documents requiring such resources
1820 while networking is off.
1821 </para>
1822 </listitem>
1823 </varlistentry>
1824 <varlistentry>
1825 <term>clean_namespaces</term>
1826 <listitem>
1827 <para>/parser, reader/</para>
1828 <para>remove redundant namespaces declarations during parsing; possible values are 0 and 1.
1829 </para>
1830 </listitem>
1831 </varlistentry>
1832 <varlistentry>
1833 <term>no_cdata</term>
1834 <listitem>
1835 <para>/parser, html, reader/</para>
1836 <para>merge CDATA as text nodes; possible values are 0 and 1</para>
1837 </listitem>
1838 </varlistentry>
1839 <varlistentry>
1840 <term>no_basefix</term>
1841 <listitem>
1842 <para>/parser, reader/</para>
1843 <para>not fixup XINCLUDE xml#base URIS; possible values are 0 and 1</para>
1844 </listitem>
1845 </varlistentry>
1846 <varlistentry>
1847 <term>huge</term>
1848 <listitem>
1849 <para>/parser, html, reader/</para>
1850 <para>relax any hardcoded limit from the parser; possible values are 0 and 1. Unless specified,
1851 XML::LibXML sets this option to 0.</para>
1852 <para>Note: the default value for this option was changed to protect against denial
1853 of service through entity expansion attacks. Before enabling the option ensure
1854 you have taken alternative measures to protect your application against this type
1855 of attack.</para>
1856 </listitem>
1857 </varlistentry>
1858 <varlistentry>
1859 <term>gdome</term>
1860 <listitem>
1861 <para>/parser/</para>
1862 <para>THIS OPTION IS EXPERIMENTAL!</para>
1863 <para>Although quite powerful, XML::LibXML's DOM implementation is incomplete with respect to
1864 the DOM level 2 or level 3 specifications.
1865 XML::GDOME is based on libxml2 as well, and provides a rather complete DOM implementation by wrapping libgdome.
1866 This flag allows you to make
1867 use of XML::LibXML's full parser options and XML::GDOME's DOM implementation at the same time.</para>
1868 <para>To make use of this function, one has to install libgdome and configure XML::LibXML to use this library.
1869 For this you need to rebuild XML::LibXML!</para>
1870 <para>Note: this feature was not seriously tested in recent XML::LibXML releases.</para>
1871 </listitem>
1872 </varlistentry>
1873 </variablelist>
1874 <para>For compatibility with XML::LibXML versions prior to 1.70,
1875 the following methods are also supported for querying and setting the corresponding parser options
1876 (if called without arguments, the methods return
1877 the current value of the corresponding parser options; with an argument sets the option to a given value):
1878 </para>
1879 <programlisting>$parser->validation();
1880 $parser->recover();
1881 $parser->pedantic_parser();
1882 $parser->line_numbers();
1883 $parser->load_ext_dtd();
1884 $parser->complete_attributes();
1885 $parser->expand_xinclude();
1886 $parser->gdome_dom();
1887 $parser->clean_namespaces();
1888 $parser->no_network();</programlisting>
1889 <para>The following obsolete methods trigger parser options in some
1890 special way:</para>
1891 <variablelist>
1892 <varlistentry>
1893 <term>recover_silently</term>
1894 <listitem>
1895 <programlisting>
1896 $parser-&gt;recover_silently(1);
1897 </programlisting>
1898 <para>If called without an argument,
1899 returns true if the current value of the <literal>recover</literal> parser
1900 option is 2 and returns false otherwise.
1901 With a true argument sets the <literal>recover</literal> parser option to 2;
1902 with a false argument sets the <literal>recover</literal> parser option to 0.
1903 </para>
1904 </listitem>
1905 </varlistentry>
1906 <varlistentry>
1907 <term>expand_entities</term>
1908 <listitem>
1909 <programlisting>
1910 $parser-&gt;expand_entities(0);
1911 </programlisting>
1912 <para>Get/set the <literal>expand_entities</literal> option.
1913 If called with a true argument, also turns
1914 the <literal>load_ext_dtd</literal> option to 1.
1915 </para>
1916 </listitem>
1917 </varlistentry>
1918 <varlistentry>
1919 <term>keep_blanks</term>
1920 <listitem>
1921 <programlisting>
1922 $parser-&gt;keep_blanks(0);
1923 </programlisting>
1924 <para>This is actually the opposite of the <literal>no_blanks</literal> parser option.
1925 If used without an argument retrieves negated value of <literal>no_blanks</literal>.
1926 If used with an argument sets <literal>no_blanks</literal> to the opposite value.
1927 </para>
1928 </listitem>
1929 </varlistentry>
1930 <varlistentry>
1931 <term>base_uri</term>
1932
1933 <listitem>
1934 <programlisting>
1935 $parser-&gt;base_uri( $your_base_uri );
1936 </programlisting>
1937 <para>Get/set the <literal>URI</literal> option.</para>
1938 </listitem>
1939 </varlistentry>
1940 </variablelist>
1941 </sect1>
1942 <sect1>
1943 <title>XML Catalogs</title>
1944 <para><literal>libxml2</literal> supports XML catalogs.
1945 Catalogs are used to
1946 map remote resources to their local copies.
1947 Using catalogs can speed up parsing processes if
1948 many external resources from remote addresses
1949 are loaded into the parsed documents (such as DTDs or XIncludes).
1950 </para>
1951 <para>
1952 Note that libxml2 has a global pool of loaded catalogs,
1953 so if you apply the method <literal>load_catalog</literal>
1954 to one parser instance, all parser instances will start using the catalog
1955 (in addition to other previously loaded catalogs).
1956 </para>
1957 <para>Note also that catalogs are not used
1958 when a custom external entity handler is specified. At the
1959 current state it is not possible to make use of both
1960 types of resolving systems at the same time.</para>
1961 <variablelist>
1962 <varlistentry>
1963 <term>load_catalog</term>
1964 <listitem>
1965 <funcsynopsis role="synopsis">
1966 <funcsynopsisinfo>
1967 # XML catalogs
1968 </funcsynopsisinfo>
1969 </funcsynopsis>
1970 <funcsynopsis>
1971 <funcsynopsisinfo>$parser-&gt;load_catalog( $catalog_file );</funcsynopsisinfo>
1972 </funcsynopsis>
1973 <para>Loads the XML catalog file $catalog_file.</para>
1974 <programlisting>
1975 # Global external entity loader (similar to ext_ent_handler option
1976 # but this works really globally, also in XML::LibXSLT include etc..)
1977
1978 XML::LibXML::externalEntityLoader(\&amp;my_loader);
1979 </programlisting>
1980 </listitem>
1981 </varlistentry>
1982 </variablelist>
1983 </sect1>
1984 <sect1>
1985 <title>Error Reporting</title>
1986
1987 <para>XML::LibXML throws exceptions during parsing, validation or XPath processing (and some other occasions). These errors can be caught by using
1988 <emphasis>eval</emphasis> blocks. The error is stored in <emphasis>$@</emphasis>.
1989 There are two implementations: the old one throws $@ which is just a message string,
1990 in the new one $@ is an object from the class XML::LibXML::Error;
1991 this class overrides the operator "" so that when printed,
1992 the object flattens to the usual error message.
1993 </para>
1994
1995 <para>XML::LibXML throws errors as they occur. This is a very common misunderstanding in the use of XML::LibXML. If the eval is omitted, XML::LibXML will always halt your script by
1996 "croaking" (see Carp man page for details).</para>
1997
1998 <para>Also note that an increasing number of functions throw errors if bad data is passed as arguments. If you cannot assure valid data passed to XML::LibXML you should eval
1999 these functions.</para>
2000
2001 <para>Note: since version 1.59, get_last_error() is no longer available in XML::LibXML for thread-safety reasons.</para>
2002 </sect1>
2003 </chapter>
2004
2005 <chapter id="XML-LibXML-SAX">
2006 <title>XML::LibXML direct SAX parser</title>
2007
2008 <titleabbrev>XML::LibXML::SAX</titleabbrev>
2009
2010 <sect1>
2011 <title>Description</title>
2012
2013 <para>XML::LibXML provides an interface to libxml2 direct SAX interface. Through this interface it is possible to generate SAX events directly while
2014 parsing a document. While using the SAX parser XML::LibXML will not create a DOM Document tree.</para>
2015
2016 <para>Such an interface is useful if very large XML documents have to be processed and no DOM functions are required. By using this interface it is
2017 possible to read data stored within an XML document directly into the application data structures without loading the document into memory.</para>
2018
2019 <para>The SAX interface of XML::LibXML is based on the famous XML::SAX interface. It uses the generic interface as provided by XML::SAX::Base.</para>
2020
2021 <para>Additionally to the generic functions, which are only able to process entire documents, XML::LibXML::SAX provides <emphasis>parse_chunk()</emphasis>.
2022 This method generates SAX events from well balanced data such as is often provided by databases.</para>
2023 </sect1>
2024
2025 <sect1>
2026 <title>Features</title>
2027
2028 <para><emphasis>NOTE:</emphasis> This feature is experimental. </para>
2029
2030 <para>You can enable character data joining which may yield a
2031 significant speed boost in your XML processing in lower markup
2032 ratio situations by enabling the
2033 http://xmlns.perl.org/sax/join-character-data feature of this
2034 parser. This is done via the set_feature method like
2035 this:
2036 </para>
2037
2038 <programlisting>$p->set_feature('http://xmlns.perl.org/sax/join-character-data', 1);
2039 </programlisting>
2040
2041 <para>
2042 You can also specify a 0 to disable. The default is to have
2043 this feature disabled.
2044 </para>
2045 </sect1>
2046 </chapter>
2047
2048 <chapter id="XML-LibXML-SAX-Builder">
2049 <title>Building DOM trees from SAX events.</title>
2050
2051 <titleabbrev>XML::LibXML::SAX::Builder</titleabbrev>
2052
2053 <sect1>
2054 <title>Synopsis</title>
2055
2056 <programlisting>use XML::LibXML::SAX::Builder;
2057 my $builder = XML::LibXML::SAX::Builder-&gt;new();
2058
2059 my $gen = XML::Generator::DBI-&gt;new(Handler =&gt; $builder, dbh =&gt; $dbh);
2060 $gen-&gt;execute("SELECT * FROM Users");
2061
2062 my $doc = $builder-&gt;result();</programlisting>
2063 </sect1>
2064
2065 <sect1>
2066 <title>Description</title>
2067
2068 <para>This is a SAX handler that generates a DOM tree from SAX events. Usage is as above. Input is accepted from any SAX1 or SAX2 event generator.</para>
2069
2070 <para>Building DOM trees from SAX events is quite easy with XML::LibXML::SAX::Builder. The class is designed as a SAX2 final handler not as a
2071 filter!</para>
2072
2073 <para>Since SAX is strictly stream oriented, you should not expect anything to return from a generator. Instead you have to ask the builder instance
2074 directly to get the document built. XML::LibXML::SAX::Builder's result() function holds the document generated from the last SAX stream.</para>
2075 </sect1>
2076 </chapter>
2077
2078 <chapter id="XML-LibXML-DOM">
2079 <title>XML::LibXML DOM Implementation</title>
2080 <titleabbrev>XML::LibXML::DOM</titleabbrev>
2081 <sect1>
2082 <title>Description</title>
2083 <para>XML::LibXML provides a lightweight interface to
2084 <emphasis>modify</emphasis> a node of the document tree
2085 generated by the XML::LibXML parser. This interface
2086 follows as far as possible the DOM Level 3
2087 specification. In addition to the specified functions,
2088 XML::LibXML supports some functions that are more handy to
2089 use in the perl environment.</para>
2090
2091 <para>One also has to remember, that XML::LibXML is an
2092 interface to libxml2 nodes which actually reside on the
2093 C-Level of XML::LibXML. This means each node is a
2094 reference to a structure which is different from a perl hash or
2095 array. The only way to access these structures' values is
2096 through the DOM interface provided by XML::LibXML. This
2097 also means, that one <emphasis>can't</emphasis> simply
2098 inherit an XML::LibXML node and add new member variables as
2099 if they were hash keys.</para>
2100
2101 <para>The DOM interface of XML::LibXML does not intend to
2102 implement a full DOM interface as it is done by XML::GDOME
2103 and used for full featured application. Moreover, it
2104 offers an simple way to build or modify documents that are
2105 created by XML::LibXML's parser.</para>
2106
2107 <para>Another target of the XML::LibXML interface is to
2108 make the interfaces of libxml2 available to the perl
2109 community. This includes also some workarounds to some
2110 features where libxml2 assumes more control over the
2111 C-Level that most perl users don't have.</para>
2112
2113 <para>One of the most important parts of the XML::LibXML
2114 DOM interface is that the interfaces try to follow the
2115 <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink> rather strictly. This means the
2116 interface functions are named as the DOM specification
2117 says and not what widespread Java interfaces claim to be
2118 the standard. Although there are several functions that have
2119 only a singular interface that conforms to the DOM spec
2120 XML::LibXML provides an additional Java style alias
2121 interface.</para>
2122
2123 <para>Moreover, there are some function interfaces left over
2124 from early stages of XML::LibXML for compatibility
2125 reasons. These interfaces are for compatibility reasons
2126 <emphasis>only</emphasis>. They might disappear in one of
2127 the future versions of XML::LibXML, so a user is requested
2128 to switch over to the official functions.</para>
2129 <sect2>
2130 <title>Encodings and XML::LibXML's DOM implementation</title>
2131 <para>See the section on Encodings in the <emphasis>XML::LibXML</emphasis> manual page.</para>
2132 </sect2>
2133 <sect2>
2134 <title>Namespaces and XML::LibXML's DOM implementation</title>
2135
2136 <para>XML::LibXML's DOM implementation is
2137 limited by the DOM implementation of libxml2
2138 which treats namespaces slightly differently than
2139 required by the DOM Level 2 specification.
2140 </para>
2141 <para>According to the DOM Level 2 specification,
2142 namespaces of elements and attributes should be
2143 persistent, and nodes should be permanently bound to
2144 namespace URIs as they get created; it should be
2145 possible to manipulate the special attributes used for
2146 declaring XML namespaces just as other attributes
2147 without affecting the namespaces of other nodes.
2148 In DOM Level 2, the application is responsible
2149 for creating the special attributes consistently and/or for correct
2150 serialization of the document.
2151 </para>
2152 <para>
2153 This is both inconvenient, causes problems in serialization
2154 of DOM to XML, and most importantly, seems almost impossible
2155 to implement over libxml2.
2156 </para>
2157 <para>
2158 In libxml2, namespace URI and prefix of a node is
2159 provided by a pointer to a namespace declaration
2160 (appearing as a special xmlns attribute in the XML
2161 document). If the prefix or namespace URI of the
2162 declaration changes, the prefix and namespace URI of all
2163 nodes that point to it changes as well. Moreover, in
2164 contrast to DOM, a node (element or attribute) can only
2165 be bound to a namespace URI if there is some namespace
2166 declaration in the document to point to.
2167 </para>
2168 <para>
2169 Therefore current DOM implementation in XML::LibXML tries
2170 to treat namespace declarations in a compromise between
2171 reason, common sense, limitations of libxml2, and the DOM
2172 Level 2 specification.
2173 </para>
2174 <para>In XML::LibXML, special attributes declaring XML namespaces
2175 are often created automatically, usually when
2176 a namespaced node is attached to a document
2177 and no existing declaration of the namespace and prefix is in the
2178 scope to be reused.
2179 In this respect,
2180 XML::LibXML DOM implementation differs from the DOM
2181 Level 2 specification according to which special
2182 attributes for declaring the appropriate XML namespaces
2183 should not be added when a node with a namespace prefix
2184 and namespace URI is created.
2185 </para>
2186 <para>
2187 Namespace declarations are also created when
2188 <xref linkend="XML-LibXML-Document"/>'s
2189 createElementNS() or createAttributeNS() function are used. If the
2190 a namespace is not declared on the documentElement, the
2191 namespace will be locally declared for the newly created
2192 node. In case of Attributes this may look a bit confusing,
2193 since these nodes cannot have namespace declarations
2194 itself. In this case the namespace is internally applied
2195 to the attribute and later declared on the node the
2196 attribute is appended to (if required).</para>
2197 <para>The following example may explain this a bit:</para>
2198 <programlisting>my $doc = XML::LibXML-&gt;createDocument;
2199 my $root = $doc-&gt;createElementNS( "", "foo" );
2200 $doc-&gt;setDocumentElement( $root );
2201
2202 my $attr = $doc-&gt;createAttributeNS( "bar", "bar:foo", "test" );
2203 $root-&gt;setAttributeNodeNS( $attr );</programlisting>
2204
2205 <para>This piece of code will result in the following document:</para>
2206
2207 <programlisting>&lt;?xml version="1.0"?&gt;
2208 &lt;foo xmlns:bar="bar" bar:foo="test"/&gt;</programlisting>
2209
2210 <para>The namespace is declared on the document element
2211 during the setAttributeNodeNS() call.
2212 </para>
2213 <para>Namespaces can be also declared explicitly by the use of XML::LibXML::Element's setNamespace() function.
2214 Since 1.61, they can also be manipulated with functions
2215 setNamespaceDeclPrefix() and setNamespaceDeclURI() (not available in DOM).
2216 Changing an URI or prefix of an existing namespace declaration
2217 affects the namespace URI and prefix of all nodes which point to it
2218 (that is the nodes in its scope).
2219 </para>
2220 <para>It is also important to repeat the specification:
2221 While working with namespaces you should use the namespace
2222 aware functions instead of the simplified versions. For
2223 example you should <emphasis>never</emphasis> use
2224 setAttribute() but setAttributeNS().</para>
2225 </sect2>
2226 </sect1>
2227 </chapter>
2228
2229 <chapter id="XML-LibXML-Document">
2230 <title>XML::LibXML DOM Document Class</title>
2231
2232 <titleabbrev>XML::LibXML::Document</titleabbrev>
2233 <sect1>
2234 <title>Synopsis</title>
2235 <programlisting>use XML::LibXML;
2236 # Only methods specific to Document nodes are listed here,
2237 # see the XML::LibXML::Node manpage for other methods</programlisting>
2238 </sect1>
2239 <sect1>
2240 <title>Description</title>
2241 <para>The Document Class is in most cases the result of a parsing process. But sometimes it is necessary to create a Document from scratch. The DOM
2242 Document Class provides functions that conform to the DOM Core naming style.</para>
2243
2244 <para>It inherits all functions from <xref linkend="XML-LibXML-Node"/> as specified in the DOM specification. This enables access to the nodes
2245 besides the root element on document level - a <function>DTD</function> for example. The support for these nodes is limited at the moment.</para>
2246
2247 <para>While generally nodes are bound to a document in the DOM concept it is suggested that one should always create a node not bound to any document.
2248 There is no need of really including the node to the document, but once the node is bound to a document, it is quite safe that all strings have the
2249 correct encoding. If an unbound text node with an ISO encoded string is created (e.g. with $CLASS-&gt;new()), the <function>toString</function> function
2250 may not return the expected result.</para>
2251
2252 <para>To prevent such problems, it is recommended to pass all data to XML::LibXML methods
2253 as character strings (i.e. UTF-8 encoded, with the UTF8 flag on).</para>
2254 </sect1>
2255 <sect1>
2256 <title>Methods</title>
2257 <para>
2258 Many functions listed here are
2259 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
2260 the specification for extensive documentation.
2261 </para>
2262 <variablelist>
2263 <varlistentry>
2264 <term>new</term>
2265 <listitem>
2266 <funcsynopsis>
2267 <funcsynopsisinfo>$dom = XML::LibXML::Document-&gt;new( $version, $encoding );</funcsynopsisinfo>
2268 </funcsynopsis>
2269
2270 <para>alias for createDocument()</para>
2271 </listitem>
2272 </varlistentry>
2273
2274 <varlistentry>
2275 <term>createDocument</term>
2276
2277 <listitem>
2278 <funcsynopsis>
2279 <funcsynopsisinfo>$dom = XML::LibXML::Document-&gt;createDocument( $version, $encoding );</funcsynopsisinfo>
2280 </funcsynopsis>
2281
2282 <para>The constructor for the document class. As Parameter it takes the version string and (optionally) the encoding string. Simply calling
2283 <emphasis>createDocument</emphasis>() will create the document:</para>
2284
2285 <programlisting>&lt;?xml version="your version" encoding="your encoding"?&gt;</programlisting>
2286
2287 <para>Both parameter are optional. The default value for <emphasis>$version</emphasis> is <function>1.0</function>, of course. If the
2288 <emphasis>$encoding</emphasis> parameter is not set, the encoding will be left unset, which means UTF-8 is implied.</para>
2289
2290 <para>The call of <emphasis>createDocument</emphasis>() without any parameter will result the following code:</para>
2291
2292 <programlisting>&lt;?xml version="1.0"?&gt; </programlisting>
2293
2294 <para>Alternatively one can call this constructor directly from the XML::LibXML class level, to avoid some typing. This will not have any
2295 effect on the class instance, which is always XML::LibXML::Document.</para>
2296
2297 <programlisting> my $document = XML::LibXML-&gt;createDocument( "1.0", "UTF-8" );</programlisting>
2298
2299 <para>is therefore a shortcut for</para>
2300
2301 <programlisting>my $document = XML::LibXML::Document-&gt;createDocument( "1.0", "UTF-8" );</programlisting>
2302 </listitem>
2303 </varlistentry>
2304 <varlistentry>
2305 <term>URI</term>
2306
2307 <listitem>
2308 <funcsynopsis>
2309 <funcsynopsisinfo>$strURI = $doc-&gt;URI();</funcsynopsisinfo>
2310 </funcsynopsis>
2311
2312 <para>Returns the URI (or filename) of the original document.
2313 For documents obtained by parsing a string of a FH
2314 without using the URI parsing argument of the corresponding <function>parse_*</function> function,
2315 the result is a generated string unknown-XYZ where XYZ is some number;
2316 for documents created with the constructor <function>new</function>,
2317 the URI is undefined.
2318 </para>
2319 <para>The value can be modified by calling <function>setURI</function>
2320 method on the document node.</para>
2321 </listitem>
2322 </varlistentry>
2323 <varlistentry>
2324 <term>setURI</term>
2325
2326 <listitem>
2327 <funcsynopsis>
2328 <funcsynopsisinfo>$doc-&gt;setURI($strURI);</funcsynopsisinfo>
2329 </funcsynopsis>
2330
2331 <para>Sets the URI of the document reported by the method URI
2332 (see also the URI argument to the various <function>parse_*</function> functions).
2333 </para>
2334 </listitem>
2335 </varlistentry>
2336
2337 <varlistentry>
2338 <term>encoding</term>
2339
2340 <listitem>
2341 <funcsynopsis>
2342 <funcsynopsisinfo>$strEncoding = $doc-&gt;encoding();</funcsynopsisinfo>
2343 </funcsynopsis>
2344
2345 <para>returns the encoding string of the document.</para>
2346
2347 <programlisting>my $doc = XML::LibXML-&gt;createDocument( "1.0", "ISO-8859-15" );
2348 print $doc-&gt;encoding; # prints ISO-8859-15</programlisting>
2349
2350 </listitem>
2351 </varlistentry>
2352 <varlistentry>
2353 <term>actualEncoding</term>
2354
2355 <listitem>
2356 <funcsynopsis>
2357 <funcsynopsisinfo>$strEncoding = $doc-&gt;actualEncoding();</funcsynopsisinfo>
2358 </funcsynopsis>
2359
2360 <para>returns the encoding in which the XML will be returned by $doc-&gt;toString().
2361 This is usually the original encoding of the document as declared
2362 in the XML declaration and returned by $doc-&gt;encoding.
2363 If the original encoding is not known (e.g. if created in memory or parsed from a
2364 XML without a declared encoding), 'UTF-8' is returned.
2365 </para>
2366
2367 <programlisting>my $doc = XML::LibXML-&gt;createDocument( "1.0", "ISO-8859-15" );
2368 print $doc-&gt;encoding; # prints ISO-8859-15</programlisting>
2369
2370 </listitem>
2371 </varlistentry>
2372
2373 <varlistentry>
2374 <term>setEncoding</term>
2375
2376 <listitem>
2377 <funcsynopsis>
2378 <funcsynopsisinfo>$doc-&gt;setEncoding($new_encoding);</funcsynopsisinfo>
2379 </funcsynopsis>
2380
2381 <para>This method allows one to change the declaration of
2382 encoding in the XML declaration of the document.
2383 The value also affects the encoding in which the
2384 document is serialized to XML by $doc-&gt;toString().
2385 Use setEncoding() to remove the encoding declaration.
2386 </para>
2387 </listitem>
2388 </varlistentry>
2389
2390 <varlistentry>
2391 <term>version</term>
2392
2393 <listitem>
2394 <funcsynopsis>
2395 <funcsynopsisinfo>$strVersion = $doc-&gt;version();</funcsynopsisinfo>
2396 </funcsynopsis>
2397
2398 <para>returns the version string of the document</para>
2399
2400 <para><emphasis>getVersion()</emphasis> is an alternative form of this function.</para>
2401 </listitem>
2402 </varlistentry>
2403
2404 <varlistentry>
2405 <term>standalone</term>
2406
2407 <listitem>
2408 <funcsynopsis>
2409 <funcsynopsisinfo>$doc-&gt;standalone</funcsynopsisinfo>
2410 </funcsynopsis>
2411
2412 <para>This function returns the Numerical value of a documents XML declarations standalone attribute. It returns <emphasis>1</emphasis> if
2413 standalone="yes" was found, <emphasis>0</emphasis> if standalone="no" was found and <emphasis>-1</emphasis> if standalone
2414 was not specified (default on creation).</para>
2415 </listitem>
2416 </varlistentry>
2417
2418 <varlistentry>
2419 <term>setStandalone</term>
2420
2421 <listitem>
2422 <funcsynopsis>
2423 <funcsynopsisinfo>$doc-&gt;setStandalone($numvalue);</funcsynopsisinfo>
2424 </funcsynopsis>
2425
2426 <para>Through this method it is possible to alter the value of a documents standalone attribute. Set it to <emphasis>1</emphasis> to set
2427 standalone="yes", to <emphasis>0</emphasis> to set standalone="no" or set it to <emphasis>-1</emphasis> to remove the
2428 standalone attribute from the XML declaration.</para>
2429 </listitem>
2430 </varlistentry>
2431
2432 <varlistentry>
2433 <term>compression</term>
2434
2435 <listitem>
2436 <funcsynopsis>
2437 <funcsynopsisinfo>my $compression = $doc-&gt;compression;</funcsynopsisinfo>
2438 </funcsynopsis>
2439
2440 <para>libxml2 allows reading of documents directly from gzipped files. In this case the compression variable is set to the compression level
2441 of that file (0-8). If XML::LibXML parsed a different source or the file wasn't compressed, the returned value will be
2442 <emphasis>-1</emphasis>.</para>
2443 </listitem>
2444 </varlistentry>
2445
2446 <varlistentry>
2447 <term>setCompression</term>
2448
2449 <listitem>
2450 <funcsynopsis>
2451 <funcsynopsisinfo>$doc-&gt;setCompression($ziplevel);</funcsynopsisinfo>
2452 </funcsynopsis>
2453
2454 <para>If one intends to write the document directly to a file, it is possible to set the compression level for a given document. This level
2455 can be in the range from 0 to 8. If XML::LibXML should not try to compress use <emphasis>-1</emphasis> (default).</para>
2456
2457 <para>Note that this feature will <emphasis>only</emphasis> work if libxml2 is compiled with zlib support and toFile() is used for output.</para>
2458 </listitem>
2459 </varlistentry>
2460
2461 <varlistentry>
2462 <term>toString</term>
2463
2464 <listitem>
2465 <funcsynopsis>
2466 <funcsynopsisinfo>$docstring = $dom-&gt;toString($format);</funcsynopsisinfo>
2467 </funcsynopsis>
2468
2469 <para><emphasis>toString</emphasis> is a DOM serializing function,
2470 so the DOM Tree is serialized into an XML string, ready for output.</para>
2471 <para>IMPORTANT: unlike toString for other nodes, on document nodes
2472 this function returns the XML as a byte string in the original encoding of the
2473 document (see the actualEncoding() method)! This means you
2474 can simply do:
2475 </para>
2476 <programlisting>open my $out_fh, '&gt;', $file;
2477 print {$out_fh} $doc-&gt;toString;</programlisting>
2478 <para>regardless of the actual encoding of the document.
2479 See the section on encodings in <xref linkend="XML-LibXML"/> for more details.</para>
2480 <para>The optional <emphasis>$format</emphasis> parameter sets the indenting of the output. This parameter is expected to be an
2481 <function>integer</function> value, that specifies that indentation should be used. The format parameter can have three different values if
2482 it is used:</para>
2483
2484 <para>If $format is 0, than the document is dumped as it was originally parsed</para>
2485
2486 <para>If $format is 1, libxml2 will add ignorable white spaces, so the nodes content is easier to read. Existing text nodes will not be
2487 altered</para>
2488
2489 <para>If $format is 2 (or higher), libxml2 will act as $format == 1 but it add a leading and a trailing line break to each text node.</para>
2490
2491 <para>libxml2 uses a hard-coded indentation of 2 space characters per indentation level. This value can not be altered on run-time.</para>
2492 </listitem>
2493 </varlistentry>
2494
2495 <varlistentry>
2496 <term>toStringC14N</term>
2497
2498 <listitem>
2499 <para><funcsynopsis><funcsynopsisinfo>$c14nstr = $doc-&gt;toStringC14N($comment_flag, $xpath [, $xpath_context ]); </funcsynopsisinfo></funcsynopsis>
2500 See the documentation in <xref linkend="XML-LibXML-Node"/>.
2501 </para>
2502 </listitem>
2503 </varlistentry>
2504 <varlistentry>
2505 <term>toStringEC14N</term>
2506
2507 <listitem>
2508 <para><funcsynopsis><funcsynopsisinfo>$ec14nstr = $doc-&gt;toStringEC14N($comment_flag, $xpath [, $xpath_context ], $inclusive_prefix_list); </funcsynopsisinfo></funcsynopsis>
2509 See the documentation in <xref linkend="XML-LibXML-Node"/>.
2510 </para>
2511 </listitem>
2512 </varlistentry>
2513 <varlistentry>
2514 <term>serialize</term>
2515
2516 <listitem>
2517 <funcsynopsis>
2518 <funcsynopsisinfo>$str = $doc-&gt;serialize($format); </funcsynopsisinfo>
2519 </funcsynopsis>
2520
2521 <para>An alias for toString(). This function was name added to be more consistent
2522 with libxml2.</para>
2523 </listitem>
2524 </varlistentry>
2525
2526 <varlistentry>
2527 <term>serialize_c14n</term>
2528
2529 <listitem>
2530 <para>An alias for toStringC14N().</para>
2531 </listitem>
2532 </varlistentry>
2533 <varlistentry>
2534 <term>serialize_exc_c14n</term>
2535
2536 <listitem>
2537 <para>An alias for toStringEC14N().</para>
2538 </listitem>
2539 </varlistentry>
2540
2541 <varlistentry>
2542 <term>toFile</term>
2543
2544 <listitem>
2545 <funcsynopsis>
2546 <funcsynopsisinfo>$state = $doc-&gt;toFile($filename, $format);</funcsynopsisinfo>
2547 </funcsynopsis>
2548
2549 <para>This function is similar to toString(), but it writes the document directly into a filesystem. This function is very useful, if one
2550 needs to store large documents.</para>
2551
2552 <para>The format parameter has the same behaviour as in toString().</para>
2553 </listitem>
2554 </varlistentry>
2555
2556 <varlistentry>
2557 <term>toFH</term>
2558
2559 <listitem>
2560 <funcsynopsis>
2561 <funcsynopsisinfo>$state = $doc-&gt;toFH($fh, $format);</funcsynopsisinfo>
2562 </funcsynopsis>
2563
2564 <para>This function is similar to toString(), but it writes the document directly to a filehandle or a stream. A byte stream in the document encoding is passed to the file handle. Do NOT apply any <literal>:encoding(...)</literal> or <literal>:utf8</literal> PerlIO layer to
2565 the filehandle! See the section on encodings in <xref linkend="XML-LibXML"/> for more details.</para>
2566
2567 <para>The format parameter has the same behaviour as in toString().</para>
2568 </listitem>
2569 </varlistentry>
2570
2571 <varlistentry>
2572 <term>toStringHTML</term>
2573
2574 <listitem>
2575 <funcsynopsis>
2576 <funcsynopsisinfo>$str = $document-&gt;toStringHTML();</funcsynopsisinfo>
2577 </funcsynopsis>
2578
2579 <para><emphasis>toStringHTML</emphasis> serialize the tree to a byte string in the document encoding as HTML. With this method indenting is automatic and managed by
2580 libxml2 internally.</para>
2581 </listitem>
2582 </varlistentry>
2583
2584 <varlistentry>
2585 <term>serialize_html</term>
2586
2587 <listitem>
2588 <funcsynopsis>
2589 <funcsynopsisinfo>$str = $document-&gt;serialize_html();</funcsynopsisinfo>
2590 </funcsynopsis>
2591
2592 <para>An alias for toStringHTML().</para>
2593 </listitem>
2594 </varlistentry>
2595
2596 <varlistentry>
2597 <term>is_valid</term>
2598
2599 <listitem>
2600 <funcsynopsis>
2601 <funcsynopsisinfo>$bool = $dom-&gt;is_valid();</funcsynopsisinfo>
2602 </funcsynopsis>
2603
2604 <para>Returns either TRUE or FALSE depending on whether the DOM Tree is a valid Document or not.</para>
2605
2606 <para>You may also pass in a <xref linkend="XML-LibXML-Dtd"/> object, to validate against an external DTD:</para>
2607
2608 <programlisting> if (!$dom-&gt;is_valid($dtd)) {
2609 warn("document is not valid!");
2610 }</programlisting>
2611 </listitem>
2612 </varlistentry>
2613
2614 <varlistentry>
2615 <term>validate</term>
2616
2617 <listitem>
2618 <funcsynopsis>
2619 <funcsynopsisinfo>$dom-&gt;validate();</funcsynopsisinfo>
2620 </funcsynopsis>
2621
2622 <para>This is an exception throwing equivalent of is_valid. If the document is not valid it will throw an exception containing the error.
2623 This allows you much better error reporting than simply is_valid or not.</para>
2624
2625 <para>Again, you may pass in a DTD object</para>
2626 </listitem>
2627 </varlistentry>
2628
2629 <varlistentry>
2630 <term>documentElement</term>
2631
2632 <listitem>
2633 <funcsynopsis>
2634 <funcsynopsisinfo>$root = $dom-&gt;documentElement();</funcsynopsisinfo>
2635 </funcsynopsis>
2636
2637 <para>Returns the root element of the Document. A document can have just one root element to contain the documents data.</para>
2638
2639 <para>Optionally one can use <emphasis>getDocumentElement</emphasis>.</para>
2640 </listitem>
2641 </varlistentry>
2642
2643 <varlistentry>
2644 <term>setDocumentElement</term>
2645
2646 <listitem>
2647 <funcsynopsis>
2648 <funcsynopsisinfo>$dom-&gt;setDocumentElement( $root );</funcsynopsisinfo>
2649 </funcsynopsis>
2650
2651 <para>This function enables you to set the root element for a document. The function supports the import of a node from a different document
2652 tree, but does not support a document fragment as $root.</para>
2653 </listitem>
2654 </varlistentry>
2655
2656 <varlistentry>
2657 <term>createElement</term>
2658
2659 <listitem>
2660 <funcsynopsis>
2661 <funcsynopsisinfo>$element = $dom-&gt;createElement( $nodename );</funcsynopsisinfo>
2662 </funcsynopsis>
2663
2664 <para>This function creates a new Element Node bound to the DOM with the name <function>$nodename</function>.</para>
2665 </listitem>
2666 </varlistentry>
2667
2668 <varlistentry>
2669 <term>createElementNS</term>
2670
2671 <listitem>
2672 <funcsynopsis>
2673 <funcsynopsisinfo>$element = $dom-&gt;createElementNS( $namespaceURI, $nodename );</funcsynopsisinfo>
2674 </funcsynopsis>
2675
2676 <para>This function creates a new Element Node bound to the DOM with the name <function>$nodename</function> and placed in the given
2677 namespace.</para>
2678 </listitem>
2679 </varlistentry>
2680
2681 <varlistentry>
2682 <term>createTextNode</term>
2683
2684 <listitem>
2685 <funcsynopsis>
2686 <funcsynopsisinfo>$text = $dom-&gt;createTextNode( $content_text );</funcsynopsisinfo>
2687 </funcsynopsis>
2688
2689 <para>As an equivalent of <emphasis>createElement</emphasis>, but it creates a <emphasis>Text Node</emphasis> bound to the DOM.</para>
2690 </listitem>
2691 </varlistentry>
2692
2693 <varlistentry>
2694 <term>createComment</term>
2695
2696 <listitem>
2697 <funcsynopsis>
2698 <funcsynopsisinfo>$comment = $dom-&gt;createComment( $comment_text );</funcsynopsisinfo>
2699 </funcsynopsis>
2700
2701 <para>As an equivalent of <emphasis>createElement</emphasis>, but it creates a <emphasis>Comment Node</emphasis> bound to the DOM.</para>
2702 </listitem>
2703 </varlistentry>
2704
2705 <varlistentry>
2706 <term>createAttribute</term>
2707
2708 <listitem>
2709 <funcsynopsis>
2710 <funcsynopsisinfo>$attrnode = $doc-&gt;createAttribute($name [,$value]);</funcsynopsisinfo>
2711 </funcsynopsis>
2712
2713 <para>Creates a new Attribute node.</para>
2714 </listitem>
2715 </varlistentry>
2716
2717 <varlistentry>
2718 <term>createAttributeNS</term>
2719
2720 <listitem>
2721 <funcsynopsis>
2722 <funcsynopsisinfo>$attrnode = $doc-&gt;createAttributeNS( namespaceURI, $name [,$value] );</funcsynopsisinfo>
2723 </funcsynopsis>
2724
2725 <para>Creates an Attribute bound to a namespace.</para>
2726 </listitem>
2727 </varlistentry>
2728
2729 <varlistentry>
2730 <term>createDocumentFragment</term>
2731
2732 <listitem>
2733 <funcsynopsis>
2734 <funcsynopsisinfo>$fragment = $doc-&gt;createDocumentFragment();</funcsynopsisinfo>
2735 </funcsynopsis>
2736
2737 <para>This function creates a DocumentFragment.</para>
2738 </listitem>
2739 </varlistentry>
2740
2741 <varlistentry>
2742 <term>createCDATASection</term>
2743
2744 <listitem>
2745 <funcsynopsis>
2746 <funcsynopsisinfo>$cdata = $dom-&gt;createCDATASection( $cdata_content );</funcsynopsisinfo>
2747 </funcsynopsis>
2748
2749 <para>Similar to createTextNode and createComment, this function creates a CDataSection bound to the current DOM.</para>
2750 </listitem>
2751 </varlistentry>
2752
2753 <varlistentry>
2754 <term>createProcessingInstruction</term>
2755
2756 <listitem>
2757 <funcsynopsis>
2758 <funcsynopsisinfo>my $pi = $doc-&gt;createProcessingInstruction( $target, $data );</funcsynopsisinfo>
2759 </funcsynopsis>
2760
2761 <para>create a processing instruction node.</para>
2762
2763 <para>Since this method is quite long one may use its short form <emphasis>createPI()</emphasis>.</para>
2764 </listitem>
2765 </varlistentry>
2766
2767 <varlistentry>
2768 <term>createEntityReference</term>
2769
2770 <listitem>
2771 <funcsynopsis>
2772 <funcsynopsisinfo>my $entref = $doc-&gt;createEntityReference($refname);</funcsynopsisinfo>
2773 </funcsynopsis>
2774
2775 <para>If a document has a DTD specified, one can create entity references by using this function. If one wants to add a entity reference to
2776 the document, this reference has to be created by this function.</para>
2777
2778 <para>An entity reference is unique to a document and cannot be passed to other documents as other nodes can be passed.</para>
2779
2780 <para><emphasis>NOTE:</emphasis> A text content containing something that looks like an entity reference, will not be expanded to a real
2781 entity reference unless it is a predefined entity</para>
2782
2783 <programlisting> my $string = "&amp;foo;";
2784 $some_element-&gt;appendText( $string );
2785 print $some_element-&gt;textContent; # prints "&amp;amp;foo;"</programlisting>
2786 </listitem>
2787 </varlistentry>
2788
2789 <varlistentry>
2790 <term>createInternalSubset</term>
2791
2792 <listitem>
2793 <funcsynopsis>
2794 <funcsynopsisinfo>$dtd = $document-&gt;createInternalSubset( $rootnode, $public, $system);</funcsynopsisinfo>
2795 </funcsynopsis>
2796
2797 <para>This function creates and adds an internal subset to the given document. Because the function automatically adds the DTD to the document
2798 there is no need to add the created node explicitly to the document.</para>
2799
2800 <programlisting> my $document = XML::LibXML::Document-&gt;new();
2801 my $dtd = $document-&gt;createInternalSubset( "foo", undef, "foo.dtd" );</programlisting>
2802
2803 <para>will result in the following XML document:</para>
2804
2805 <programlisting>&lt;?xml version="1.0"?&gt;
2806 &lt;!DOCTYPE foo SYSTEM "foo.dtd"&gt; </programlisting>
2807
2808 <para>By setting the public parameter it is possible to set PUBLIC DTDs to a given document. So</para>
2809
2810 <programlisting>my $document = XML::LibXML::Document-&gt;new();
2811 my $dtd = $document-&gt;createInternalSubset( "foo", "-//FOO//DTD FOO 0.1//EN", undef );
2812 </programlisting>
2813
2814 <para>will cause the following declaration to be created on the document:</para>
2815
2816 <programlisting>&lt;?xml version="1.0"?&gt;
2817 &lt;!DOCTYPE foo PUBLIC "-//FOO//DTD FOO 0.1//EN"&gt;</programlisting>
2818 </listitem>
2819 </varlistentry>
2820
2821 <varlistentry>
2822 <term>createExternalSubset</term>
2823
2824 <listitem>
2825 <funcsynopsis>
2826 <funcsynopsisinfo>$dtd = $document-&gt;createExternalSubset( $rootnode_name, $publicId, $systemId);</funcsynopsisinfo>
2827 </funcsynopsis>
2828
2829 <para>This function is similar to <function>createInternalSubset()</function> but this DTD is considered to be external and is therefore not
2830 added to the document itself. Nevertheless it can be used for validation purposes.</para>
2831 </listitem>
2832 </varlistentry>
2833
2834 <varlistentry>
2835 <term>importNode</term>
2836
2837 <listitem>
2838 <funcsynopsis>
2839 <funcsynopsisinfo>$document-&gt;importNode( $node );</funcsynopsisinfo>
2840 </funcsynopsis>
2841
2842 <para>If a node is not part of a document, it can be imported to another document. As specified in DOM Level 2 Specification the Node will
2843 not be altered or removed from its original document (<function>$node-&gt;cloneNode(1)</function> will get called implicitly).</para>
2844
2845 <para><emphasis>NOTE:</emphasis> Don't try to use importNode() to import sub-trees that contain an entity reference - even if the entity
2846 reference is the root node of the sub-tree. This will cause serious problems to your program. This is a limitation of libxml2 and not of
2847 XML::LibXML itself.</para>
2848 </listitem>
2849 </varlistentry>
2850
2851 <varlistentry>
2852 <term>adoptNode</term>
2853
2854 <listitem>
2855 <funcsynopsis>
2856 <funcsynopsisinfo>$document-&gt;adoptNode( $node );</funcsynopsisinfo>
2857 </funcsynopsis>
2858
2859 <para>If a node is not part of a document, it can be imported to another document. As specified in DOM Level 3 Specification the Node will
2860 not be altered but it will removed from its original document.</para>
2861
2862 <para>After a document adopted a node, the node, its attributes and all its descendants belong to the new document. Because the node does
2863 not belong to the old document, it will be unlinked from its old location first.</para>
2864
2865 <para><emphasis>NOTE:</emphasis> Don't try to adoptNode() to import sub-trees that contain entity references - even if the entity
2866 reference is the root node of the sub-tree. This will cause serious problems to your program. This is a limitation of libxml2 and not of
2867 XML::LibXML itself.</para>
2868 </listitem>
2869 </varlistentry>
2870
2871 <varlistentry>
2872 <term>externalSubset</term>
2873
2874 <listitem>
2875 <funcsynopsis>
2876 <funcsynopsisinfo>my $dtd = $doc-&gt;externalSubset;</funcsynopsisinfo>
2877 </funcsynopsis>
2878
2879 <para>If a document has an external subset defined it will be returned by this function.</para>
2880
2881 <para><emphasis>NOTE</emphasis> Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in XML::LibXML is still limited. In
2882 particular one may not want use common node function on doctype declaration nodes!</para>
2883 </listitem>
2884 </varlistentry>
2885
2886 <varlistentry>
2887 <term>internalSubset</term>
2888
2889 <listitem>
2890 <funcsynopsis>
2891 <funcsynopsisinfo>my $dtd = $doc-&gt;internalSubset;</funcsynopsisinfo>
2892 </funcsynopsis>
2893
2894 <para>If a document has an internal subset defined it will be returned by this function.</para>
2895
2896 <para><emphasis>NOTE</emphasis> Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in XML::LibXML is still limited. In
2897 particular one may not want use common node function on doctype declaration nodes!</para>
2898 </listitem>
2899 </varlistentry>
2900
2901 <varlistentry>
2902 <term>setExternalSubset</term>
2903
2904 <listitem>
2905 <funcsynopsis>
2906 <funcsynopsisinfo>$doc-&gt;setExternalSubset($dtd);</funcsynopsisinfo>
2907 </funcsynopsis>
2908
2909 <para><emphasis>EXPERIMENTAL!</emphasis></para>
2910
2911 <para>This method sets a DTD node as an external subset of the given document.</para>
2912 </listitem>
2913 </varlistentry>
2914
2915 <varlistentry>
2916 <term>setInternalSubset</term>
2917
2918 <listitem>
2919 <funcsynopsis>
2920 <funcsynopsisinfo>$doc-&gt;setInternalSubset($dtd);</funcsynopsisinfo>
2921 </funcsynopsis>
2922
2923 <para><emphasis>EXPERIMENTAL!</emphasis></para>
2924
2925 <para>This method sets a DTD node as an internal subset of the given document.</para>
2926 </listitem>
2927 </varlistentry>
2928
2929 <varlistentry>
2930 <term>removeExternalSubset</term>
2931
2932 <listitem>
2933 <funcsynopsis>
2934 <funcsynopsisinfo>my $dtd = $doc-&gt;removeExternalSubset();</funcsynopsisinfo>
2935 </funcsynopsis>
2936
2937 <para><emphasis>EXPERIMENTAL!</emphasis></para>
2938
2939 <para>If a document has an external subset defined it can be removed from the document by using this function. The removed dtd node will be
2940 returned.</para>
2941 </listitem>
2942 </varlistentry>
2943
2944 <varlistentry>
2945 <term>removeInternalSubset</term>
2946
2947 <listitem>
2948 <funcsynopsis>
2949 <funcsynopsisinfo>my $dtd = $doc-&gt;removeInternalSubset();</funcsynopsisinfo>
2950 </funcsynopsis>
2951
2952 <para><emphasis>EXPERIMENTAL!</emphasis></para>
2953
2954 <para>If a document has an internal subset defined it can be removed from the document by using this function. The removed dtd node will be
2955 returned.</para>
2956 </listitem>
2957 </varlistentry>
2958
2959 <varlistentry>
2960 <term>getElementsByTagName</term>
2961
2962 <listitem>
2963 <funcsynopsis>
2964 <funcsynopsisinfo>my @nodelist = $doc-&gt;getElementsByTagName($tagname);</funcsynopsisinfo>
2965 </funcsynopsis>
2966
2967 <para>Implements the DOM Level 2 function</para>
2968
2969 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
2970 </listitem>
2971 </varlistentry>
2972
2973 <varlistentry>
2974 <term>getElementsByTagNameNS</term>
2975
2976 <listitem>
2977 <funcsynopsis>
2978 <funcsynopsisinfo>my @nodelist = $doc-&gt;getElementsByTagNameNS($nsURI,$tagname);</funcsynopsisinfo>
2979 </funcsynopsis>
2980
2981 <para>Implements the DOM Level 2 function</para>
2982
2983 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
2984 </listitem>
2985 </varlistentry>
2986
2987 <varlistentry>
2988 <term>getElementsByLocalName</term>
2989
2990 <listitem>
2991 <funcsynopsis>
2992 <funcsynopsisinfo>my @nodelist = $doc-&gt;getElementsByLocalName($localname);</funcsynopsisinfo>
2993 </funcsynopsis>
2994
2995 <para>This allows the fetching of all nodes from a given document with the given Localname.</para>
2996
2997 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
2998 </listitem>
2999 </varlistentry>
3000
3001 <varlistentry>
3002 <term>getElementById</term>
3003
3004 <listitem>
3005 <funcsynopsis>
3006 <funcsynopsisinfo>my $node = $doc-&gt;getElementById($id);</funcsynopsisinfo>
3007 </funcsynopsis>
3008 <para>Returns the element that has an ID attribute
3009 with the given value. If no such element exists,
3010 this returns undef.</para>
3011 <para>Note: the ID of an element
3012 may change while manipulating the document.
3013 For documents with a DTD, the information about ID attributes
3014 is only available if DTD loading/validation has been requested.
3015 For HTML documents parsed with the HTML
3016 parser ID detection is done
3017 automatically. In XML documents, all "xml:id"
3018 attributes are considered to be of type ID.
3019 You can test ID-ness of an attribute node
3020 with $attr-&gt;isId().
3021 </para>
3022 <para>In versions 1.59 and earlier this method was
3023 called getElementsById() (plural) by
3024 mistake. Starting from 1.60 this name is
3025 maintained as an alias only for backward compatibility.
3026 </para>
3027 </listitem>
3028 </varlistentry>
3029
3030 <varlistentry>
3031 <term>indexElements</term>
3032
3033 <listitem>
3034 <funcsynopsis>
3035 <funcsynopsisinfo>$dom-&gt;indexElements();</funcsynopsisinfo>
3036 </funcsynopsis>
3037
3038 <para>This function causes libxml2 to stamp all elements in a document with their document position index which considerably speeds up XPath
3039 queries for large documents. It should only be used with static documents that won't be further changed by any DOM methods, because once
3040 a document is indexed, XPath will always prefer the index to other methods of determining the document order of nodes. XPath could therefore
3041 return improperly ordered node-lists when applied on a document that has been changed after being indexed. It is of course possible to use
3042 this method to re-index a modified document before using it with XPath again. This function is not a part of the DOM specification.</para>
3043
3044 <para>This function returns number of elements indexed, -1 if error occurred, or -2 if this feature is not available in the running libxml2.</para>
3045 </listitem>
3046 </varlistentry>
3047 </variablelist>
3048 </sect1>
3049 </chapter>
3050
3051 <chapter id="XML-LibXML-Node">
3052 <title>Abstract Base Class of XML::LibXML Nodes</title>
3053
3054 <titleabbrev>XML::LibXML::Node</titleabbrev>
3055 <sect1>
3056 <title>Synopsis</title>
3057 <programlisting>use XML::LibXML;</programlisting>
3058 </sect1>
3059 <sect1>
3060 <title>Description</title>
3061
3062 <para>XML::LibXML::Node defines functions that are common to
3063 all Node Types. An XML::LibXML::Node should never be created
3064 standalone, but as an instance of a high level class such as
3065 XML::LibXML::Element or XML::LibXML::Text. The class itself should
3066 provide only common functionality. In XML::LibXML each node is
3067 part either of a document or a document-fragment. Because of
3068 this there is no node without a parent. This may causes
3069 confusion with "unbound" nodes.</para>
3070 </sect1>
3071 <sect1>
3072 <title>Methods</title>
3073 <para>
3074 Many functions listed here are
3075 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
3076 the specification for extensive documentation.
3077 </para>
3078
3079 <variablelist>
3080 <varlistentry>
3081 <term>nodeName</term>
3082
3083 <listitem>
3084 <funcsynopsis>
3085 <funcsynopsisinfo>$name = $node-&gt;nodeName;</funcsynopsisinfo>
3086 </funcsynopsis>
3087
3088 <para>Returns the node's name. This function is
3089 aware of namespaces and returns the full name of
3090 the current node (<function>prefix:localname</function>).
3091 </para>
3092 <para>Since 1.62 this function also returns the correct
3093 DOM names for node types with constant names, namely:
3094 #text, #cdata-section, #comment, #document,
3095 #document-fragment.
3096 </para>
3097 </listitem>
3098 </varlistentry>
3099
3100 <varlistentry>
3101 <term>setNodeName</term>
3102
3103 <listitem>
3104 <funcsynopsis>
3105 <funcsynopsisinfo>$node-&gt;setNodeName( $newName );</funcsynopsisinfo>
3106 </funcsynopsis>
3107
3108 <para>In very limited situations, it is useful to change a nodes name. In the DOM specification this should throw an error. This Function is
3109 aware of namespaces.</para>
3110 </listitem>
3111 </varlistentry>
3112
3113
3114
3115 <varlistentry>
3116 <term>isSameNode</term>
3117
3118 <listitem>
3119 <funcsynopsis>
3120 <funcsynopsisinfo>$bool = $node-&gt;isSameNode( $other_node );</funcsynopsisinfo>
3121 </funcsynopsis>
3122
3123 <para>returns TRUE (1) if the given nodes refer to
3124 the same node structure, otherwise FALSE (0) is
3125 returned.</para>
3126 </listitem>
3127 </varlistentry>
3128
3129
3130
3131 <varlistentry>
3132 <term>isEqual</term>
3133
3134 <listitem>
3135 <funcsynopsis>
3136 <funcsynopsisinfo>$bool = $node-&gt;isEqual( $other_node );</funcsynopsisinfo>
3137 </funcsynopsis>
3138
3139 <para>deprecated version of isSameNode().</para>
3140
3141 <para><emphasis>NOTE</emphasis> isEqual will change behaviour to follow the DOM specification</para>
3142 </listitem>
3143 </varlistentry>
3144
3145
3146
3147 <varlistentry>
3148 <term>unique_key</term>
3149
3150 <listitem>
3151 <funcsynopsis>
3152 <funcsynopsisinfo>$num = $node-&gt;unique_key;</funcsynopsisinfo>
3153 </funcsynopsis>
3154
3155 <para>This function is not specified for any DOM level. It returns a key guaranteed to be unique for this node, and to always be the same value for this node. In other words, two node objects return the same key if and only if isSameNode indicates that they are the same node.</para>
3156
3157 <para>The returned key value is useful as a key in hashes.</para>
3158 </listitem>
3159 </varlistentry>
3160
3161
3162
3163 <varlistentry>
3164 <term>nodeValue</term>
3165
3166 <listitem>
3167 <funcsynopsis>
3168 <funcsynopsisinfo>$content = $node-&gt;nodeValue;</funcsynopsisinfo>
3169 </funcsynopsis>
3170
3171 <para>If the node has any content (such as stored in a <function>text node</function>) it can get requested through this function.</para>
3172
3173 <para><emphasis>NOTE:</emphasis> Element Nodes have no content per definition. To get the text value of an Element use textContent()
3174 instead!</para>
3175 </listitem>
3176 </varlistentry>
3177
3178
3179
3180 <varlistentry>
3181 <term>textContent</term>
3182
3183 <listitem>
3184 <funcsynopsis>
3185 <funcsynopsisinfo>$content = $node-&gt;textContent;</funcsynopsisinfo>
3186 </funcsynopsis>
3187
3188 <para>this function returns the content of all text nodes in the descendants of the given node as specified in DOM.</para>
3189 </listitem>
3190 </varlistentry>
3191
3192
3193
3194 <varlistentry>
3195 <term>nodeType</term>
3196
3197 <listitem>
3198 <funcsynopsis>
3199 <funcsynopsisinfo>$type = $node-&gt;nodeType;</funcsynopsisinfo>
3200 </funcsynopsis>
3201
3202 <para>Return a numeric value representing the node type of this node.
3203 The module XML::LibXML by default exports constants
3204 for the node types (see the EXPORT section in the
3205 <xref linkend="XML-LibXML"/> manual page).</para>
3206 </listitem>
3207 </varlistentry>
3208
3209 <varlistentry>
3210 <term>unbindNode</term>
3211
3212 <listitem>
3213 <funcsynopsis>
3214 <funcsynopsisinfo>$node-&gt;unbindNode();</funcsynopsisinfo>
3215 </funcsynopsis>
3216
3217 <para>Unbinds the Node from its siblings and Parent, but not from the Document it belongs to. If the node is not inserted into the DOM
3218 afterwards, it will be lost after the program terminates. From a low level view, the unbound node is stripped from the context it is and
3219 inserted into a (hidden) document-fragment.</para>
3220 </listitem>
3221 </varlistentry>
3222
3223
3224
3225 <varlistentry>
3226 <term>removeChild</term>
3227
3228 <listitem>
3229 <funcsynopsis>
3230 <funcsynopsisinfo>$childnode = $node-&gt;removeChild( $childnode );</funcsynopsisinfo>
3231 </funcsynopsis>
3232
3233 <para>This will unbind the Child Node from its parent <function>$node</function>. The function returns the unbound node. If
3234 <function>oldNode</function> is not a child of the given Node the function will fail.</para>
3235 </listitem>
3236 </varlistentry>
3237
3238
3239
3240 <varlistentry>
3241 <term>replaceChild</term>
3242
3243 <listitem>
3244 <funcsynopsis>
3245 <funcsynopsisinfo>$oldnode = $node-&gt;replaceChild( $newNode, $oldNode );</funcsynopsisinfo>
3246 </funcsynopsis>
3247
3248 <para>Replaces the <function>$oldNode</function> with the <function>$newNode</function>. The <function>$oldNode</function> will be unbound
3249 from the Node. This function differs from the DOM L2 specification, in the case, if the new node is not part of the document, the node will
3250 be imported first.</para>
3251 </listitem>
3252 </varlistentry>
3253
3254
3255
3256 <varlistentry>
3257 <term>replaceNode</term>
3258
3259 <listitem>
3260 <funcsynopsis>
3261 <funcsynopsisinfo>$node-&gt;replaceNode($newNode);</funcsynopsisinfo>
3262 </funcsynopsis>
3263
3264 <para>This function is very similar to replaceChild(), but it replaces the node itself rather than a childnode. This is useful if a node
3265 found by any XPath function, should be replaced.</para>
3266 </listitem>
3267 </varlistentry>
3268
3269
3270
3271 <varlistentry>
3272 <term>appendChild</term>
3273
3274 <listitem>
3275 <funcsynopsis>
3276 <funcsynopsisinfo>$childnode = $node-&gt;appendChild( $childnode );</funcsynopsisinfo>
3277 </funcsynopsis>
3278
3279 <para>The function will add the <function>$childnode</function> to the end of <function>$node</function>'s children. The function should
3280 fail, if the new childnode is already a child of <function>$node</function>. This function differs from the DOM L2 specification, in the
3281 case, if the new node is not part of the document, the node will be imported first.</para>
3282 </listitem>
3283 </varlistentry>
3284
3285
3286
3287 <varlistentry>
3288 <term>addChild</term>
3289
3290 <listitem>
3291 <funcsynopsis>
3292 <funcsynopsisinfo>$childnode = $node-&gt;addChild( $childnode );</funcsynopsisinfo>
3293 </funcsynopsis>
3294
3295 <para>As an alternative to appendChild() one can use the addChild() function. This function is a bit faster, because it avoids all DOM
3296 conformity checks. Therefore this function is quite useful if one builds XML documents in memory where the order and ownership (<function>ownerDocument</function>)
3297 is assured.</para>
3298
3299 <para>addChild() uses libxml2's own xmlAddChild() function. Thus it has to be used with extra care: If a text node is added to a node
3300 and the node itself or its last childnode is as well a text node, the node to add will be merged with the one already available. The current
3301 node will be removed from memory after this action. Because perl is not aware of this action, the perl instance is still available.
3302 XML::LibXML will catch the loss of a node and refuse to run any function called on that node.</para>
3303
3304 <programlisting> my $t1 = $doc-&gt;createTextNode( "foo" );
3305 my $t2 = $doc-&gt;createTextNode( "bar" );
3306 $t1-&gt;addChild( $t2 ); # is OK
3307 my $val = $t2-&gt;nodeValue(); # will fail, script dies</programlisting>
3308
3309 <para>Also addChild() will not check if the added node belongs to the same document as the node it will be added to. This could lead to
3310 inconsistent documents and in more worse cases even to memory violations, if one does not keep track of this issue.</para>
3311
3312 <para>Although this sounds like a lot of trouble, addChild() is useful if a document is built from a stream, such as happens sometimes in
3313 SAX handlers or filters.</para>
3314
3315 <para>If you are not sure about the source of your nodes, you better stay with appendChild(), because this function is more user friendly in
3316 the sense of being more error tolerant.</para>
3317 </listitem>
3318 </varlistentry>
3319
3320
3321
3322 <varlistentry>
3323 <term>addNewChild</term>
3324
3325 <listitem>
3326 <funcsynopsis>
3327 <funcsynopsisinfo>$node = $parent-&gt;addNewChild( $nsURI, $name );</funcsynopsisinfo>
3328 </funcsynopsis>
3329
3330 <para>Similar to <function>addChild()</function>, this function uses low level libxml2 functionality to provide faster interface for DOM
3331 building. <emphasis>addNewChild()</emphasis> uses <function>xmlNewChild()</function> to create a new node on a given parent element.</para>
3332
3333 <para>addNewChild() has two parameters $nsURI and $name, where $nsURI is an (optional) namespace URI. $name is the fully qualified element
3334 name; addNewChild() will determine the correct prefix if necessary.</para>
3335
3336 <para>The function returns the newly created node.</para>
3337
3338 <para>This function is very useful for DOM building, where a created node can be directly associated with its parent. <emphasis>NOTE</emphasis>
3339 this function is not part of the DOM specification and its use will limit your code to XML::LibXML.</para>
3340 </listitem>
3341 </varlistentry>
3342
3343
3344
3345 <varlistentry>
3346 <term>addSibling</term>
3347
3348 <listitem>
3349 <funcsynopsis>
3350 <funcsynopsisinfo>$node-&gt;addSibling($newNode);</funcsynopsisinfo>
3351 </funcsynopsis>
3352
3353 <para>addSibling() allows adding an additional node to the end of a nodelist, defined by the given node.</para>
3354 </listitem>
3355 </varlistentry>
3356
3357
3358
3359 <varlistentry>
3360 <term>cloneNode</term>
3361
3362 <listitem>
3363 <funcsynopsis>
3364 <funcsynopsisinfo>$newnode =$node-&gt;cloneNode( $deep );</funcsynopsisinfo>
3365 </funcsynopsis>
3366
3367 <para><emphasis>cloneNode</emphasis> creates a
3368 copy of <function>$node</function>. When $deep is
3369 set to 1 (true) the function will copy all
3370 child nodes as well. If $deep is 0 only the current
3371 node will be copied. Note that in case of element,
3372 attributes are copied even if $deep is 0.
3373 </para>
3374 <para>Note that the behavior of this function for $deep=0
3375 has changed in 1.62 in order to be consistent with the DOM spec
3376 (in older versions attributes and namespace information
3377 was not copied for elements).</para>
3378 </listitem>
3379 </varlistentry>
3380
3381
3382
3383 <varlistentry>
3384 <term>parentNode</term>
3385
3386 <listitem>
3387 <funcsynopsis>
3388 <funcsynopsisinfo>$parentnode = $node-&gt;parentNode;</funcsynopsisinfo>
3389 </funcsynopsis>
3390
3391 <para>Returns simply the Parent Node of the current node.</para>
3392 </listitem>
3393 </varlistentry>
3394
3395
3396
3397 <varlistentry>
3398 <term>nextSibling</term>
3399
3400 <listitem>
3401 <funcsynopsis>
3402 <funcsynopsisinfo>$nextnode = $node-&gt;nextSibling();</funcsynopsisinfo>
3403 </funcsynopsis>
3404
3405 <para>Returns the next sibling if any .</para>
3406 </listitem>
3407 </varlistentry>
3408
3409 <varlistentry>
3410 <term>nextNonBlankSibling</term>
3411
3412 <listitem>
3413 <funcsynopsis>
3414 <funcsynopsisinfo>$nextnode = $node-&gt;nextNonBlankSibling();</funcsynopsisinfo>
3415 </funcsynopsis>
3416
3417 <para>Returns the next non-blank sibling if any (a node is blank if it is a Text or CDATA node consisting of whitespace only). This method is not defined by DOM.</para>
3418 </listitem>
3419 </varlistentry>
3420
3421
3422
3423 <varlistentry>
3424 <term>previousSibling</term>
3425
3426 <listitem>
3427 <funcsynopsis>
3428 <funcsynopsisinfo>$prevnode = $node-&gt;previousSibling();</funcsynopsisinfo>
3429 </funcsynopsis>
3430
3431 <para>Analogous to <emphasis>getNextSibling</emphasis> the function returns the previous sibling if any.</para>
3432 </listitem>
3433 </varlistentry>
3434
3435 <varlistentry>
3436 <term>previousNonBlankSibling</term>
3437
3438 <listitem>
3439 <funcsynopsis>
3440 <funcsynopsisinfo>$prevnode = $node-&gt;previousNonBlankSibling();</funcsynopsisinfo>
3441 </funcsynopsis>
3442
3443 <para>Returns the previous non-blank sibling if any (a node is blank if it is a Text or CDATA node consisting of whitespace only). This method is not defined by DOM.</para>
3444 </listitem>
3445 </varlistentry>
3446
3447
3448
3449 <varlistentry>
3450 <term>hasChildNodes</term>
3451
3452 <listitem>
3453 <funcsynopsis>
3454 <funcsynopsisinfo>$boolean = $node-&gt;hasChildNodes();</funcsynopsisinfo>
3455 </funcsynopsis>
3456
3457 <para>If the current node has child nodes this function returns TRUE (1), otherwise it returns FALSE (0, not undef).</para>
3458 </listitem>
3459 </varlistentry>
3460
3461
3462
3463 <varlistentry>
3464 <term>firstChild</term>
3465
3466 <listitem>
3467 <funcsynopsis>
3468 <funcsynopsisinfo>$childnode = $node-&gt;firstChild;</funcsynopsisinfo>
3469 </funcsynopsis>
3470
3471 <para>If a node has child nodes this function will return the first node in the child list.</para>
3472 </listitem>
3473 </varlistentry>
3474
3475
3476
3477 <varlistentry>
3478 <term>lastChild</term>
3479
3480 <listitem>
3481 <funcsynopsis>
3482 <funcsynopsisinfo>$childnode = $node-&gt;lastChild;</funcsynopsisinfo>
3483 </funcsynopsis>
3484
3485 <para>If the <function>$node</function> has child nodes this function returns the last child node.</para>
3486 </listitem>
3487 </varlistentry>
3488
3489
3490
3491 <varlistentry>
3492 <term>ownerDocument</term>
3493
3494 <listitem>
3495 <funcsynopsis>
3496 <funcsynopsisinfo>$documentnode = $node-&gt;ownerDocument;</funcsynopsisinfo>
3497 </funcsynopsis>
3498
3499 <para>Through this function it is always possible to access the document the current node is bound to.</para>
3500 </listitem>
3501 </varlistentry>
3502
3503
3504
3505 <varlistentry>
3506 <term>getOwner</term>
3507
3508 <listitem>
3509 <funcsynopsis>
3510 <funcsynopsisinfo>$node = $node-&gt;getOwner;</funcsynopsisinfo>
3511 </funcsynopsis>
3512
3513 <para>This function returns the node the current node is associated with. In most cases this will be a document node or a document fragment
3514 node.</para>
3515 </listitem>
3516 </varlistentry>
3517
3518
3519
3520 <varlistentry>
3521 <term>setOwnerDocument</term>
3522
3523 <listitem>
3524 <funcsynopsis>
3525 <funcsynopsisinfo>$node-&gt;setOwnerDocument( $doc );</funcsynopsisinfo>
3526 </funcsynopsis>
3527
3528 <para>This function binds a node to another DOM. This method unbinds the node first, if it is already bound to another document.</para>
3529
3530 <para>This function is the opposite calling of <xref linkend="XML-LibXML-Document"/>'s adoptNode() function. Because of this it has the same limitations
3531 with Entity References as adoptNode().</para>
3532 </listitem>
3533 </varlistentry>
3534
3535
3536
3537 <varlistentry>
3538 <term>insertBefore</term>
3539
3540 <listitem>
3541 <funcsynopsis>
3542 <funcsynopsisinfo>$node-&gt;insertBefore( $newNode, $refNode );</funcsynopsisinfo>
3543 </funcsynopsis>
3544
3545 <para>The method inserts <function>$newNode</function> before <function>$refNode</function>. If <function>$refNode</function> is undefined,
3546 the newNode will be set as the new last child of the parent node. This function differs from the DOM L2 specification, in the case, if the
3547 new node is not part of the document, the node will be imported first, automatically.</para>
3548
3549 <para>$refNode has to be passed to the function even if it is undefined:</para>
3550
3551 <programlisting> $node-&gt;insertBefore( $newNode, undef ); # the same as $node-&gt;appendChild( $newNode );
3552 $node-&gt;insertBefore( $newNode ); # wrong</programlisting>
3553
3554 <para>Note, that the reference node has to be a direct child of the node the function is called on. Also, $newChild is not allowed to be an
3555 ancestor of the new parent node.</para>
3556 </listitem>
3557 </varlistentry>
3558
3559
3560
3561 <varlistentry>
3562 <term>insertAfter</term>
3563
3564 <listitem>
3565 <funcsynopsis>
3566 <funcsynopsisinfo>$node-&gt;insertAfter( $newNode, $refNode );</funcsynopsisinfo>
3567 </funcsynopsis>
3568
3569 <para>The method inserts <function>$newNode</function> after <function>$refNode</function>. If <function>$refNode</function> is undefined,
3570 the newNode will be set as the new last child of the parent node.</para>
3571
3572 <para>Note, that $refNode has to be passed explicitly even if it is undef.</para>
3573 </listitem>
3574 </varlistentry>
3575
3576 <varlistentry>
3577 <term>findnodes</term>
3578
3579 <listitem>
3580 <funcsynopsis>
3581 <funcsynopsisinfo>@nodes = $node-&gt;findnodes( $xpath_expression );</funcsynopsisinfo>
3582 </funcsynopsis>
3583
3584 <para><emphasis>findnodes</emphasis> evaluates the xpath expression (XPath 1.0) on the current node and returns the resulting node set as an array. In scalar context, returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
3585 <para>The xpath expression can be passed either as a string, or
3586 as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
3587 </para>
3588 <para><emphasis>NOTE ON NAMESPACES AND XPATH</emphasis>:</para>
3589 <para>A common mistake about
3590 XPath is to assume that node tests consisting of an
3591 element name with no prefix match elements in the default
3592 namespace. This assumption is wrong - by XPath
3593 specification, such node tests can only match elements
3594 that are in no (i.e. null) namespace.
3595 </para>
3596 <para>
3597 So, for example, one cannot match the root element of an
3598 XHTML document with <literal>$node-&gt;find('/html')</literal>
3599 since <literal>'/html'</literal> would only match if the
3600 root element <literal>&lt;html&gt;</literal> had no
3601 namespace, but all XHTML elements belong to the namespace
3602 http://www.w3.org/1999/xhtml. (Note that
3603 <literal>xmlns="..."</literal> namespace declarations can
3604 also be specified in a DTD, which makes the situation even worse, since
3605 the XML document looks as if there was no default namespace).
3606 </para>
3607 <para>There are several possible ways to deal with namespaces in XPath:
3608 </para>
3609 <itemizedlist>
3610 <listitem>
3611 <para>
3612 The recommended way is to use the
3613 <xref linkend="XML-LibXML-XPathContext"/> module
3614 to define an explicit context
3615 for XPath evaluation, in which a document independent
3616 prefix-to-namespace mapping can be defined. For
3617 example:
3618 </para>
3619 <programlisting>my $xpc = XML::LibXML::XPathContext-&gt;new;
3620 $xpc-&gt;registerNs('x', 'http://www.w3.org/1999/xhtml');
3621 $xpc-&gt;find('/x:html',$node);</programlisting>
3622 </listitem>
3623 <listitem><para>
3624 Another possibility is to use prefixes declared
3625 in the queried document (if known).
3626 If the document declares a prefix for the
3627 namespace in question (and the context node is in the
3628 scope of the declaration),
3629 <function>XML::LibXML</function> allows you to use the
3630 prefix in the XPath expression, e.g.:
3631 </para>
3632 <programlisting>$node-&gt;find('/x:html');</programlisting>
3633 </listitem>
3634 </itemizedlist>
3635 <para>See also XML::LibXML::XPathContext-&gt;findnodes.</para>
3636 </listitem>
3637 </varlistentry>
3638
3639
3640
3641 <varlistentry>
3642 <term>find</term>
3643
3644 <listitem>
3645 <funcsynopsis>
3646 <funcsynopsisinfo>$result = $node-&gt;find( $xpath );</funcsynopsisinfo>
3647 </funcsynopsis>
3648
3649 <para><emphasis>find</emphasis> evaluates the XPath 1.0 expression using the current node as the context of the expression, and returns the
3650 result depending on what type of result the XPath expression had. For example, the XPath "1 * 3 + 52" results in a
3651 <olink targetdoc="XML::LibXML::Number">XML::LibXML::Number</olink> object being returned. Other expressions might return an <olink targetdoc="XML::LibXML::Boolean">XML::LibXML::Boolean</olink>
3652 object, or an <olink targetdoc="XML::LibXML::Literal">XML::LibXML::Literal</olink> object (a string). Each of those objects uses Perl's overload feature to "do
3653 the right thing" in different contexts.</para>
3654 <para>The xpath expression can be passed either as a string,
3655 or as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
3656 </para>
3657 <para>See also <xref linkend="XML-LibXML-XPathContext"/>-&gt;find.</para>
3658 </listitem>
3659 </varlistentry>
3660
3661
3662
3663 <varlistentry>
3664 <term>findvalue</term>
3665
3666 <listitem>
3667 <funcsynopsis>
3668 <funcsynopsisinfo>print $node-&gt;findvalue( $xpath );</funcsynopsisinfo>
3669 </funcsynopsis>
3670
3671 <para><emphasis>findvalue</emphasis> is exactly equivalent to:</para>
3672
3673 <programlisting> $node-&gt;find( $xpath )-&gt;to_literal; </programlisting>
3674
3675 <para>That is, it returns the literal value of the results. This enables you to ensure that you get a string back from your search, allowing
3676 certain shortcuts. This could be used as the equivalent of XSLT's &lt;xsl:value-of select="some_xpath"/&gt;.</para>
3677 <para>See also <xref linkend="XML-LibXML-XPathContext"/>-&gt;findvalue.</para>
3678 <para>The xpath expression can be passed either as a string, or
3679 as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
3680 </para>
3681 </listitem>
3682 </varlistentry>
3683
3684 <varlistentry>
3685 <term>exists</term>
3686
3687 <listitem>
3688 <funcsynopsis>
3689 <funcsynopsisinfo>$bool = $node-&gt;exists( $xpath_expression );</funcsynopsisinfo>
3690 </funcsynopsis>
3691 <para>This method behaves like <emphasis>findnodes</emphasis>, except
3692 that it only returns a boolean value (1 if the expression matches a node, 0 otherwise)
3693 and may be faster than <emphasis>findnodes</emphasis>, because
3694 the XPath evaluation may stop early on the first match (this is true for libxml2 >= 2.6.27).
3695 </para><para>For XPath expressions that do not return node-set,
3696 the method returns true if the returned value is a non-zero number or a non-empty string.</para>
3697 </listitem>
3698 </varlistentry>
3699
3700 <varlistentry>
3701 <term>childNodes</term>
3702
3703 <listitem>
3704 <funcsynopsis>
3705 <funcsynopsisinfo>@childnodes = $node-&gt;childNodes();</funcsynopsisinfo>
3706 </funcsynopsis>
3707
3708 <para><emphasis>childNodes</emphasis> implements a more intuitive interface to the childnodes of the current node. It enables you to pass
3709 all children directly to a <function>map</function> or <function>grep</function>. If this function is called in scalar context, a
3710 <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object will be returned.</para>
3711 </listitem>
3712 </varlistentry>
3713
3714 <varlistentry>
3715 <term>nonBlankChildNodes</term>
3716
3717 <listitem>
3718 <funcsynopsis>
3719 <funcsynopsisinfo>@childnodes = $node-&gt;nonBlankChildNodes();</funcsynopsisinfo>
3720 </funcsynopsis>
3721
3722 <para>This is like <emphasis>childNodes</emphasis>,
3723 but returns only non-blank nodes
3724 (where a node is blank if it is a Text or CDATA node consisting of whitespace only). This method is not defined by DOM.</para>
3725 </listitem>
3726 </varlistentry>
3727
3728 <varlistentry>
3729 <term>toString</term>
3730
3731 <listitem>
3732 <funcsynopsis>
3733 <funcsynopsisinfo>$xmlstring = $node-&gt;toString($format,$docencoding);</funcsynopsisinfo>
3734 </funcsynopsis>
3735
3736 <para>This method is similar to the method <function>toString</function> of a <xref linkend="XML-LibXML-Document"/> but for a single node. It returns a string consisting of XML serialization of the given node and all its descendants. Unlike <function>XML::LibXML::Document::toString</function>, in this case the resulting string is by default a character string (UTF-8 encoded with UTF8 flag on). An optional flag $format controls indentation, as in <function>XML::LibXML::Document::toString</function>. If the second optional $docencoding flag is true, the result will be a byte string in the document encoding (see <function>XML::LibXML::Document::actualEncoding</function>).</para>
3737 </listitem>
3738 </varlistentry>
3739
3740 <varlistentry>
3741 <term>toStringC14N</term>
3742
3743 <listitem>
3744 <funcsynopsis>
3745 <funcsynopsisinfo>$c14nstring = $node-&gt;toStringC14N();
3746 $c14nstring = $node-&gt;toStringC14N($with_comments, $xpath_expression , $xpath_context);</funcsynopsisinfo>
3747 </funcsynopsis>
3748
3749 <para>The function is similar to
3750 toString(). Instead of simply serializing the
3751 document tree, it transforms it as it is specified
3752 in the XML-C14N Specification
3753 (see <ulink url="http://www.w3.org/TR/xml-c14n">http://www.w3.org/TR/xml-c14n</ulink>).
3754 Such transformation is known as
3755 canonization.</para>
3756
3757 <para>If $with_comments is 0 or not defined, the
3758 result-document will not contain any comments that
3759 exist in the original document. To include
3760 comments into the canonized document,
3761 $with_comments has to be set to 1.</para>
3762
3763 <para>The parameter $xpath_expression defines the
3764 nodeset of nodes that should be visible in the
3765 resulting document. This can be used to filter out
3766 some nodes. One has to note, that only the nodes
3767 that are part of the nodeset, will be included
3768 into the result-document. Their child-nodes will
3769 not exist in the resulting document, unless they
3770 are part of the nodeset defined by the xpath
3771 expression.
3772 </para>
3773 <para>If $xpath_expression is omitted or empty,
3774 toStringC14N() will include all nodes in the given
3775 sub-tree, using the following XPath expressions:
3776 with comments
3777 <programlisting>(. | .//node() | .//@* | .//namespace::*)</programlisting>
3778 and without comments
3779 <programlisting>(. | .//node() | .//@* | .//namespace::*)[not(self::comment())]</programlisting>
3780 </para>
3781 <para>
3782 An optional parameter $xpath_context can be used
3783 to pass an <xref linkend="XML-LibXML-XPathContext"/> object defining
3784 the context for evaluation of $xpath_expression.
3785 This is useful for mapping namespace prefixes used in the XPath expression
3786 to namespace URIs.
3787 Note, however, that
3788 $node will be used as the context node for the evaluation, not
3789 the context node of $xpath_context!
3790 </para>
3791 </listitem>
3792 </varlistentry>
3793 <varlistentry>
3794 <term>toStringC14N_v1_1</term>
3795
3796 <listitem>
3797 <funcsynopsis>
3798 <funcsynopsisinfo>$c14nstring = $node-&gt;toStringC14N_v1_1();
3799 $c14nstring = $node-&gt;toStringC14N_v1_1($with_comments, $xpath_expression , $xpath_context);</funcsynopsisinfo>
3800 </funcsynopsis>
3801 <para>
3802 This function behaves like toStringC14N() except that
3803 it uses the "XML_C14N_1_1" constant for
3804 canonicalising using the "C14N 1.1 spec".
3805 </para>
3806 </listitem>
3807 </varlistentry>
3808 <varlistentry>
3809 <term>toStringEC14N</term>
3810
3811 <listitem>
3812 <funcsynopsis>
3813 <funcsynopsisinfo>$ec14nstring = $node-&gt;toStringEC14N();
3814 $ec14nstring = $node-&gt;toStringEC14N($with_comments, $xpath_expression, $inclusive_prefix_list);
3815 $ec14nstring = $node-&gt;toStringEC14N($with_comments, $xpath_expression, $xpath_context, $inclusive_prefix_list);</funcsynopsisinfo>
3816 </funcsynopsis>
3817
3818 <para>The function is similar to toStringC14N() but follows
3819 the XML-EXC-C14N Specification (see <ulink url="http://www.w3.org/TR/xml-exc-c14n">http://www.w3.org/TR/xml-exc-c14n</ulink>)
3820 for exclusive canonization of XML.</para>
3821
3822 <para>The arguments $with_comments, $xpath_expression, $xpath_context are as in toStringC14N().
3823 An ARRAY reference can be passed as the last argument $inclusive_prefix_list,
3824 listing namespace prefixes that are to be handled in the manner described by the Canonical XML Recommendation (i.e. preserved in the output even if the namespace is not used). C.f. the spec for details.
3825 </para>
3826 </listitem>
3827 </varlistentry>
3828
3829 <varlistentry>
3830 <term>serialize</term>
3831
3832 <listitem>
3833 <funcsynopsis>
3834 <funcsynopsisinfo>$str = $doc-&gt;serialize($format); </funcsynopsisinfo>
3835 </funcsynopsis>
3836 <para>An alias for toString(). This function was name added to be more consistent
3837 with libxml2.</para>
3838 </listitem>
3839 </varlistentry>
3840
3841 <varlistentry>
3842 <term>serialize_c14n</term>
3843
3844 <listitem>
3845 <para>An alias for toStringC14N().</para>
3846 </listitem>
3847 </varlistentry>
3848 <varlistentry>
3849 <term>serialize_exc_c14n</term>
3850
3851 <listitem>
3852 <para>An alias for toStringEC14N().</para>
3853 </listitem>
3854 </varlistentry>
3855
3856
3857
3858 <varlistentry>
3859 <term>localname</term>
3860
3861 <listitem>
3862 <funcsynopsis>
3863 <funcsynopsisinfo>$localname = $node-&gt;localname;</funcsynopsisinfo>
3864 </funcsynopsis>
3865
3866 <para>Returns the local name of a tag. This is the part behind the colon.</para>
3867 </listitem>
3868 </varlistentry>
3869
3870
3871
3872 <varlistentry>
3873 <term>prefix</term>
3874
3875 <listitem>
3876 <funcsynopsis>
3877 <funcsynopsisinfo>$nameprefix = $node-&gt;prefix;</funcsynopsisinfo>
3878 </funcsynopsis>
3879
3880 <para>Returns the prefix of a tag. This is the part before the colon.</para>
3881 </listitem>
3882 </varlistentry>
3883
3884
3885
3886 <varlistentry>
3887 <term>namespaceURI</term>
3888
3889 <listitem>
3890 <funcsynopsis>
3891 <funcsynopsisinfo>$uri = $node-&gt;namespaceURI();</funcsynopsisinfo>
3892 </funcsynopsis>
3893
3894 <para>returns the URI of the current namespace.</para>
3895 </listitem>
3896 </varlistentry>
3897
3898
3899
3900 <varlistentry>
3901 <term>hasAttributes</term>
3902
3903 <listitem>
3904 <funcsynopsis>
3905 <funcsynopsisinfo>$boolean = $node-&gt;hasAttributes();</funcsynopsisinfo>
3906 </funcsynopsis>
3907
3908 <para>returns 1 (TRUE) if the current node has any attributes set, otherwise 0 (FALSE) is returned.</para>
3909 </listitem>
3910 </varlistentry>
3911
3912
3913
3914 <varlistentry>
3915 <term>attributes</term>
3916
3917 <listitem>
3918 <funcsynopsis>
3919 <funcsynopsisinfo>@attributelist = $node-&gt;attributes();</funcsynopsisinfo>
3920 </funcsynopsis>
3921
3922 <para>This function returns all attributes and namespace declarations assigned to the given node.</para>
3923
3924 <para>Because XML::LibXML does not implement namespace declarations and attributes the same way, it is required to test what kind of node is
3925 handled while accessing the functions result.</para>
3926
3927 <para>If this function is called in array context the attribute nodes are returned as an array. In scalar context, the function will return a
3928 <olink targetdoc="XML::LibXML::NamedNodeMap">XML::LibXML::NamedNodeMap</olink> object.</para>
3929 </listitem>
3930 </varlistentry>
3931
3932
3933
3934 <varlistentry>
3935 <term>lookupNamespaceURI</term>
3936
3937 <listitem>
3938 <funcsynopsis>
3939 <funcsynopsisinfo>$URI = $node-&gt;lookupNamespaceURI( $prefix );</funcsynopsisinfo>
3940 </funcsynopsis>
3941
3942 <para>Find a namespace URI by its prefix starting at the current node.</para>
3943 </listitem>
3944 </varlistentry>
3945
3946
3947
3948 <varlistentry>
3949 <term>lookupNamespacePrefix</term>
3950
3951 <listitem>
3952 <funcsynopsis>
3953 <funcsynopsisinfo>$prefix = $node-&gt;lookupNamespacePrefix( $URI );</funcsynopsisinfo>
3954 </funcsynopsis>
3955
3956 <para>Find a namespace prefix by its URI starting at the current node.</para>
3957
3958 <para><emphasis>NOTE</emphasis> Only the namespace URIs are meant to be unique. The prefix is only document related. Also the document might
3959 have more than a single prefix defined for a namespace.</para>
3960 </listitem>
3961 </varlistentry>
3962
3963
3964 <varlistentry>
3965 <term>normalize</term>
3966
3967 <listitem>
3968 <funcsynopsis>
3969 <funcsynopsisinfo>$node-&gt;normalize;</funcsynopsisinfo>
3970 </funcsynopsis>
3971
3972 <para>This function normalizes adjacent text nodes. This function is not as strict as libxml2's xmlTextMerge() function, since it will
3973 not free a node that is still referenced by the perl layer.</para>
3974 </listitem>
3975 </varlistentry>
3976
3977
3978
3979 <varlistentry>
3980 <term>getNamespaces</term>
3981
3982 <listitem>
3983 <funcsynopsis>
3984 <funcsynopsisinfo>@nslist = $node-&gt;getNamespaces;</funcsynopsisinfo>
3985 </funcsynopsis>
3986
3987 <para>If a node has any namespaces defined, this function will return these namespaces. Note, that this will not return all namespaces that
3988 are in scope, but only the ones declared explicitly for that node.</para>
3989
3990 <para>Although getNamespaces is available for all nodes, it only makes sense if used with element nodes.</para>
3991 </listitem>
3992 </varlistentry>
3993
3994
3995
3996 <varlistentry>
3997 <term>removeChildNodes</term>
3998
3999 <listitem>
4000 <funcsynopsis>
4001 <funcsynopsisinfo>$node-&gt;removeChildNodes();</funcsynopsisinfo>
4002 </funcsynopsis>
4003
4004 <para>This function is not specified for any DOM level: It removes all childnodes from a node in a single step. Other than the libxml2
4005 function itself (xmlFreeNodeList), this function will not immediately remove the nodes from the memory. This saves one from getting memory
4006 violations, if there are nodes still referred to from the Perl level.</para>
4007 </listitem>
4008 </varlistentry>
4009
4010 <varlistentry>
4011 <term>baseURI ()</term>
4012 <listitem>
4013 <funcsynopsis>
4014 <funcsynopsisinfo>$strURI = $node-&gt;baseURI();</funcsynopsisinfo>
4015 </funcsynopsis>
4016 <para>
4017 Searches for the base URL of the node. The method should work on both XML
4018 and HTML documents even if base mechanisms for these are completely different.
4019 It returns the base as defined in RFC 2396 sections
4020 "5.1.1. Base URI within Document Content"
4021 and
4022 "5.1.2. Base URI from the Encapsulating Entity".
4023 However it does not return the document base (5.1.3), use method <function>URI</function>
4024 of <function>XML::LibXML::Document</function> for this.
4025 </para>
4026 </listitem>
4027 </varlistentry>
4028 <varlistentry>
4029 <term>setBaseURI ($strURI)</term>
4030 <listitem>
4031 <funcsynopsis>
4032 <funcsynopsisinfo>$node-&gt;setBaseURI($strURI);</funcsynopsisinfo>
4033 </funcsynopsis>
4034 <para>This method only does something useful for an element node
4035 in an XML document.
4036 It sets the xml:base attribute on the node to $strURI, which
4037 effectively sets the base URI of the node to the same value.
4038 </para>
4039 <para>
4040 Note: For HTML documents this behaves as if the document was XML
4041 which may not be desired, since it does not effectively
4042 set the base URI of the node. See RFC 2396 appendix D
4043 for an example of how base URI can be specified in HTML.
4044 </para>
4045 </listitem>
4046 </varlistentry>
4047
4048 <varlistentry>
4049 <term>nodePath</term>
4050
4051 <listitem>
4052 <funcsynopsis>
4053 <funcsynopsisinfo>$node-&gt;nodePath();</funcsynopsisinfo>
4054 </funcsynopsis>
4055
4056 <para>This function is not specified for any DOM level: It returns a canonical structure based XPath for a given node.</para>
4057 </listitem>
4058 </varlistentry>
4059
4060
4061
4062 <varlistentry>
4063 <term>line_number</term>
4064
4065 <listitem>
4066 <funcsynopsis>
4067 <funcsynopsisinfo>$lineno = $node-&gt;line_number();</funcsynopsisinfo>
4068 </funcsynopsis>
4069
4070 <para>This function returns the line number where the tag was found during parsing. If a node is added to the document the line number is 0.
4071 Problems may occur, if a node from one document is passed to another one.</para>
4072 <para>IMPORTANT:
4073 Due to limitations in the libxml2 library line numbers greater than
4074 65535 will be returned as 65535. Please see
4075 <ulink url="http://bugzilla.gnome.org/show_bug.cgi?id=325533">http://bugzilla.gnome.org/show_bug.cgi?id=325533</ulink> for more details.
4076 </para>
4077 <para>Note: line_number() is special to XML::LibXML and not part of the DOM specification.</para>
4078
4079 <para>If the line_numbers flag of the parser was not activated before parsing, line_number() will always return 0.</para>
4080 </listitem>
4081 </varlistentry>
4082 </variablelist>
4083 </sect1>
4084 </chapter>
4085
4086 <chapter id="XML-LibXML-Element">
4087 <title>XML::LibXML Class for Element Nodes</title>
4088
4089 <titleabbrev>XML::LibXML::Element</titleabbrev>
4090 <sect1>
4091 <title>Synopsis</title>
4092 <programlisting>use XML::LibXML;
4093 # Only methods specific to Element nodes are listed here,
4094 # see the XML::LibXML::Node manpage for other methods</programlisting>
4095 </sect1>
4096 <sect1>
4097 <title>Methods</title>
4098 <para>
4099 The class inherits from <xref linkend="XML-LibXML-Node"/>.
4100 The documentation for Inherited methods is not listed here.
4101 </para>
4102 <para>
4103 Many functions listed here are
4104 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
4105 the specification for extensive documentation.
4106 </para>
4107 <variablelist>
4108 <varlistentry>
4109 <term>new</term>
4110
4111 <listitem>
4112 <funcsynopsis>
4113 <funcsynopsisinfo>$node = XML::LibXML::Element-&gt;new( $name );</funcsynopsisinfo>
4114 </funcsynopsis>
4115
4116 <para>This function creates a new node unbound to any DOM.</para>
4117 </listitem>
4118 </varlistentry>
4119
4120
4121
4122 <varlistentry>
4123 <term>setAttribute</term>
4124
4125 <listitem>
4126 <funcsynopsis>
4127 <funcsynopsisinfo>$node-&gt;setAttribute( $aname, $avalue );</funcsynopsisinfo>
4128 </funcsynopsis>
4129
4130 <para>This method sets or replaces the node's attribute <function>$aname</function> to the value <function>$avalue</function></para>
4131 </listitem>
4132 </varlistentry>
4133
4134
4135
4136 <varlistentry>
4137 <term>setAttributeNS</term>
4138
4139 <listitem>
4140 <funcsynopsis>
4141 <funcsynopsisinfo>$node-&gt;setAttributeNS( $nsURI, $aname, $avalue );</funcsynopsisinfo>
4142 </funcsynopsis>
4143 <para>Namespace-aware version of <function>setAttribute</function>, where
4144 <function>$nsURI</function> is a namespace URI,
4145 <function>$aname</function> is a qualified name,
4146 and <function>$avalue</function> is the value.
4147 The namespace URI may be null (empty or undefined)
4148 in order to create an attribute which has no namespace.
4149 </para>
4150 <para>
4151 The current implementation differs from DOM in the following aspects
4152 </para>
4153 <para>
4154 If an attribute with the same local name and namespace URI already exists
4155 on the element, but its prefix differs from the prefix of <function>$aname</function>,
4156 then this function is supposed to change the prefix (regardless
4157 of namespace declarations and possible collisions).
4158 However, the current implementation does rather the opposite.
4159 If a prefix is declared for the namespace URI in the scope
4160 of the attribute, then the already declared prefix is used,
4161 disregarding the prefix specified in <function>$aname</function>.
4162 If no prefix is declared for the namespace, the function tries
4163 to declare the prefix specified in <function>$aname</function>
4164 and dies if the prefix is already taken by some other namespace.
4165 </para>
4166 <para>According to DOM Level 2 specification, this method can also be used to
4167 create or modify special attributes used for declaring XML namespaces
4168 (which belong to the namespace "http://www.w3.org/2000/xmlns/" and
4169 have prefix or name "xmlns"). This should work since version 1.61,
4170 but again the implementation differs from DOM specification in the following:
4171 if a declaration of the same namespace prefix already exists
4172 on the element, then changing its value via this method
4173 automatically changes the namespace of all elements and attributes
4174 in its scope. This is because in libxml2 the namespace URI of an element
4175 is not static but is computed from a pointer to a namespace declaration attribute.
4176 </para>
4177 </listitem>
4178 </varlistentry>
4179
4180
4181
4182 <varlistentry>
4183 <term>getAttribute</term>
4184
4185 <listitem>
4186 <funcsynopsis>
4187 <funcsynopsisinfo>$avalue = $node-&gt;getAttribute( $aname );</funcsynopsisinfo>
4188 </funcsynopsis>
4189
4190 <para>If <function>$node</function> has an attribute with the name <function>$aname</function>, the value of this attribute will get
4191 returned.</para>
4192 </listitem>
4193 </varlistentry>
4194
4195
4196
4197 <varlistentry>
4198 <term>getAttributeNS</term>
4199
4200 <listitem>
4201 <funcsynopsis>
4202 <funcsynopsisinfo>$avalue = $node-&gt;getAttributeNS( $nsURI, $aname );</funcsynopsisinfo>
4203 </funcsynopsis>
4204
4205 <para>Retrieves an attribute value by local name and namespace URI.</para>
4206 </listitem>
4207 </varlistentry>
4208
4209
4210
4211 <varlistentry>
4212 <term>getAttributeNode</term>
4213
4214 <listitem>
4215 <funcsynopsis>
4216 <funcsynopsisinfo>$attrnode = $node-&gt;getAttributeNode( $aname );</funcsynopsisinfo>
4217 </funcsynopsis>
4218
4219 <para>Retrieve an attribute node by name. If no attribute with a given name exists, <function>undef</function> is returned.</para>
4220 </listitem>
4221 </varlistentry>
4222
4223
4224
4225 <varlistentry>
4226 <term>getAttributeNodeNS</term>
4227
4228 <listitem>
4229 <funcsynopsis>
4230 <funcsynopsisinfo>$attrnode = $node-&gt;getAttributeNodeNS( $namespaceURI, $aname );</funcsynopsisinfo>
4231 </funcsynopsis>
4232
4233 <para>Retrieves an attribute node by local name and namespace URI. If no attribute with a given localname and namespace exists, <function>undef</function> is returned.</para>
4234 </listitem>
4235 </varlistentry>
4236
4237
4238
4239 <varlistentry>
4240 <term>removeAttribute</term>
4241
4242 <listitem>
4243 <funcsynopsis>
4244 <funcsynopsisinfo>$node-&gt;removeAttribute( $aname );</funcsynopsisinfo>
4245 </funcsynopsis>
4246
4247 <para>The method removes the attribute <function>$aname</function> from the node's attribute list, if the attribute can be found.</para>
4248 </listitem>
4249 </varlistentry>
4250
4251
4252
4253 <varlistentry>
4254 <term>removeAttributeNS</term>
4255
4256 <listitem>
4257 <funcsynopsis>
4258 <funcsynopsisinfo>$node-&gt;removeAttributeNS( $nsURI, $aname );</funcsynopsisinfo>
4259 </funcsynopsis>
4260
4261 <para>Namespace version of <function>removeAttribute</function></para>
4262 </listitem>
4263 </varlistentry>
4264
4265
4266
4267 <varlistentry>
4268 <term>hasAttribute</term>
4269
4270 <listitem>
4271 <funcsynopsis>
4272 <funcsynopsisinfo>$boolean = $node-&gt;hasAttribute( $aname );</funcsynopsisinfo>
4273 </funcsynopsis>
4274
4275 <para>This function tests if the named attribute is set for the node. If the attribute is specified, TRUE (1) will be returned, otherwise the
4276 return value is FALSE (0).</para>
4277 </listitem>
4278 </varlistentry>
4279
4280
4281
4282 <varlistentry>
4283 <term>hasAttributeNS</term>
4284
4285 <listitem>
4286 <funcsynopsis>
4287 <funcsynopsisinfo>$boolean = $node-&gt;hasAttributeNS( $nsURI, $aname );</funcsynopsisinfo>
4288 </funcsynopsis>
4289
4290 <para>namespace version of <function>hasAttribute</function></para>
4291 </listitem>
4292 </varlistentry>
4293
4294
4295
4296 <varlistentry>
4297 <term>getChildrenByTagName</term>
4298
4299 <listitem>
4300 <funcsynopsis>
4301 <funcsynopsisinfo>@nodes = $node-&gt;getChildrenByTagName($tagname);</funcsynopsisinfo>
4302 </funcsynopsis>
4303
4304 <para>The function gives direct access to all child elements of the current node with a given tagname, where
4305 tagname is a qualified name, that is, in case of namespace usage it may consist of a prefix and local
4306 name. This function makes things a lot easier if one needs
4307 to handle big data sets. A special tagname '*' can be used to match any name.</para>
4308
4309 <para>If this function is called in SCALAR context, it returns the number of elements found.</para>
4310 </listitem>
4311 </varlistentry>
4312
4313
4314
4315 <varlistentry>
4316 <term>getChildrenByTagNameNS</term>
4317
4318 <listitem>
4319 <funcsynopsis>
4320 <funcsynopsisinfo>@nodes = $node-&gt;getChildrenByTagNameNS($nsURI,$tagname);</funcsynopsisinfo>
4321 </funcsynopsis>
4322
4323 <para>Namespace version of <function>getChildrenByTagName</function>. A special nsURI '*' matches any namespace URI,
4324 in which case the function behaves just like <function>getChildrenByLocalName</function>.</para>
4325
4326 <para>If this function is called in SCALAR context, it returns the number of elements found.</para>
4327 </listitem>
4328 </varlistentry>
4329
4330
4331
4332 <varlistentry>
4333 <term>getChildrenByLocalName</term>
4334
4335 <listitem>
4336 <funcsynopsis>
4337 <funcsynopsisinfo>@nodes = $node-&gt;getChildrenByLocalName($localname);</funcsynopsisinfo>
4338 </funcsynopsis>
4339
4340 <para>The function gives direct access to all child elements of the current node with a given local name. It makes things a lot easier if one needs
4341 to handle big data sets. A special <function>localname</function> '*' can be used to match any local name.</para>
4342
4343 <para>If this function is called in SCALAR context, it returns the number of elements found.</para>
4344 </listitem>
4345 </varlistentry>
4346
4347
4348
4349 <varlistentry>
4350 <term>getElementsByTagName</term>
4351
4352 <listitem>
4353 <funcsynopsis>
4354 <funcsynopsisinfo>@nodes = $node-&gt;getElementsByTagName($tagname);</funcsynopsisinfo>
4355 </funcsynopsis>
4356
4357 <para>This function is part of the spec. It
4358 fetches all descendants of a node with a given tagname,
4359 where <function>tagname</function> is a qualified name,
4360 that is, in case of namespace usage it may consist of a prefix and
4361 local name.
4362 A special <function>tagname</function> '*' can be used to match any tag name.
4363 </para>
4364
4365 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
4366 </listitem>
4367 </varlistentry>
4368
4369
4370
4371 <varlistentry>
4372 <term>getElementsByTagNameNS</term>
4373
4374 <listitem>
4375 <funcsynopsis>
4376 <funcsynopsisinfo>@nodes = $node-&gt;getElementsByTagNameNS($nsURI,$localname);</funcsynopsisinfo>
4377 </funcsynopsis>
4378
4379 <para>Namespace version of <function>getElementsByTagName</function> as found in the DOM spec.
4380 A special <function>localname</function> '*' can be used to match any local name
4381 and <function>nsURI</function> '*' can be used to match any namespace URI.</para>
4382
4383 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
4384 </listitem>
4385 </varlistentry>
4386
4387
4388
4389 <varlistentry>
4390 <term>getElementsByLocalName</term>
4391
4392 <listitem>
4393 <funcsynopsis>
4394 <funcsynopsisinfo>@nodes = $node-&gt;getElementsByLocalName($localname);</funcsynopsisinfo>
4395 </funcsynopsis>
4396
4397 <para>This function is not found in the DOM specification. It is a mix of getElementsByTagName and getElementsByTagNameNS. It will fetch all
4398 tags matching the given local-name. This allows one to select tags with the same local name across namespace borders.</para>
4399
4400 <para>In SCALAR context this function returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object.</para>
4401 </listitem>
4402 </varlistentry>
4403
4404
4405
4406 <varlistentry>
4407 <term>appendWellBalancedChunk</term>
4408
4409 <listitem>
4410 <funcsynopsis>
4411 <funcsynopsisinfo>$node-&gt;appendWellBalancedChunk( $chunk );</funcsynopsisinfo>
4412 </funcsynopsis>
4413
4414 <para>Sometimes it is necessary to append a string coded XML Tree to a node. <emphasis>appendWellBalancedChunk</emphasis> will do the trick
4415 for you. But this is only done if the String is <function>well-balanced</function>.</para>
4416
4417 <para><emphasis>Note that appendWellBalancedChunk() is only left for compatibility reasons</emphasis>. Implicitly it uses</para>
4418
4419 <programlisting> my $fragment = $parser-&gt;parse_balanced_chunk( $chunk );
4420 $node-&gt;appendChild( $fragment );</programlisting>
4421
4422 <para>This form is more explicit and makes it easier to control the flow of a script.</para>
4423 </listitem>
4424 </varlistentry>
4425
4426
4427
4428 <varlistentry>
4429 <term>appendText</term>
4430
4431 <listitem>
4432 <funcsynopsis>
4433 <funcsynopsisinfo>$node-&gt;appendText( $PCDATA );</funcsynopsisinfo>
4434 </funcsynopsis>
4435
4436 <para>alias for appendTextNode().</para>
4437 </listitem>
4438 </varlistentry>
4439
4440
4441
4442 <varlistentry>
4443 <term>appendTextNode</term>
4444
4445 <listitem>
4446 <funcsynopsis>
4447 <funcsynopsisinfo>$node-&gt;appendTextNode( $PCDATA );</funcsynopsisinfo>
4448 </funcsynopsis>
4449
4450 <para>This wrapper function lets you add a string directly to an element node.</para>
4451 </listitem>
4452 </varlistentry>
4453
4454
4455
4456 <varlistentry>
4457 <term>appendTextChild</term>
4458
4459 <listitem>
4460 <funcsynopsis>
4461 <funcsynopsisinfo>$node-&gt;appendTextChild( $childname , $PCDATA );</funcsynopsisinfo>
4462 </funcsynopsis>
4463
4464 <para>Somewhat similar with <function>appendTextNode</function>: It lets you set an Element, that contains only a <function>text node</function>
4465 directly by specifying the name and the text content.</para>
4466 </listitem>
4467 </varlistentry>
4468 <varlistentry>
4469 <term>setNamespace</term>
4470
4471 <listitem>
4472 <funcsynopsis>
4473 <funcsynopsisinfo>$node-&gt;setNamespace( $nsURI , $nsPrefix, $activate );</funcsynopsisinfo>
4474 </funcsynopsis>
4475
4476 <para>setNamespace() allows one to apply a
4477 namespace to an element. The function takes three
4478 parameters: 1. the namespace URI, which is
4479 required and the two optional values prefix, which
4480 is the namespace prefix, as it should be used in
4481 child elements or attributes as well as the
4482 additional activate parameter. If prefix is not given,
4483 undefined or empty, this function tries to create a
4484 declaration of the default namespace.
4485 </para>
4486 <para>The activate parameter is most useful: If
4487 this parameter is set to FALSE (0), a new namespace
4488 declaration is simply added to the element
4489 while the element's namespace itself is not
4490 altered. Nevertheless, activate is set to TRUE (1)
4491 on default. In this case the namespace
4492 is used as the node's effective
4493 namespace. This means the namespace prefix is
4494 added to the node name and if there was a
4495 namespace already active for the node, it will
4496 be replaced (but its declaration is not removed from the document).
4497 A new namespace declaration is only created if necessary
4498 (that is, if the element is already in the scope
4499 of a namespace declaration associating the prefix
4500 with the namespace URI, then this declaration is reused).
4501 </para>
4502
4503 <para>The following example may clarify this:</para>
4504
4505 <programlisting> my $e1 = $doc-&gt;createElement("bar");
4506 $e1-&gt;setNamespace("http://foobar.org", "foo")</programlisting>
4507
4508 <para>results</para>
4509
4510 <programlisting> &lt;foo:bar xmlns:foo="http://foobar.org"/&gt;</programlisting>
4511
4512 <para>while</para>
4513
4514 <programlisting> my $e2 = $doc-&gt;createElement("bar");
4515 $e2-&gt;setNamespace("http://foobar.org", "foo",0)</programlisting>
4516
4517 <para>results only</para>
4518
4519 <programlisting> &lt;bar xmlns:foo="http://foobar.org"/&gt;</programlisting>
4520
4521 <para>By using $activate == 0 it is possible to
4522 create multiple namespace declarations on a single
4523 element.</para>
4524 <para>The function fails if it is required to
4525 create a declaration associating the prefix
4526 with the namespace URI but the element already
4527 carries a declaration with the same prefix but
4528 different namespace URI.
4529 </para>
4530 </listitem>
4531 </varlistentry>
4532 <varlistentry>
4533 <term>setNamespaceDeclURI</term>
4534
4535 <listitem>
4536 <funcsynopsis>
4537 <funcsynopsisinfo>$node-&gt;setNamespaceDeclURI( $nsPrefix, $newURI );</funcsynopsisinfo>
4538 </funcsynopsis>
4539 <para>EXPERIMENTAL IN 1.61 !</para>
4540 <para>This function manipulates
4541 directly with an existing namespace
4542 declaration on an element. It takes
4543 two parameters: the prefix by which it
4544 looks up the namespace declaration and
4545 a new namespace URI which replaces its previous
4546 value.</para>
4547 <para>It returns 1 if the namespace declaration
4548 was found and changed, 0 otherwise.</para>
4549 <para>All elements and attributes (even those previously
4550 unbound from the document) for which the
4551 namespace declaration determines their namespace
4552 belong to the new namespace after
4553 the change.
4554 </para>
4555 <para>If the new URI is undef or empty, the nodes
4556 have no namespace and no prefix after the change.
4557 Namespace declarations
4558 once nulled in this way do not
4559 further appear in the serialized output
4560 (but do remain in the document for internal integrity
4561 of libxml2 data structures).
4562 </para>
4563 <para>This function is NOT part of any DOM API.</para>
4564 </listitem>
4565 </varlistentry>
4566 <varlistentry>
4567 <term>setNamespaceDeclPrefix</term>
4568 <listitem>
4569 <funcsynopsis>
4570 <funcsynopsisinfo>$node-&gt;setNamespaceDeclPrefix( $oldPrefix, $newPrefix );</funcsynopsisinfo>
4571 </funcsynopsis>
4572 <para>EXPERIMENTAL IN 1.61 !</para>
4573 <para>This function manipulates
4574 directly with an existing namespace
4575 declaration on an element. It takes
4576 two parameters: the old prefix by which it
4577 looks up the namespace declaration and
4578 a new prefix which is to replace the old one.</para>
4579 <para>The function dies with an error
4580 if the element is in the scope of
4581 another declaration whose prefix equals
4582 to the new prefix, or if the change should
4583 result in a declaration with a non-empty prefix but
4584 empty namespace URI.
4585 Otherwise, it returns 1 if the namespace declaration
4586 was found and changed and 0 if not found.</para>
4587 <para>All elements and attributes (even those previously
4588 unbound from the document) for which the
4589 namespace declaration determines their namespace
4590 change their prefix to the new value.
4591 </para>
4592 <para>If the new prefix is undef or empty,
4593 the namespace declaration becomes
4594 a declaration of a default namespace.
4595 The corresponding nodes drop their namespace prefix
4596 (but remain in the, now default, namespace).
4597 In this case the function fails, if the containing element
4598 is in the scope of another default namespace declaration.
4599 </para>
4600 <para>This function is NOT part of any DOM API.</para>
4601 </listitem>
4602 </varlistentry>
4603
4604 </variablelist>
4605 </sect1>
4606 <sect1>
4607 <title>Overloading</title>
4608 <para>XML::LibXML::Element overloads hash dereferencing to
4609 provide access to the element's attributes. For non-namespaced
4610 attributes, the attribute name is the hash key, and the attribute
4611 value is the hash value. For namespaced attributes, the hash key
4612 is qualified with the namespace URI, using Clark notation.</para>
4613 <para>Perl's "tied hash" feature is used, which means that the
4614 hash gives you read-write access to the element's attributes.
4615 For more information, see <olink targetdoc="XML::LibXML::AttributeHash"
4616 >XML::LibXML::AttributeHash</olink></para>
4617 </sect1>
4618 </chapter>
4619
4620 <chapter id="XML-LibXML-Text">
4621 <title>XML::LibXML Class for Text Nodes</title>
4622
4623 <titleabbrev>XML::LibXML::Text</titleabbrev>
4624 <sect1>
4625 <title>Synopsis</title>
4626 <programlisting>use XML::LibXML;
4627 # Only methods specific to Text nodes are listed here,
4628 # see the XML::LibXML::Node manpage for other methods</programlisting>
4629 </sect1>
4630 <sect1>
4631 <title>Description</title>
4632
4633 <para>Unlike the DOM specification, XML::LibXML implements the text node as the base class of all character data node. Therefore there exists no
4634 CharacterData class. This allows one to apply methods of text nodes also to Comments and CDATA-sections.</para>
4635 </sect1>
4636 <sect1>
4637 <title>Methods</title>
4638 <para>
4639 The class inherits from <xref linkend="XML-LibXML-Node"/>.
4640 The documentation for Inherited methods is not listed here.
4641 </para>
4642 <para>
4643 Many functions listed here are
4644 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
4645 the specification for extensive documentation.
4646 </para>
4647
4648 <variablelist>
4649 <varlistentry>
4650 <term>new</term>
4651
4652 <listitem>
4653 <funcsynopsis>
4654 <funcsynopsisinfo>$text = XML::LibXML::Text-&gt;new( $content ); </funcsynopsisinfo>
4655 </funcsynopsis>
4656
4657 <para>The constructor of the class. It creates an unbound text node.</para>
4658 </listitem>
4659 </varlistentry>
4660
4661
4662
4663 <varlistentry>
4664 <term>data</term>
4665
4666 <listitem>
4667 <funcsynopsis>
4668 <funcsynopsisinfo>$nodedata = $text-&gt;data;</funcsynopsisinfo>
4669 </funcsynopsis>
4670
4671 <para>Although there exists the <function>nodeValue</function> attribute in the Node class, the DOM specification defines data as a separate
4672 attribute. <function>XML::LibXML</function> implements these two attributes not as different attributes, but as aliases, such as
4673 <function>libxml2</function> does. Therefore</para>
4674
4675 <programlisting> $text-&gt;data;</programlisting>
4676
4677 <para>and</para>
4678
4679 <programlisting> $text-&gt;nodeValue;</programlisting>
4680
4681 <para>will have the same result and are not different entities.</para>
4682 </listitem>
4683 </varlistentry>
4684
4685
4686
4687 <varlistentry>
4688 <term>setData($string)</term>
4689
4690 <listitem>
4691 <funcsynopsis>
4692 <funcsynopsisinfo>$text-&gt;setData( $text_content );</funcsynopsisinfo>
4693 </funcsynopsis>
4694
4695 <para>This function sets or replaces text content to a node. The node has to be of the type "text", "cdata" or
4696 "comment".</para>
4697 </listitem>
4698 </varlistentry>
4699
4700
4701
4702 <varlistentry>
4703 <term>substringData($offset,$length)</term>
4704
4705 <listitem>
4706 <funcsynopsis>
4707 <funcsynopsisinfo>$text-&gt;substringData($offset, $length);</funcsynopsisinfo>
4708 </funcsynopsis>
4709
4710 <para>Extracts a range of data from the node. (DOM Spec) This function takes the two parameters $offset and $length and returns the
4711 sub-string, if available.</para>
4712
4713 <para>If the node contains no data or $offset refers to an non-existing string index, this function will return <emphasis>undef</emphasis>.
4714 If $length is out of range <function>substringData</function> will return the data starting at $offset instead of causing an error.</para>
4715 </listitem>
4716 </varlistentry>
4717
4718
4719
4720 <varlistentry>
4721 <term>appendData($string)</term>
4722
4723 <listitem>
4724 <funcsynopsis>
4725 <funcsynopsisinfo>$text-&gt;appendData( $somedata );</funcsynopsisinfo>
4726 </funcsynopsis>
4727
4728 <para>Appends a string to the end of the existing data. If the current text node contains no data, this function has the same effect as
4729 <function>setData</function>.</para>
4730 </listitem>
4731 </varlistentry>
4732
4733
4734
4735 <varlistentry>
4736 <term>insertData($offset,$string)</term>
4737
4738 <listitem>
4739 <funcsynopsis>
4740 <funcsynopsisinfo>$text-&gt;insertData($offset, $string);</funcsynopsisinfo>
4741 </funcsynopsis>
4742
4743 <para>Inserts the parameter $string at the given $offset of the existing data of the node. This operation will not remove existing data, but
4744 change the order of the existing data.</para>
4745
4746 <para>The $offset has to be a positive value. If $offset is out of range, <function>insertData</function> will have the same behaviour as
4747 <function>appendData</function>.</para>
4748 </listitem>
4749 </varlistentry>
4750
4751
4752
4753 <varlistentry>
4754 <term>deleteData($offset, $length)</term>
4755
4756 <listitem>
4757 <funcsynopsis>
4758 <funcsynopsisinfo>$text-&gt;deleteData($offset, $length);</funcsynopsisinfo>
4759 </funcsynopsis>
4760
4761 <para>This method removes a chunk from the existing node data at the given offset. The $length parameter tells, how many characters should
4762 be removed from the string.</para>
4763 </listitem>
4764 </varlistentry>
4765
4766
4767
4768 <varlistentry>
4769 <term>deleteDataString($string, [$all])</term>
4770
4771 <listitem>
4772 <funcsynopsis>
4773 <funcsynopsisinfo>$text-&gt;deleteDataString($remstring, $all);</funcsynopsisinfo>
4774 </funcsynopsis>
4775
4776 <para>This method removes a chunk from the existing node data. Since the DOM spec is quite unhandy if you already know <function>which</function>
4777 string to remove from a text node, this method allows more perlish code :)</para>
4778
4779 <para>The functions takes two parameters: <emphasis>$string</emphasis> and optional the <emphasis>$all</emphasis> flag. If $all is not set,
4780 <emphasis>undef</emphasis> or <emphasis>0</emphasis>, <function>deleteDataString</function> will remove only the first occurrence of
4781 $string. If $all is <emphasis>TRUE</emphasis> <function>deleteDataString</function> will remove all occurrences of <emphasis>$string</emphasis>
4782 from the node data.</para>
4783 </listitem>
4784 </varlistentry>
4785
4786
4787
4788 <varlistentry>
4789 <term>replaceData($offset, $length, $string)</term>
4790
4791 <listitem>
4792 <funcsynopsis>
4793 <funcsynopsisinfo>$text-&gt;replaceData($offset, $length, $string);</funcsynopsisinfo>
4794 </funcsynopsis>
4795
4796 <para>The DOM style version to replace node data.</para>
4797 </listitem>
4798 </varlistentry>
4799
4800
4801
4802 <varlistentry>
4803 <term>replaceDataString($oldstring, $newstring, [$all])</term>
4804
4805 <listitem>
4806 <funcsynopsis>
4807 <funcsynopsisinfo>$text-&gt;replaceDataString($old, $new, $flag);</funcsynopsisinfo>
4808 </funcsynopsis>
4809
4810 <para>The more programmer friendly version of replaceData() :)</para>
4811
4812 <para>Instead of giving offsets and length one can specify
4813 the exact string (<emphasis>$oldstring</emphasis>) to
4814 be replaced. Additionally the <emphasis>$all</emphasis>
4815 flag allows one to replace all occurrences of
4816 <emphasis>$oldstring</emphasis>.</para>
4817
4818 </listitem>
4819 </varlistentry>
4820
4821
4822
4823 <varlistentry>
4824 <term>replaceDataRegEx( $search_cond, $replace_cond, $reflags )</term>
4825
4826 <listitem>
4827 <funcsynopsis>
4828 <funcsynopsisinfo>$text-&gt;replaceDataRegEx( $search_cond, $replace_cond, $reflags );</funcsynopsisinfo>
4829 </funcsynopsis>
4830
4831 <para>This method replaces the node's data by a
4832 <function>simple</function> regular expression.
4833 Optional, this function allows one to pass some flags
4834 that will be added as flag to the replace
4835 statement.</para>
4836
4837 <para><emphasis>NOTE:</emphasis> This is a shortcut for</para>
4838
4839 <programlisting> my $datastr = $node-&gt;getData();
4840 $datastr =~ s/somecond/replacement/g; # 'g' is just an example for any flag
4841 $node-&gt;setData( $datastr );</programlisting>
4842
4843 <para>This function can make things easier to read for simple replacements. For more complex variants it is recommended to use the code
4844 snippet above.</para>
4845 </listitem>
4846 </varlistentry>
4847 </variablelist>
4848 </sect1>
4849 </chapter>
4850
4851 <chapter id="XML-LibXML-Comment">
4852 <title>XML::LibXML Comment Class</title>
4853
4854 <titleabbrev>XML::LibXML::Comment</titleabbrev>
4855 <sect1>
4856 <title>Synopsis</title>
4857 <programlisting>use XML::LibXML;
4858 # Only methods specific to Comment nodes are listed here,
4859 # see the XML::LibXML::Node manpage for other methods</programlisting>
4860 </sect1>
4861 <sect1>
4862 <title>Description</title>
4863
4864 <para>This class provides all functions of <xref linkend="XML-LibXML-Text"/>, but for comment nodes. This can be done, since only the output of the
4865 node types is different, but not the data structure. :-)</para>
4866 </sect1>
4867 <sect1>
4868 <title>Methods</title>
4869 <para>
4870 The class inherits from <xref linkend="XML-LibXML-Node"/>.
4871 The documentation for Inherited methods is not listed here.
4872 </para>
4873 <para>
4874 Many functions listed here are
4875 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
4876 the specification for extensive documentation.
4877 </para>
4878
4879 <variablelist>
4880 <varlistentry>
4881 <term>new</term>
4882
4883 <listitem>
4884 <funcsynopsis>
4885 <funcsynopsisinfo>$node = XML::LibXML::Comment-&gt;new( $content );</funcsynopsisinfo>
4886 </funcsynopsis>
4887
4888 <para>The constructor is the only provided function for this package. It is required, because <emphasis>libxml2</emphasis> treats text nodes
4889 and comment nodes slightly differently.</para>
4890 </listitem>
4891 </varlistentry>
4892 </variablelist>
4893 </sect1>
4894 </chapter>
4895
4896 <chapter id="XML-LibXML-CDATASection">
4897 <title>XML::LibXML Class for CDATA Sections</title>
4898
4899 <titleabbrev>XML::LibXML::CDATASection</titleabbrev>
4900 <sect1>
4901 <title>Synopsis</title>
4902 <programlisting>use XML::LibXML;
4903 # Only methods specific to CDATA nodes are listed here,
4904 # see the XML::LibXML::Node manpage for other methods</programlisting>
4905 </sect1>
4906 <sect1>
4907 <title>Description</title>
4908 <para>This class provides all functions of <xref linkend="XML-LibXML-Text"/>, but for CDATA nodes.</para>
4909 </sect1>
4910 <sect1>
4911 <title>Methods</title>
4912 <para>
4913 The class inherits from <xref linkend="XML-LibXML-Node"/>.
4914 The documentation for Inherited methods is not listed here.
4915 </para>
4916 <para>
4917 Many functions listed here are
4918 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
4919 the specification for extensive documentation.
4920 </para>
4921
4922
4923 <variablelist>
4924 <varlistentry>
4925 <term>new</term>
4926
4927 <listitem>
4928 <funcsynopsis>
4929 <funcsynopsisinfo>$node = XML::LibXML::CDATASection-&gt;new( $content );</funcsynopsisinfo>
4930 </funcsynopsis>
4931
4932 <para>The constructor is the only provided function for this package. It is required, because <emphasis>libxml2</emphasis> treats the
4933 different text node types slightly differently.</para>
4934 </listitem>
4935 </varlistentry>
4936 </variablelist>
4937 </sect1>
4938 </chapter>
4939
4940 <chapter id="XML-LibXML-Attr">
4941 <title>XML::LibXML Attribute Class</title>
4942
4943 <titleabbrev>XML::LibXML::Attr</titleabbrev>
4944 <sect1>
4945 <title>Synopsis</title>
4946 <programlisting>use XML::LibXML;
4947 # Only methods specific to Attribute nodes are listed here,
4948 # see the XML::LibXML::Node manpage for other methods</programlisting>
4949 </sect1>
4950 <sect1>
4951 <title>Description</title>
4952
4953 <para>This is the interface to handle Attributes like ordinary nodes. The naming of the class relies on the W3C DOM documentation.</para>
4954 </sect1>
4955 <sect1>
4956 <title>Methods</title>
4957 <para>
4958 The class inherits from <xref linkend="XML-LibXML-Node"/>.
4959 The documentation for Inherited methods is not listed here.
4960 </para>
4961 <para>
4962 Many functions listed here are
4963 extensively documented in the <ulink url="http://www.w3.org/TR/DOM-Level-3-Core/">DOM Level 3 specification</ulink>. Please refer to
4964 the specification for extensive documentation.
4965 </para>
4966 <variablelist>
4967 <varlistentry>
4968 <term>new</term>
4969
4970 <listitem>
4971 <funcsynopsis>
4972 <funcsynopsisinfo>$attr = XML::LibXML::Attr-&gt;new($name [,$value]);</funcsynopsisinfo>
4973 </funcsynopsis>
4974
4975 <para>Class constructor. If you need to work with ISO encoded strings, you should <emphasis>always</emphasis> use the <function>createAttribute</function>
4976 of <xref linkend="XML-LibXML-Document"/>.</para>
4977 </listitem>
4978 </varlistentry>
4979
4980 <varlistentry>
4981 <term>getValue</term>
4982
4983 <listitem>
4984 <funcsynopsis>
4985 <funcsynopsisinfo>$string = $attr-&gt;getValue();</funcsynopsisinfo>
4986 </funcsynopsis>
4987
4988 <para>Returns the value stored for the attribute. If undef is returned, the attribute has no value, which is different of being
4989 <function>not specified</function>.</para>
4990 </listitem>
4991 </varlistentry>
4992
4993 <varlistentry>
4994 <term>value</term>
4995
4996 <listitem>
4997 <funcsynopsis>
4998 <funcsynopsisinfo>$string = $attr-&gt;value;</funcsynopsisinfo>
4999 </funcsynopsis>
5000
5001 <para>Alias for <emphasis>getValue()</emphasis></para>
5002 </listitem>
5003 </varlistentry>
5004
5005
5006
5007 <varlistentry>
5008 <term>setValue</term>
5009
5010 <listitem>
5011 <funcsynopsis>
5012 <funcsynopsisinfo>$attr-&gt;setValue( $string );</funcsynopsisinfo>
5013 </funcsynopsis>
5014
5015 <para>This is needed to set a new attribute value. If ISO encoded strings are passed as parameter, the node has to be bound to a document,
5016 otherwise the encoding might be done incorrectly.</para>
5017 </listitem>
5018 </varlistentry>
5019
5020
5021
5022 <varlistentry>
5023 <term>getOwnerElement</term>
5024
5025 <listitem>
5026 <funcsynopsis>
5027 <funcsynopsisinfo>$node = $attr-&gt;getOwnerElement();</funcsynopsisinfo>
5028 </funcsynopsis>
5029
5030 <para>returns the node the attribute belongs to. If the attribute is not bound to a node, undef will be returned. Overwriting the underlying
5031 implementation, the <emphasis>parentNode</emphasis> function will return undef, instead of the owner element.</para>
5032 </listitem>
5033 </varlistentry>
5034
5035
5036
5037 <varlistentry>
5038 <term>setNamespace</term>
5039
5040 <listitem>
5041 <funcsynopsis>
5042 <funcsynopsisinfo>$attr-&gt;setNamespace($nsURI, $prefix);</funcsynopsisinfo>
5043 </funcsynopsis>
5044
5045 <para>This function tries to bound the attribute to a given namespace.
5046 If <function>$nsURI</function> is undefined or empty,
5047 the function discards any previous association of the attribute with a namespace.
5048 If the namespace was not previously declared in the context of the
5049 attribute, this function will fail.
5050 In this case you may wish to call setNamespace() on the ownerElement.
5051 If the namespace URI is non-empty and
5052 declared in the context of the attribute, but only with a different
5053 (non-empty) prefix, then the attribute is still bound to the namespace
5054 but gets a different prefix than <function>$prefix</function>.
5055 The function also fails if the prefix is empty but the namespace URI
5056 is not (because unprefixed attributes should by definition belong to
5057 no namespace).
5058 This function returns 1 on success, 0 otherwise.
5059 </para>
5060 </listitem>
5061 </varlistentry>
5062
5063
5064 <varlistentry>
5065 <term>isId</term>
5066 <listitem>
5067 <funcsynopsis>
5068 <funcsynopsisinfo>$bool = $attr-&gt;isId;</funcsynopsisinfo>
5069 </funcsynopsis>
5070 <para>Determine whether an attribute is of type
5071 ID. For documents with a DTD, this information
5072 is only available if DTD loading/validation has been requested.
5073 For HTML documents parsed with the HTML
5074 parser ID detection is done
5075 automatically. In XML documents, all "xml:id"
5076 attributes are considered to be of type ID.
5077 </para>
5078 </listitem>
5079 </varlistentry>
5080
5081 <varlistentry>
5082 <term>serializeContent($docencoding)</term>
5083
5084 <listitem>
5085 <funcsynopsis>
5086 <funcsynopsisinfo>$string = $attr-&gt;serializeContent;</funcsynopsisinfo>
5087 </funcsynopsis>
5088
5089 <para>This function is not part of DOM API. It returns attribute content
5090 in the form in which it serializes into XML, that is
5091 with all meta-characters properly quoted and with raw
5092 entity references (except for entities expanded during parse time).
5093 Setting the optional $docencoding flag to 1 enforces document
5094 encoding for the output string (which is then passed to Perl as a
5095 byte string). Otherwise the string is passed to Perl as (UTF-8 encoded)
5096 characters.
5097 </para>
5098 </listitem>
5099 </varlistentry>
5100
5101 </variablelist>
5102 </sect1>
5103 </chapter>
5104
5105 <chapter id="XML-LibXML-DocumentFragment">
5106 <title>XML::LibXML's DOM L2 Document Fragment Implementation</title>
5107
5108 <titleabbrev>XML::LibXML::DocumentFragment</titleabbrev>
5109 <sect1>
5110 <title>Synopsis</title>
5111 <programlisting>use XML::LibXML;</programlisting>
5112 </sect1>
5113 <sect1>
5114 <title>Description</title>
5115
5116 <para>This class is a helper class as described in the DOM Level 2 Specification. It is implemented as a node without name. All adding, inserting or
5117 replacing functions are aware of document fragments now.</para>
5118
5119 <para>As well <emphasis>all</emphasis> unbound nodes (all nodes that do not belong to any document sub-tree) are implicit members of document fragments.</para>
5120 </sect1>
5121 </chapter>
5122
5123 <chapter id="XML-LibXML-Namespace">
5124 <title>XML::LibXML Namespace Implementation</title>
5125
5126 <titleabbrev>XML::LibXML::Namespace</titleabbrev>
5127 <sect1>
5128 <title>Synopsis</title>
5129 <programlisting>use XML::LibXML;
5130 # Only methods specific to Namespace nodes are listed here,
5131 # see the XML::LibXML::Node manpage for other methods</programlisting>
5132 </sect1>
5133 <sect1>
5134 <title>Description</title>
5135
5136 <para>Namespace nodes are returned by both $element-&gt;findnodes('namespace::foo') or by $node-&gt;getNamespaces().</para>
5137
5138 <para>The namespace node API is not part of any current DOM API, and so it is quite minimal. It should be noted that namespace nodes are
5139 <emphasis>not</emphasis> a sub class of <xref linkend="XML-LibXML-Node"/>, however Namespace nodes act a lot like attribute nodes, and similarly named methods will
5140 return what you would expect if you treated the namespace node as an attribute. Note that in order to fix several inconsistencies between the API and the documentation, the behavior of some functions have been changed in 1.64.</para>
5141 </sect1>
5142 <sect1>
5143 <title>Methods</title>
5144 <variablelist>
5145 <varlistentry>
5146 <term>new</term>
5147
5148 <listitem>
5149 <funcsynopsis>
5150 <funcsynopsisinfo>my $ns = XML::LibXML::Namespace-&gt;new($nsURI);</funcsynopsisinfo>
5151 </funcsynopsis>
5152
5153 <para>Creates a new Namespace node. Note that this is not a 'node' as an attribute or an element node. Therefore you can't do
5154 call all <xref linkend="XML-LibXML-Node"/> Functions. All functions available for this node are listed below.</para>
5155
5156 <para>Optionally you can pass the prefix to the namespace constructor. If this second parameter is omitted you will create a so called
5157 default namespace. Note, the newly created namespace is not bound to any document or node, therefore you should not expect it to be
5158 available in an existing document.</para>
5159 </listitem>
5160 </varlistentry>
5161
5162 <varlistentry>
5163 <term>declaredURI</term>
5164 <listitem>
5165 <para>Returns the URI for this namespace.</para>
5166 </listitem>
5167 </varlistentry>
5168 <varlistentry>
5169 <term>declaredPrefix</term>
5170 <listitem>
5171 <para>Returns the prefix for this namespace.</para>
5172 </listitem>
5173 </varlistentry>
5174
5175 <varlistentry>
5176 <term>nodeName</term>
5177
5178 <listitem>
5179 <funcsynopsis>
5180 <funcsynopsisinfo>print $ns-&gt;nodeName();</funcsynopsisinfo>
5181 </funcsynopsis>
5182
5183 <para>Returns "xmlns:prefix", where prefix is the prefix for this namespace.</para>
5184 </listitem>
5185 </varlistentry>
5186 <varlistentry>
5187 <term>name</term>
5188
5189 <listitem>
5190 <funcsynopsis>
5191 <funcsynopsisinfo>print $ns-&gt;name();</funcsynopsisinfo>
5192 </funcsynopsis>
5193
5194 <para>Alias for nodeName()</para>
5195 </listitem>
5196 </varlistentry>
5197
5198 <varlistentry>
5199 <term>getLocalName</term>
5200
5201 <listitem>
5202 <funcsynopsis>
5203 <funcsynopsisinfo>$localname = $ns-&gt;getLocalName();</funcsynopsisinfo>
5204 </funcsynopsis>
5205
5206 <para>Returns the local name of this node as if it were an attribute, that is, the prefix associated with the namespace.</para>
5207 </listitem>
5208 </varlistentry>
5209
5210 <varlistentry>
5211 <term>getData</term>
5212
5213 <listitem>
5214 <funcsynopsis>
5215 <funcsynopsisinfo>print $ns-&gt;getData();</funcsynopsisinfo>
5216 </funcsynopsis>
5217
5218 <para>Returns the URI of the namespace, i.e. the value of this node as if it were an attribute.</para>
5219 </listitem>
5220 </varlistentry>
5221
5222 <varlistentry>
5223 <term>getValue</term>
5224
5225 <listitem>
5226 <funcsynopsis>
5227 <funcsynopsisinfo>print $ns-&gt;getValue();</funcsynopsisinfo>
5228 </funcsynopsis>
5229
5230 <para>Alias for getData()</para>
5231 </listitem>
5232 </varlistentry>
5233
5234 <varlistentry>
5235 <term>value</term>
5236
5237 <listitem>
5238 <funcsynopsis>
5239 <funcsynopsisinfo>print $ns-&gt;value();</funcsynopsisinfo>
5240 </funcsynopsis>
5241
5242 <para>Alias for getData()</para>
5243 </listitem>
5244 </varlistentry>
5245
5246 <varlistentry>
5247 <term>getNamespaceURI</term>
5248
5249 <listitem>
5250 <funcsynopsis>
5251 <funcsynopsisinfo>$known_uri = $ns-&gt;getNamespaceURI();</funcsynopsisinfo>
5252 </funcsynopsis>
5253
5254 <para>Returns the string "http://www.w3.org/2000/xmlns/"</para>
5255 </listitem>
5256 </varlistentry>
5257
5258 <varlistentry>
5259 <term>getPrefix</term>
5260
5261 <listitem>
5262 <funcsynopsis>
5263 <funcsynopsisinfo>$known_prefix = $ns-&gt;getPrefix();</funcsynopsisinfo>
5264 </funcsynopsis>
5265
5266 <para>Returns the string "xmlns"</para>
5267 </listitem>
5268 </varlistentry>
5269
5270 <varlistentry>
5271 <term>unique_key</term>
5272
5273 <listitem>
5274 <funcsynopsis>
5275 <funcsynopsisinfo>$key = $ns-&gt;unique_key();</funcsynopsisinfo>
5276 </funcsynopsis>
5277
5278 <para>This method returns a key guaranteed to be unique for this namespace, and to always be the same value for this namespace. Two namespace objects return the same key if and only if they have the same prefix and the same URI. The returned key value is useful as a key in hashes.</para>
5279 </listitem>
5280 </varlistentry>
5281 </variablelist>
5282 </sect1>
5283 </chapter>
5284
5285 <chapter id="XML-LibXML-PI">
5286 <title>XML::LibXML Processing Instructions</title>
5287
5288 <titleabbrev>XML::LibXML::PI</titleabbrev>
5289 <sect1>
5290 <title>Synopsis</title>
5291 <programlisting>use XML::LibXML;
5292 # Only methods specific to Processing Instruction nodes are listed here,
5293 # see the XML::LibXML::Node manpage for other methods</programlisting>
5294 </sect1>
5295 <sect1>
5296 <title>Description</title>
5297
5298 <para>Processing instructions are implemented with XML::LibXML with read and write access. The PI data is the PI without the PI target (as specified in
5299 XML 1.0 [17]) as a string. This string can be accessed with getData as implemented in <xref linkend="XML-LibXML-Node"/>.</para>
5300
5301 <para>The write access is aware about the fact, that many processing instructions have attribute like data. Therefore setData() provides besides the DOM
5302 spec conform Interface to pass a set of named parameter. So the code segment</para>
5303
5304 <programlisting>my $pi = $dom-&gt;createProcessingInstruction("abc");
5305 $pi-&gt;setData(foo=&gt;'bar', foobar=&gt;'foobar');
5306 $dom-&gt;appendChild( $pi );</programlisting>
5307
5308 <para>will result the following PI in the DOM:</para>
5309
5310 <programlisting>&lt;?abc foo="bar" foobar="foobar"?&gt;</programlisting>
5311
5312 <para>Which is how it is specified in the DOM specification. This three step interface creates temporary a node in perl space. This can be avoided while
5313 using the insertProcessingInstruction() method. Instead of the three calls described above, the call</para>
5314
5315 <programlisting>$dom-&gt;insertProcessingInstruction("abc",'foo="bar" foobar="foobar"');</programlisting>
5316
5317 <para>will have the same result as above.</para>
5318
5319 <para><xref linkend="XML-LibXML-PI"/>'s implementation of setData() documented below differs a bit from the standard version as available in <xref linkend="XML-LibXML-Node"/>:</para>
5320 <variablelist>
5321 <varlistentry>
5322 <term>setData</term>
5323
5324 <listitem>
5325 <funcsynopsis>
5326 <funcsynopsisinfo>$pinode-&gt;setData( $data_string );
5327 $pinode-&gt;setData( name=&gt;string_value [...] );</funcsynopsisinfo>
5328 </funcsynopsis>
5329
5330 <para>This method allows one to change the content data of
5331 a PI. Additionally to the interface specified for DOM
5332 Level2, the method provides a named parameter
5333 interface to set the data. This parameter list is
5334 converted into a string before it is appended to the
5335 PI.</para>
5336 </listitem>
5337 </varlistentry>
5338 </variablelist>
5339 </sect1>
5340 </chapter>
5341
5342 <chapter id="XML-LibXML-Dtd">
5343 <title>XML::LibXML DTD Handling</title>
5344
5345 <titleabbrev>XML::LibXML::Dtd</titleabbrev>
5346 <sect1>
5347 <title>Synopsis</title>
5348 <programlisting>use XML::LibXML;</programlisting>
5349 </sect1>
5350 <sect1>
5351 <title>Description</title>
5352
5353 <para>This class holds a DTD. You may parse a DTD from either a string, or from an external SYSTEM identifier.</para>
5354
5355 <para>No support is available as yet for parsing from a filehandle.</para>
5356
5357 <para>XML::LibXML::Dtd is a sub-class of <xref linkend="XML-LibXML-Node"/>, so all the methods available to nodes (particularly toString()) are available to Dtd objects.</para>
5358 </sect1>
5359 <sect1>
5360 <title>Methods</title>
5361
5362
5363 <variablelist>
5364 <varlistentry>
5365 <term>new</term>
5366
5367 <listitem>
5368 <funcsynopsis>
5369 <funcsynopsisinfo>$dtd = XML::LibXML::Dtd-&gt;new($public_id, $system_id);</funcsynopsisinfo>
5370 </funcsynopsis>
5371
5372 <para>Parse a DTD from the system identifier, and return a DTD object that you can pass to $doc-&gt;is_valid() or $doc-&gt;validate().</para>
5373
5374 <programlisting> my $dtd = XML::LibXML::Dtd-&gt;new(
5375 "SOME // Public / ID / 1.0",
5376 "test.dtd"
5377 );
5378 my $doc = XML::LibXML-&gt;new-&gt;parse_file("test.xml");
5379 $doc-&gt;validate($dtd);</programlisting>
5380 </listitem>
5381 </varlistentry>
5382
5383 <varlistentry>
5384 <term>parse_string</term>
5385
5386 <listitem>
5387 <funcsynopsis>
5388 <funcsynopsisinfo>$dtd = XML::LibXML::Dtd-&gt;parse_string($dtd_str);</funcsynopsisinfo>
5389 </funcsynopsis>
5390
5391 <para>The same as new() above, except you can parse a DTD from a string. Note that parsing from string may fail if the DTD contains external parametric-entity references with relative URLs.</para>
5392 </listitem>
5393 </varlistentry>
5394 <varlistentry>
5395 <term>getName</term>
5396
5397 <listitem>
5398 <funcsynopsis>
5399 <funcsynopsisinfo>$publicId = $dtd-&gt;getName();</funcsynopsisinfo>
5400 </funcsynopsis>
5401
5402 <para>Returns the name of DTD; i.e., the name immediately following the DOCTYPE keyword.</para>
5403 </listitem>
5404 </varlistentry>
5405 <varlistentry>
5406 <term>publicId</term>
5407
5408 <listitem>
5409 <funcsynopsis>
5410 <funcsynopsisinfo>$publicId = $dtd-&gt;publicId();</funcsynopsisinfo>
5411 </funcsynopsis>
5412
5413 <para>Returns the public identifier of the external subset.</para>
5414 </listitem>
5415 </varlistentry>
5416 <varlistentry>
5417 <term>systemId</term>
5418
5419 <listitem>
5420 <funcsynopsis>
5421 <funcsynopsisinfo>$systemId = $dtd-&gt;systemId();</funcsynopsisinfo>
5422 </funcsynopsis>
5423
5424 <para>Returns the system identifier of the external subset.</para>
5425 </listitem>
5426 </varlistentry>
5427 </variablelist>
5428 </sect1>
5429 </chapter>
5430
5431 <chapter id="XML-LibXML-InputCallback">
5432 <title>XML::LibXML Class for Input Callbacks</title>
5433
5434 <titleabbrev>XML::LibXML::InputCallback</titleabbrev>
5435 <sect1>
5436 <title>Synopsis</title>
5437 <programlisting>use XML::LibXML;</programlisting>
5438 </sect1>
5439
5440 <sect1>
5441 <title>Synopsis</title>
5442
5443 <programlisting>my $input_callbacks = XML::LibXML::InputCallback-&gt;new();
5444 $input_callbacks-&gt;register_callbacks([ $match_cb1, $open_cb1,
5445 $read_cb1, $close_cb1 ] );
5446 $input_callbacks-&gt;register_callbacks([ $match_cb2, $open_cb2,
5447 $read_cb2, $close_cb2 ] );
5448 $input_callbacks-&gt;register_callbacks( [ $match_cb3, $open_cb3,
5449 $read_cb3, $close_cb3 ] );
5450
5451 $parser-&gt;input_callbacks( $input_callbacks );
5452 $parser-&gt;parse_file( $some_xml_file );</programlisting>
5453 </sect1>
5454
5455 <sect1>
5456 <title>Description</title>
5457
5458 <para>You may get unexpected results if you are trying to load external documents during libxml2 parsing if the location of the resource is not a
5459 HTTP, FTP or relative location but a absolute path for example. To get around this limitation, you may add your own input handler to open, read and
5460 close particular types of locations or URI classes. Using this input callback handlers, you can handle your own custom URI schemes for example.</para>
5461
5462 <para>The input callbacks are used whenever XML::LibXML has to get something other than externally parsed entities from somewhere. They are implemented
5463 using a callback stack on the Perl layer in analogy to libxml2's native callback stack.</para>
5464
5465 <para>The XML::LibXML::InputCallback class transparently registers the input callbacks for the libxml2's parser processes.</para>
5466
5467 <sect2>
5468 <title>How does XML::LibXML::InputCallback work?</title>
5469
5470 <para>The libxml2 library offers a callback implementation as global functions only. To work-around the troubles resulting in having only global
5471 callbacks - for example, if the same global callback stack is manipulated by different applications running together in a single Apache
5472 Web-server environment -, XML::LibXML::InputCallback comes with a object-oriented and a function-oriented part.</para>
5473
5474 <para>Using the function-oriented part the global callback stack of libxml2 can be manipulated. Those functions can be used as interface to the
5475 callbacks on the C- and XS Layer. At the object-oriented part, operations for working with the "pseudo-localized" callback stack are
5476 implemented. Currently, you can register and de-register callbacks on the Perl layer and initialize them on a per parser basis.</para>
5477
5478 <sect3>
5479 <title>Callback Groups</title>
5480
5481 <para>The libxml2 input callbacks come in groups. One group contains a URI matcher (<emphasis>match</emphasis>), a data stream constructor (<emphasis>open</emphasis>),
5482 a data stream reader (<emphasis>read</emphasis>), and a data stream destructor (<emphasis>close</emphasis>). The callbacks can be
5483 manipulated on a per group basis only.</para>
5484 </sect3>
5485
5486 <sect3>
5487 <title>The Parser Process</title>
5488
5489 <para>The parser process works on an XML data stream, along which, links to other resources can be embedded. This can be links to external
5490 DTDs or XIncludes for example. Those resources are identified by URIs. The callback implementation of libxml2 assumes that one callback
5491 group can handle a certain amount of URIs and a certain URI scheme. Per default, callback handlers for <emphasis>file://*</emphasis>,
5492 <emphasis>file:://*.gz</emphasis>, <emphasis>http://*</emphasis> and <emphasis>ftp://*</emphasis> are registered.</para>
5493
5494 <para>Callback groups in the callback stack are processed from top to bottom, meaning that callback groups registered later will be
5495 processed before the earlier registered ones.</para>
5496
5497 <para>While parsing the data stream, the libxml2 parser checks if a registered callback group will handle a URI - if they will not, the URI
5498 will be interpreted as <emphasis>file://URI</emphasis>. To handle a URI, the <emphasis>match</emphasis> callback will have to return
5499 '1'. If that happens, the handling of the URI will be passed to that callback group. Next, the URI will be passed to the
5500 <emphasis>open</emphasis> callback, which should return a <emphasis>reference</emphasis> to the data stream if it successfully opened the
5501 file, '0' otherwise. If opening the stream was successful, the <emphasis>read</emphasis> callback will be called repeatedly until it
5502 returns an empty string. After the read callback, the <emphasis>close</emphasis> callback will be called to close the stream.</para>
5503 </sect3>
5504
5505 <sect3>
5506 <title>Organisation of callback groups in XML::LibXML::InputCallback</title>
5507
5508 <para>Callback groups are implemented as a stack (Array),
5509 each entry holds a reference to an array of the
5510 callbacks. For the libxml2 library, the
5511 XML::LibXML::InputCallback callback implementation
5512 appears as one single callback group. The Perl
5513 implementation however allows one to manage different
5514 callback stacks on a per libxml2-parser basis.</para>
5515 </sect3>
5516 </sect2>
5517
5518 <sect2>
5519 <title>Using XML::LibXML::InputCallback</title>
5520
5521 <para>After object instantiation using the parameter-less constructor, you can register callback groups.</para>
5522
5523 <programlisting>my $input_callbacks = XML::LibXML::InputCallback-&gt;new();
5524 $input_callbacks-&gt;register_callbacks([ $match_cb1, $open_cb1,
5525 $read_cb1, $close_cb1 ] );
5526 $input_callbacks-&gt;register_callbacks([ $match_cb2, $open_cb2,
5527 $read_cb2, $close_cb2 ] );
5528 $input_callbacks-&gt;register_callbacks( [ $match_cb3, $open_cb3,
5529 $read_cb3, $close_cb3 ] );
5530
5531 $parser-&gt;input_callbacks( $input_callbacks );
5532 $parser-&gt;parse_file( $some_xml_file );</programlisting>
5533 </sect2>
5534
5535 <sect2>
5536 <title>What about the old callback system prior to XML::LibXML::InputCallback?</title>
5537
5538 <para>In XML::LibXML versions prior to 1.59 - i.e. without the XML::LibXML::InputCallback module - you could define your callbacks either using
5539 globally or locally. You still can do that using XML::LibXML::InputCallback, and in addition to that you can define the callbacks on a per
5540 parser basis!</para>
5541
5542 <para>If you use the old callback interface through global callbacks, XML::LibXML::InputCallback will treat them with a lower priority as the
5543 ones registered using the new interface. The global callbacks will not override the callback groups registered using the new interface. Local
5544 callbacks are attached to a specific parser instance, therefore they are treated with highest priority. If the <emphasis>match</emphasis>
5545 callback of the callback group registered as local variable is identical to one of the callback groups registered using the new interface, that
5546 callback group will be replaced.</para>
5547
5548 <para>Users of the old callback implementation whose <emphasis>open</emphasis> callback returned a plain string, will have to adapt their code
5549 to return a reference to that string after upgrading to version &gt;= 1.59. The new callback system can only deal with the
5550 <emphasis>open</emphasis> callback returning a reference!</para>
5551 </sect2>
5552 </sect1>
5553
5554 <sect1>
5555 <title>Interface Description</title>
5556
5557 <sect2>
5558 <title>Global Variables</title>
5559
5560 <variablelist>
5561 <varlistentry>
5562 <term>$_CUR_CB</term>
5563
5564 <listitem>
5565 <para>Stores the current callback and can be used as shortcut to access the callback stack.</para>
5566 </listitem>
5567 </varlistentry>
5568
5569 <varlistentry>
5570 <term>@_GLOBAL_CALLBACKS</term>
5571
5572 <listitem>
5573 <para>Stores all callback groups for the current parser process.</para>
5574 </listitem>
5575 </varlistentry>
5576
5577 <varlistentry>
5578 <term>@_CB_STACK</term>
5579
5580 <listitem>
5581 <para>Stores the currently used callback group. Used to prevent parser errors when dealing with nested XML data.</para>
5582 </listitem>
5583 </varlistentry>
5584 </variablelist>
5585 </sect2>
5586
5587 <sect2>
5588 <title>Global Callbacks</title>
5589
5590 <variablelist>
5591 <varlistentry>
5592 <term>_callback_match</term>
5593
5594 <listitem>
5595 <para>Implements the interface for the <emphasis>match</emphasis> callback at C-level and for the selection of the callback group
5596 from the callbacks defined at the Perl-level.</para>
5597 </listitem>
5598 </varlistentry>
5599
5600 <varlistentry>
5601 <term>_callback_open</term>
5602
5603 <listitem>
5604 <para>Forwards the <emphasis>open</emphasis> callback from libxml2 to the corresponding callback function at the Perl-level.</para>
5605 </listitem>
5606 </varlistentry>
5607
5608 <varlistentry>
5609 <term>_callback_read</term>
5610
5611 <listitem>
5612 <para>Forwards the read request to the corresponding callback function at the Perl-level and returns the result to libxml2.</para>
5613 </listitem>
5614 </varlistentry>
5615
5616 <varlistentry>
5617 <term>_callback_close</term>
5618
5619 <listitem>
5620 <para>Forwards the <emphasis>close</emphasis> callback from libxml2 to the corresponding callback function at the Perl-level..</para>
5621 </listitem>
5622 </varlistentry>
5623 </variablelist>
5624 </sect2>
5625
5626 <sect2>
5627 <title>Class methods</title>
5628
5629 <variablelist>
5630 <varlistentry>
5631 <term>new()</term>
5632
5633 <listitem>
5634 <para>A simple constructor.</para>
5635 </listitem>
5636 </varlistentry>
5637
5638 <varlistentry>
5639 <term>register_callbacks( [ $match_cb, $open_cb, $read_cb, $close_cb ])</term>
5640
5641 <listitem>
5642 <para>The four callbacks <emphasis>have</emphasis> to be given as array reference in the above order <emphasis>match</emphasis>,
5643 <emphasis>open</emphasis>, <emphasis>read</emphasis>, <emphasis>close</emphasis>!</para>
5644 </listitem>
5645 </varlistentry>
5646
5647 <varlistentry>
5648 <term>unregister_callbacks( [ $match_cb, $open_cb, $read_cb, $close_cb ])</term>
5649
5650 <listitem>
5651 <para>With no arguments given, <function>unregister_callbacks()</function> will delete the last registered callback group from the
5652 stack. If four callbacks are passed as array reference, the callback group to unregister will be identified by the
5653 <emphasis>match</emphasis> callback and deleted from the callback stack. Note that if several identical <emphasis>match</emphasis>
5654 callbacks are defined in different callback groups, ALL of them will be deleted from the stack.</para>
5655 </listitem>
5656 </varlistentry>
5657
5658 <varlistentry>
5659 <term>init_callbacks( $parser )</term>
5660
5661 <listitem>
5662 <para>Initializes the callback system for the provided parser before starting a parsing process.</para>
5663 </listitem>
5664 </varlistentry>
5665
5666 <varlistentry>
5667 <term>cleanup_callbacks()</term>
5668
5669 <listitem>
5670 <para>Resets global variables and the libxml2 callback stack.</para>
5671 </listitem>
5672 </varlistentry>
5673
5674 <varlistentry>
5675 <term>lib_init_callbacks()</term>
5676
5677 <listitem>
5678 <para>Used internally for callback registration at C-level.</para>
5679 </listitem>
5680 </varlistentry>
5681
5682 <varlistentry>
5683 <term>lib_cleanup_callbacks()</term>
5684
5685 <listitem>
5686 <para>Used internally for callback resetting at the C-level.</para>
5687 </listitem>
5688 </varlistentry>
5689 </variablelist>
5690
5691 <para/>
5692 </sect2>
5693 </sect1>
5694
5695 <sect1>
5696 <title>Example callbacks</title>
5697
5698 <para>The following example is a purely fictitious example that uses a MyScheme::Handler object that responds to methods similar to an IO::Handle.</para>
5699
5700 <programlisting>
5701 # Define the four callback functions
5702 sub match_uri {
5703 my $uri = shift;
5704 return $uri =~ /^myscheme:/; # trigger our callback group at a 'myscheme' URIs
5705 }
5706
5707 sub open_uri {
5708 my $uri = shift;
5709 my $handler = MyScheme::Handler-&gt;new($uri);
5710 return $handler;
5711 }
5712
5713 # The returned $buffer will be parsed by the libxml2 parser
5714 sub read_uri {
5715 my $handler = shift;
5716 my $length = shift;
5717 my $buffer;
5718 read($handler, $buffer, $length);
5719 return $buffer; # $buffer will be an empty string '' if read() is done
5720 }
5721
5722 # Close the handle associated with the resource.
5723 sub close_uri {
5724 my $handler = shift;
5725 close($handler);
5726 }
5727
5728 # Register them with a instance of XML::LibXML::InputCallback
5729 my $input_callbacks = XML::LibXML::InputCallback-&gt;new();
5730 $input_callbacks-&gt;register_callbacks([ \&amp;match_uri, \&amp;open_uri,
5731 \&amp;read_uri, \&amp;close_uri ] );
5732
5733 # Register the callback group at a parser instance
5734 $parser-&gt;input_callbacks( $input_callbacks );
5735
5736 # $some_xml_file will be parsed using our callbacks
5737 $parser-&gt;parse_file( $some_xml_file );
5738
5739
5740 </programlisting>
5741 </sect1>
5742 </chapter>
5743
5744 <chapter id="XML-LibXML-RelaxNG">
5745 <title>RelaxNG Schema Validation</title>
5746
5747 <titleabbrev>XML::LibXML::RelaxNG</titleabbrev>
5748 <sect1>
5749 <title>Synopsis</title>
5750 <programlisting>use XML::LibXML;
5751 $doc = XML::LibXML-&gt;new-&gt;parse_file($url);</programlisting>
5752 </sect1>
5753 <sect1>
5754 <title>Description</title>
5755
5756 <para>The XML::LibXML::RelaxNG class is a tiny frontend to libxml2's RelaxNG implementation. Currently it supports only schema parsing and document
5757 validation.</para>
5758 </sect1>
5759 <sect1>
5760 <title>Methods</title>
5761
5762 <variablelist>
5763 <varlistentry>
5764 <term>new</term>
5765
5766 <listitem>
5767 <funcsynopsis>
5768 <funcsynopsisinfo>$rngschema = XML::LibXML::RelaxNG-&gt;new( location =&gt; $filename_or_url );
5769 $rngschema = XML::LibXML::RelaxNG-&gt;new( string =&gt; $xmlschemastring );
5770 $rngschema = XML::LibXML::RelaxNG-&gt;new( DOM =&gt; $doc );</funcsynopsisinfo>
5771 </funcsynopsis>
5772
5773 <para>The constructor of XML::LibXML::RelaxNG may get called with either one of three parameters. The parameter tells the class from which
5774 source it should generate a validation schema. It is important, that each schema only have a single source.</para>
5775
5776 <para>The location parameter allows one to parse a schema
5777 from the filesystem or a URL.</para>
5778
5779 <para>The string parameter will parse the schema from the given XML string.</para>
5780
5781 <para>The DOM parameter allows one to parse the schema from a pre-parsed <xref linkend="XML-LibXML-Document"/>.</para>
5782
5783 <para>Note that the constructor will die() if the schema does not meed the constraints of the RelaxNG specification.</para>
5784 </listitem>
5785 </varlistentry>
5786
5787 <varlistentry>
5788 <term>validate</term>
5789
5790 <listitem>
5791 <funcsynopsis>
5792 <funcsynopsisinfo>eval { $rngschema-&gt;validate( $doc ); };</funcsynopsisinfo>
5793 </funcsynopsis>
5794
5795 <para>This function allows one to validate a (parsed)
5796 document against the given RelaxNG schema. The argument
5797 of this function should be an XML::LibXML::Document
5798 object. If this function succeeds, it will return 0,
5799 otherwise it will die() and report the errors found.
5800 Because of this validate() should be always
5801 evaluated.</para>
5802
5803 </listitem>
5804 </varlistentry>
5805 </variablelist>
5806 </sect1>
5807 </chapter>
5808
5809 <chapter id="XML-LibXML-Schema">
5810 <title>XML Schema Validation</title>
5811
5812 <titleabbrev>XML::LibXML::Schema</titleabbrev>
5813 <sect1>
5814 <title>Synopsis</title>
5815 <programlisting>use XML::LibXML;
5816 $doc = XML::LibXML-&gt;new-&gt;parse_file($url);</programlisting>
5817 </sect1>
5818 <sect1>
5819 <title>Description</title>
5820
5821 <para>The XML::LibXML::Schema class is a tiny frontend to libxml2's XML Schema implementation. Currently it supports only schema parsing and
5822 document validation. As of 2.6.32, libxml2 only supports decimal types up to 24 digits (the standard requires at least 18).
5823 </para>
5824 </sect1>
5825 <sect1>
5826 <title>Methods</title>
5827 <variablelist>
5828 <varlistentry>
5829 <term>new</term>
5830
5831 <listitem>
5832 <funcsynopsis>
5833 <funcsynopsisinfo>$xmlschema = XML::LibXML::Schema-&gt;new( location =&gt; $filename_or_url );
5834 $xmlschema = XML::LibXML::Schema-&gt;new( string =&gt; $xmlschemastring );</funcsynopsisinfo>
5835 </funcsynopsis>
5836
5837 <para>The constructor of XML::LibXML::Schema may get called with either one of two parameters. The parameter tells the class from which
5838 source it should generate a validation schema. It is important, that each schema only have a single source.</para>
5839
5840 <para>The location parameter allows one to parse a schema
5841 from the filesystem or a URL.</para>
5842
5843 <para>The string parameter will parse the schema from the given XML string.</para>
5844
5845 <para>Note that the constructor will die() if the schema does not meed the constraints of the XML Schema specification.</para>
5846 </listitem>
5847 </varlistentry>
5848
5849 <varlistentry>
5850 <term>validate</term>
5851
5852 <listitem>
5853 <funcsynopsis>
5854 <funcsynopsisinfo>eval { $xmlschema-&gt;validate( $doc ); };</funcsynopsisinfo>
5855 </funcsynopsis>
5856
5857 <para>This function allows one to validate a (parsed)
5858 document against the given XML Schema. The argument of
5859 this function should be a <xref
5860 linkend="XML-LibXML-Document"/> object. If this
5861 function succeeds, it will return 0, otherwise it will
5862 die() and report the errors found. Because of this
5863 validate() should be always evaluated.</para>
5864
5865 </listitem>
5866 </varlistentry>
5867 </variablelist>
5868 </sect1>
5869 </chapter>
5870 <chapter id="XML-LibXML-XPathContext">
5871 <title>XPath Evaluation</title>
5872 <titleabbrev>XML::LibXML::XPathContext</titleabbrev>
5873 <sect1>
5874 <title>Description</title>
5875 <para>
5876 The XML::LibXML::XPathContext
5877 class provides an almost complete
5878 interface to libxml2's XPath implementation.
5879 With XML::LibXML::XPathContext, it is possible to
5880 evaluate XPath expressions in the context
5881 of arbitrary node, context size, and context position,
5882 with a user-defined namespace-prefix mapping,
5883 custom XPath functions written in Perl, and
5884 even a custom XPath variable resolver.
5885 </para>
5886 </sect1>
5887 <sect1>
5888 <title>Examples</title>
5889 <sect2>
5890 <title>Namespaces</title>
5891 <para>This example demonstrates <function>registerNs()</function> method.
5892 It finds all paragraph nodes in an XHTML document.</para>
5893 <programlisting>my $xc = XML::LibXML::XPathContext-&gt;new($xhtml_doc);
5894 $xc-&gt;registerNs('xhtml', 'http://www.w3.org/1999/xhtml');
5895 my @nodes = $xc-&gt;findnodes('//xhtml:p');</programlisting>
5896 </sect2>
5897 <sect2>
5898 <title>Custom XPath functions</title>
5899 <para>This example demonstrates <function>registerFunction()</function> method
5900 by defining a function filtering nodes based on a Perl regular expression:</para>
5901 <programlisting>sub grep_nodes {
5902 my ($nodelist,$regexp) = @_;
5903 my $result = XML::LibXML::NodeList-&gt;new;
5904 for my $node ($nodelist-&gt;get_nodelist()) {
5905 $result-&gt;push($node) if $node-&gt;textContent =~ $regexp;
5906 }
5907 return $result;
5908 };
5909
5910 my $xc = XML::LibXML::XPathContext-&gt;new($node);
5911 $xc-&gt;registerFunction('grep_nodes', \&amp;grep_nodes);
5912 my @nodes = $xc-&gt;findnodes('//section[grep_nodes(para,"\bsearch(ing|es)?\b")]');</programlisting>
5913 </sect2>
5914 <sect2>
5915 <title>Variables</title>
5916 <para>This example demonstrates <function>registerVarLookup()</function>
5917 method. We use XPath variables to recycle results of previous evaluations:</para>
5918 <programlisting>sub var_lookup {
5919 my ($varname,$ns,$data)=@_;
5920 return $data-&gt;{$varname};
5921 }
5922
5923 my $areas = XML::LibXML-&gt;new-&gt;parse_file('areas.xml');
5924 my $empl = XML::LibXML-&gt;new-&gt;parse_file('employees.xml');
5925
5926 my $xc = XML::LibXML::XPathContext-&gt;new($empl);
5927
5928 my %variables = (
5929 A =&gt; $xc-&gt;find('/employees/employee[@salary&gt;10000]'),
5930 B =&gt; $areas-&gt;find('/areas/area[district='Brooklyn']/street'),
5931 );
5932
5933 # get names of employees from $A working in an area listed in $B
5934 $xc-&gt;registerVarLookupFunc(\&amp;var_lookup, \%variables);
5935 my @nodes = $xc-&gt;findnodes('$A[work_area/street = $B]/name');
5936 </programlisting>
5937 </sect2>
5938 </sect1>
5939 <sect1>
5940 <title>Methods</title>
5941 <variablelist>
5942 <varlistentry>
5943 <term>new</term>
5944 <listitem>
5945 <funcsynopsis><funcsynopsisinfo>my $xpc = XML::LibXML::XPathContext-&gt;new();</funcsynopsisinfo></funcsynopsis>
5946 <para>Creates a new XML::LibXML::XPathContext object
5947 without a context node.</para>
5948 <funcsynopsis><funcsynopsisinfo>my $xpc = XML::LibXML::XPathContext-&gt;new($node);</funcsynopsisinfo></funcsynopsis>
5949 <para>Creates a new XML::LibXML::XPathContext object with
5950 the context node set to <literal>$node</literal>.</para>
5951 </listitem>
5952 </varlistentry>
5953 <varlistentry>
5954 <term>registerNs</term>
5955 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;registerNs($prefix, $namespace_uri)</funcsynopsisinfo></funcsynopsis>
5956 <para>Registers namespace <literal>$prefix</literal> to
5957 <literal>$namespace_uri</literal>.</para>
5958 </listitem>
5959 </varlistentry>
5960 <varlistentry>
5961 <term>unregisterNs</term>
5962 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;unregisterNs($prefix)</funcsynopsisinfo></funcsynopsis>
5963 <para>Unregisters namespace <literal>$prefix</literal>.</para>
5964 </listitem>
5965 </varlistentry>
5966 <varlistentry>
5967 <term>lookupNs</term>
5968 <listitem><funcsynopsis><funcsynopsisinfo>$uri = $xpc-&gt;lookupNs($prefix)</funcsynopsisinfo></funcsynopsis>
5969 <para>Returns namespace URI registered with
5970 <literal>$prefix</literal>. If <literal>$prefix</literal>
5971 is not registered to any namespace URI returns
5972 <literal>undef</literal>.</para>
5973 </listitem>
5974 </varlistentry>
5975 <varlistentry>
5976 <term>registerVarLookupFunc</term>
5977 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;registerVarLookupFunc($callback, $data)</funcsynopsisinfo></funcsynopsis>
5978 <para>Registers variable lookup function
5979 <literal>$prefix</literal>. The registered function is
5980 executed by the XPath engine each time an XPath variable
5981 is evaluated. It takes three arguments:
5982 <literal>$data</literal>, variable name, and variable
5983 ns-URI and must return one value: a number or string or
5984 any <literal>XML::LibXML::</literal> object that can be a result
5985 of findnodes: Boolean, Literal, Number, Node
5986 (e.g. Document, Element, etc.), or NodeList. For
5987 convenience, simple (non-blessed) array references
5988 containing only <xref linkend="XML-LibXML-Node"/> objects can be
5989 used instead of an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink>.</para>
5990 </listitem>
5991 </varlistentry>
5992 <varlistentry>
5993 <term>getVarLookupData</term>
5994 <listitem>
5995 <funcsynopsis><funcsynopsisinfo>$data = $xpc-&gt;getVarLookupData();</funcsynopsisinfo></funcsynopsis>
5996 <para>
5997 Returns the data that have been associated with a
5998 variable lookup function during a previous call to
5999 <literal>registerVarLookupFunc</literal>.</para>
6000 </listitem>
6001 </varlistentry>
6002 <varlistentry>
6003 <term>getVarLookupFunc</term>
6004 <listitem>
6005 <funcsynopsis><funcsynopsisinfo>$callback = $xpc-&gt;getVarLookupFunc();</funcsynopsisinfo></funcsynopsis>
6006 <para>
6007 Returns the variable lookup function previously registered with
6008 <literal>registerVarLookupFunc</literal>.</para>
6009 </listitem>
6010 </varlistentry>
6011 <varlistentry>
6012 <term>unregisterVarLookupFunc</term>
6013 <listitem>
6014 <funcsynopsis><funcsynopsisinfo>$xpc-&gt;unregisterVarLookupFunc($name);</funcsynopsisinfo></funcsynopsis>
6015 <para>Unregisters variable lookup function and the associated lookup data.</para>
6016 </listitem>
6017 </varlistentry>
6018 <varlistentry>
6019 <term>registerFunctionNS</term>
6020 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;registerFunctionNS($name, $uri, $callback)</funcsynopsisinfo></funcsynopsis>
6021 <para>Registers an extension function
6022 <literal>$name</literal> in <literal>$uri</literal>
6023 namespace. <literal>$callback</literal> must be a CODE
6024 reference. The arguments of the callback function are
6025 either simple scalars or <literal>XML::LibXML::*</literal> objects
6026 depending on the XPath argument types. The function is
6027 responsible for checking the argument number and
6028 types. Result of the callback code must be a single
6029 value of the following types: a simple scalar
6030 (number, string) or an arbitrary <literal>XML::LibXML::*</literal>
6031 object that can be a result of findnodes: Boolean,
6032 Literal, Number, Node (e.g. Document, Element, etc.), or
6033 NodeList. For convenience, simple (non-blessed) array
6034 references containing only <xref linkend="XML-LibXML-Node"/>
6035 objects can be used instead of a
6036 <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink>.</para>
6037 </listitem>
6038 </varlistentry>
6039 <varlistentry>
6040 <term>unregisterFunctionNS</term>
6041 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;unregisterFunctionNS($name, $uri)</funcsynopsisinfo></funcsynopsis>
6042 <para>
6043 Unregisters extension function <literal>$name</literal>
6044 in <literal>$uri</literal> namespace. Has the same
6045 effect as passing <literal>undef</literal> as
6046 <literal>$callback</literal> to
6047 registerFunctionNS.</para>
6048 </listitem>
6049 </varlistentry>
6050 <varlistentry>
6051 <term>registerFunction</term>
6052 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;registerFunction($name, $callback)</funcsynopsisinfo></funcsynopsis>
6053 <para>Same as <literal>registerFunctionNS</literal> but
6054 without a namespace.</para>
6055 </listitem>
6056 </varlistentry>
6057 <varlistentry>
6058 <term>unregisterFunction</term>
6059 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;unregisterFunction($name)</funcsynopsisinfo></funcsynopsis>
6060 <para>Same as <literal>unregisterFunctionNS</literal> but
6061 without a namespace.</para>
6062 </listitem>
6063 </varlistentry>
6064 <varlistentry>
6065 <term>findnodes</term>
6066 <listitem><funcsynopsis><funcsynopsisinfo>@nodes = $xpc-&gt;findnodes($xpath)</funcsynopsisinfo></funcsynopsis>
6067 <funcsynopsis><funcsynopsisinfo>@nodes = $xpc-&gt;findnodes($xpath, $context_node )</funcsynopsisinfo></funcsynopsis>
6068 <funcsynopsis><funcsynopsisinfo>$nodelist = $xpc-&gt;findnodes($xpath, $context_node )</funcsynopsisinfo></funcsynopsis>
6069 <para>Performs the xpath statement on the current node and
6070 returns the result as an array. In scalar context,
6071 returns an <olink targetdoc="XML::LibXML::NodeList">XML::LibXML::NodeList</olink> object. Optionally, a
6072 node may be passed as a second argument to set the
6073 context node for the query.</para>
6074 <para>The xpath expression can be passed either as a string, or
6075 as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
6076 </para>
6077 </listitem>
6078 </varlistentry>
6079 <varlistentry>
6080 <term>find</term>
6081 <listitem><funcsynopsis><funcsynopsisinfo>$object = $xpc-&gt;find($xpath )</funcsynopsisinfo></funcsynopsis>
6082 <funcsynopsis><funcsynopsisinfo>$object = $xpc-&gt;find($xpath, $context_node )</funcsynopsisinfo></funcsynopsis>
6083 <para>Performs the xpath expression using the current node
6084 as the context of the expression, and returns the result
6085 depending on what type of result the XPath expression
6086 had. For example, the XPath <literal>1 * 3 +
6087 52</literal> results in an <olink targetdoc="XML::LibXML::Number">XML::LibXML::Number</olink> object
6088 being returned. Other expressions might return a
6089 <olink targetdoc="XML::LibXML::Boolean">XML::LibXML::Boolean</olink> object, or a
6090 <olink targetdoc="XML::LibXML::Literal">XML::LibXML::Literal</olink> object (a string). Each of those
6091 objects uses Perl's overload feature to ``do the right
6092 thing'' in different contexts. Optionally, a node may be
6093 passed as a second argument to set the context node for
6094 the query.</para>
6095 <para>The xpath expression can be passed either as a string, or
6096 as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
6097 </para>
6098 </listitem>
6099 </varlistentry>
6100 <varlistentry>
6101 <term>findvalue</term>
6102 <listitem><funcsynopsis><funcsynopsisinfo>$value = $xpc-&gt;findvalue($xpath )</funcsynopsisinfo></funcsynopsis>
6103 <funcsynopsis><funcsynopsisinfo>$value = $xpc-&gt;findvalue($xpath, $context_node )</funcsynopsisinfo></funcsynopsis>
6104 <para>Is exactly equivalent to:</para>
6105 <programlisting>$xpc-&gt;find( $xpath, $context_node )-&gt;to_literal;</programlisting>
6106 <para>That is, it returns the literal value of the
6107 results. This enables you to ensure that you get a string
6108 back from your search, allowing certain shortcuts. This
6109 could be used as the equivalent of &lt;xsl:value-of
6110 select=``some_xpath''/&gt;. Optionally, a node may be
6111 passed in the second argument to set the context node for
6112 the query.</para>
6113 <para>The xpath expression can be passed either as a string, or
6114 as a <olink targetdoc="XML::LibXML::XPathExpression">XML::LibXML::XPathExpression</olink> object.
6115 </para>
6116 </listitem>
6117 </varlistentry>
6118 <varlistentry>
6119 <term>exists</term>
6120 <listitem>
6121 <funcsynopsis>
6122 <funcsynopsisinfo>$bool = $xpc-&gt;exists( $xpath_expression, $context_node );</funcsynopsisinfo>
6123 </funcsynopsis>
6124 <para>This method behaves like <emphasis>findnodes</emphasis>, except
6125 that it only returns a boolean value (1 if the expression matches a node, 0 otherwise)
6126 and may be faster than <emphasis>findnodes</emphasis>, because
6127 the XPath evaluation may stop early on the first match (this is true for libxml2 >= 2.6.27).
6128 </para><para>For XPath expressions that do not return node-set,
6129 the method returns true if the returned value is a non-zero number or a non-empty string.</para>
6130 </listitem>
6131 </varlistentry>
6132 <varlistentry>
6133 <term>setContextNode</term>
6134 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;setContextNode($node)</funcsynopsisinfo></funcsynopsis>
6135 <para>Set the current context node.</para>
6136 </listitem>
6137 </varlistentry>
6138 <varlistentry>
6139 <term>getContextNode</term>
6140 <listitem>
6141 <funcsynopsis><funcsynopsisinfo>my $node = $xpc-&gt;getContextNode;</funcsynopsisinfo></funcsynopsis>
6142 <para>Get the current context node.</para>
6143 </listitem>
6144 </varlistentry>
6145 <varlistentry>
6146 <term>setContextPosition</term>
6147 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;setContextPosition($position)</funcsynopsisinfo></funcsynopsis>
6148 <para>
6149 Set the current context position. By default, this
6150 value is -1 (and evaluating XPath function
6151 <literal>position()</literal> in the initial context
6152 raises an XPath error), but can be set to any value up
6153 to context size. This usually only serves to cheat the
6154 XPath engine to return given position when
6155 <literal>position()</literal> XPath function is
6156 called. Setting this value to -1 restores the default
6157 behavior.</para>
6158 </listitem>
6159 </varlistentry>
6160 <varlistentry>
6161 <term>getContextPosition</term>
6162 <listitem>
6163 <funcsynopsis><funcsynopsisinfo>my $position = $xpc-&gt;getContextPosition;</funcsynopsisinfo></funcsynopsis>
6164 <para>Get the current context position.</para>
6165 </listitem>
6166 </varlistentry>
6167 <varlistentry>
6168 <term>setContextSize</term>
6169 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;setContextSize($size)</funcsynopsisinfo></funcsynopsis>
6170 <para>
6171 Set the current context size. By default, this value is -1 (and
6172 evaluating XPath function <literal>last()</literal> in
6173 the initial context raises an XPath error), but can be
6174 set to any non-negative value. This usually only serves
6175 to cheat the XPath engine to return the given value when
6176 <literal>last()</literal> XPath function is called. If
6177 context size is set to 0, position is automatically also
6178 set to 0. If context size is positive, position is
6179 automatically set to 1. Setting context size to -1
6180 restores the default behavior.</para>
6181 </listitem>
6182 </varlistentry>
6183 <varlistentry>
6184 <term>getContextSize</term>
6185 <listitem>
6186 <funcsynopsis><funcsynopsisinfo>my $size = $xpc-&gt;getContextSize;</funcsynopsisinfo></funcsynopsis>
6187 <para>Get the current context size.</para>
6188 </listitem>
6189 </varlistentry>
6190 <varlistentry>
6191 <term>setContextNode</term>
6192 <listitem><funcsynopsis><funcsynopsisinfo>$xpc-&gt;setContextNode($node)</funcsynopsisinfo></funcsynopsis>
6193 <para>Set the current context node.</para>
6194 </listitem>
6195 </varlistentry>
6196 </variablelist>
6197 </sect1>
6198 <sect1>
6199 <title>Bugs And Caveats</title>
6200 <para>
6201 XML::LibXML::XPathContext objects
6202 <emphasis>are</emphasis> reentrant, meaning that you can call
6203 methods of an XML::LibXML::XPathContext even from XPath
6204 extension functions registered with the same object or from a
6205 variable lookup function. On the other hand, you should rather
6206 avoid registering new extension functions, namespaces and a
6207 variable lookup function from within extension functions and a
6208 variable lookup function, unless you want to experience
6209 untested behavior.
6210 </para>
6211 </sect1>
6212 <sect1>
6213 <title>Authors</title>
6214 <para>Ilya Martynov and Petr Pajas, based on
6215 XML::LibXML and XML::LibXSLT code by Matt Sergeant and
6216 Christian Glahn.</para>
6217 </sect1>
6218 <sect1>
6219 <title>Historical remark</title>
6220 <para>Prior to XML::LibXML 1.61 this module was distributed separately
6221 for maintenance reasons.
6222 </para>
6223 </sect1>
6224 </chapter>
6225 <chapter id="XML-LibXML-Reader">
6226 <title>XML::LibXML::Reader - interface to libxml2 pull parser</title>
6227 <titleabbrev>XML::LibXML::Reader</titleabbrev>
6228 <sect1>
6229 <title>Synopsis</title>
6230 <programlisting>use XML::LibXML::Reader;</programlisting>
6231 <programlisting>my $reader = XML::LibXML::Reader-&gt;new(location =&gt; "file.xml")
6232 or die "cannot read file.xml\n";
6233 while ($reader-&gt;read) {
6234 processNode($reader);
6235 }</programlisting>
6236 <programlisting>
6237 sub processNode {
6238 my $reader = shift;
6239 printf "%d %d %s %d\n", ($reader-&gt;depth,
6240 $reader-&gt;nodeType,
6241 $reader-&gt;name,
6242 $reader-&gt;isEmptyElement);
6243 }
6244 </programlisting>
6245 <para>or</para>
6246 <programlisting>
6247 my $reader = XML::LibXML::Reader-&gt;new(location =&gt; "file.xml")
6248 or die "cannot read file.xml\n";
6249 $reader-&gt;preservePattern('//table/tr');
6250 $reader-&gt;finish;
6251 print $reader-&gt;document-&gt;toString(1);
6252 </programlisting>
6253 </sect1>
6254 <sect1>
6255 <title>DESCRIPTION</title>
6256 <para>This is a perl interface to libxml2's pull-parser implementation
6257 xmlTextReader
6258 <emphasis>http://xmlsoft.org/html/libxml-xmlreader.html</emphasis>.
6259 This feature requires at least libxml2-2.6.21.
6260 Pull-parsers (such as StAX in Java, or XmlReader in C#) use an iterator
6261 approach to parse XML documents. They are easier to program than
6262 event-based parser (SAX) and much more lightweight than
6263 tree-based parser (DOM), which load the complete tree into
6264 memory.</para>
6265 <para>The Reader acts as a cursor going forward on the document
6266 stream and stopping at each node on the way. At every point,
6267 the DOM-like methods of the Reader object allow one to examine the
6268 current node (name, namespace, attributes, etc.)</para>
6269 <para>The user's code keeps control of the progress and simply
6270 calls the <literal>read()</literal> function repeatedly to
6271 progress to the next node in the document order. Other
6272 functions provide means for skipping complete sub-trees, or
6273 nodes until a specific element, etc.</para>
6274 <para>At every time, only a very limited portion of the
6275 document is kept in the memory, which makes the API more
6276 memory-efficient than using DOM. However, it is also possible
6277 to mix Reader with DOM. At every point the user may copy the
6278 current node (optionally expanded into a complete sub-tree)
6279 from the processed document to another DOM tree, or to
6280 instruct the Reader to collect sub-document in form of a DOM
6281 tree consisting of selected nodes.</para>
6282 <para>Reader API also supports namespaces, xml:base, entity
6283 handling, and DTD validation. Schema and RelaxNG validation
6284 support will probably be added in some later revision of the
6285 Perl interface.</para>
6286 <para>The naming of methods compared to libxml2 and C#
6287 XmlTextReader has been changed slightly to match the
6288 conventions of XML::LibXML. Some functions have been changed
6289 or added with respect to the C interface.</para>
6290 </sect1>
6291 <sect1>
6292 <title>CONSTRUCTOR</title>
6293 <para>Depending on the XML source, the Reader object can be created with either of:</para>
6294 <programlisting>
6295 my $reader = XML::LibXML::Reader-&gt;new( location =&gt; "file.xml", ... );
6296 my $reader = XML::LibXML::Reader-&gt;new( string =&gt; $xml_string, ... );
6297 my $reader = XML::LibXML::Reader-&gt;new( IO =&gt; $file_handle, ... );
6298 my $reader = XML::LibXML::Reader-&gt;new( FD =&gt; fileno(STDIN), ... );
6299 my $reader = XML::LibXML::Reader-&gt;new( DOM =&gt; $dom, ... );
6300 </programlisting>
6301 <para>where ... are (optional) reader options described below in <xref linkend="reader-parsing-options"/>
6302 or various parser options described in <xref linkend="XML-LibXML-Parser"/>.
6303 The constructor recognizes the following XML sources:</para>
6304 <sect2>
6305 <title>Source specification</title>
6306 <variablelist>
6307 <varlistentry>
6308 <term>location</term>
6309 <listitem>
6310 <para>Read XML from a local file or URL.</para>
6311 </listitem>
6312 </varlistentry>
6313 <varlistentry>
6314 <term>string</term>
6315 <listitem>
6316 <para>Read XML from a string.</para>
6317 </listitem>
6318 </varlistentry>
6319 <varlistentry>
6320 <term>IO</term>
6321 <listitem>
6322 <para>Read XML a Perl IO filehandle.</para>
6323 </listitem>
6324 </varlistentry>
6325 <varlistentry>
6326 <term>FD</term>
6327 <listitem>
6328 <para>Read XML from a file descriptor (bypasses Perl I/O
6329 layer, only applicable to filehandles for regular
6330 files or pipes). Possibly faster than IO.</para>
6331 </listitem>
6332 </varlistentry>
6333 <varlistentry>
6334 <term>DOM</term>
6335 <listitem>
6336 <para>Use reader API to walk through a pre-parsed
6337 <xref linkend="XML-LibXML-Document"/>.</para>
6338 </listitem>
6339 </varlistentry>
6340 </variablelist>
6341 </sect2>
6342 <sect2 id="reader-parsing-options">
6343 <title>Reader options</title>
6344 <variablelist>
6345 <varlistentry>
6346 <term>encoding => $encoding</term>
6347 <listitem>
6348 <para>override document encoding.</para>
6349 </listitem>
6350 </varlistentry>
6351 <varlistentry>
6352 <term>RelaxNG => $rng_schema</term>
6353 <listitem>
6354 <para>can be used to pass either a <xref linkend="XML-LibXML-RelaxNG"/>
6355 object or a filename or URL of a RelaxNG schema to the
6356 constructor. The schema is then used to validate the
6357 document as it is processed.</para>
6358 </listitem>
6359 </varlistentry>
6360 <varlistentry>
6361 <term>Schema => $xsd_schema</term>
6362 <listitem>
6363 <para>can be used to pass either a <xref linkend="XML-LibXML-Schema"/>
6364 object or a filename or URL of a W3C XSD schema to the
6365 constructor. The schema is then used to validate the
6366 document as it is processed.</para>
6367 </listitem>
6368 </varlistentry>
6369 <varlistentry>
6370 <term>...</term>
6371 <listitem>
6372 <para>the reader further supports various
6373 parser options described in
6374 <xref linkend="XML-LibXML-Parser"/> (specifically those
6375 labeled by /reader/).
6376 </para>
6377 </listitem>
6378 </varlistentry>
6379 </variablelist>
6380 </sect2>
6381 </sect1>
6382 <sect1>
6383 <title>METHODS CONTROLLING PARSING PROGRESS</title>
6384 <variablelist>
6385 <varlistentry>
6386 <term>read ()</term>
6387 <listitem>
6388 <para>Moves the position to the next node in the stream,
6389 exposing its properties.</para>
6390 <para>Returns 1 if the node was read successfully, 0 if
6391 there is no more nodes to read, or -1 in case of
6392 error</para>
6393 </listitem>
6394 </varlistentry>
6395 <varlistentry>
6396 <term>readAttributeValue ()</term>
6397 <listitem>
6398 <para>Parses an attribute value into one or more Text and
6399 EntityReference nodes.</para>
6400 <para>Returns 1 in case of success, 0 if the reader was not positioned on an attribute node or all the attribute values have been read, or -1 in case of error.</para>
6401 </listitem>
6402 </varlistentry>
6403 <varlistentry>
6404 <term>readState ()</term>
6405 <listitem>
6406 <para>Gets the read state of the reader. Returns the state
6407 value, or -1 in case of error. The module exports
6408 constants for the Reader states, see STATES
6409 below.</para>
6410 </listitem>
6411 </varlistentry>
6412 <varlistentry>
6413 <term>depth ()</term>
6414 <listitem>
6415 <para>The depth of the node in the tree, starts at 0 for
6416 the root node.</para>
6417 </listitem>
6418 </varlistentry>
6419 <varlistentry>
6420 <term>next ()</term>
6421 <listitem>
6422 <para>Skip to the node following the current one in the
6423 document order while avoiding the sub-tree if any.
6424 Returns 1 if the node was read successfully, 0 if there
6425 is no more nodes to read, or -1 in case of error.</para>
6426 </listitem>
6427 </varlistentry>
6428 <varlistentry>
6429 <term>nextElement (localname?,nsURI?)</term>
6430 <listitem>
6431 <para>Skip nodes following the current one in the document
6432 order until a specific element is reached. The element's
6433 name must be equal to a given localname if defined, and
6434 its namespace must equal to a given nsURI if defined.
6435 Either of the arguments can be undefined (or omitted, in
6436 case of the latter or both).</para>
6437 <para>Returns 1 if the element was found, 0 if there is no more nodes to read, or -1 in case of error.</para>
6438 </listitem>
6439 </varlistentry>
6440 <varlistentry>
6441 <term>nextPatternMatch (compiled_pattern)</term>
6442 <listitem>
6443 <para>Skip nodes following the current one in the document
6444 order until an element matching a given
6445 compiled pattern is reached. See
6446 <xref linkend="XML-LibXML-Pattern"/> for information on
6447 compiled patterns. See also the <literal>matchesPattern</literal>
6448 method.</para>
6449 <para>Returns 1 if the element was found, 0 if there is no more nodes to read, or -1 in case of error.</para>
6450 </listitem>
6451 </varlistentry>
6452 <varlistentry>
6453 <term>skipSiblings ()</term>
6454 <listitem>
6455 <para>Skip all nodes on the same or lower level until the
6456 first node on a higher level is reached. In particular,
6457 if the current node occurs in an element, the reader
6458 stops at the end tag of the parent element, otherwise it
6459 stops at a node immediately following the parent
6460 node.</para>
6461 <para>Returns 1 if successful, 0 if end of the document is reached, or -1 in case of error.</para>
6462 </listitem>
6463 </varlistentry>
6464 <varlistentry>
6465 <term>nextSibling ()</term>
6466 <listitem>
6467 <para>It skips to the node following the current one in
6468 the document order while avoiding the sub-tree if
6469 any.</para>
6470 <para>Returns 1 if the node was read successfully, 0 if
6471 there is no more nodes to read, or -1 in case of
6472 error</para>
6473 </listitem>
6474 </varlistentry>
6475 <varlistentry>
6476 <term>nextSiblingElement (name?,nsURI?)</term>
6477 <listitem>
6478 <para>Like nextElement but only processes sibling elements
6479 of the current node (moving forward using
6480 <literal>nextSibling ()</literal> rather than
6481 <literal>read ()</literal>, internally).</para>
6482 <para>Returns 1 if the element was found, 0 if there is no
6483 more sibling nodes, or -1 in case of error.</para>
6484 </listitem>
6485 </varlistentry>
6486 <varlistentry>
6487 <term>finish ()</term>
6488 <listitem>
6489 <para>Skip all remaining nodes in the document, reaching end of the document.</para>
6490 <para>Returns 1 if successful, 0 in case of error.</para>
6491 </listitem>
6492 </varlistentry>
6493 <varlistentry>
6494 <term>close ()</term>
6495 <listitem>
6496 <para>This method releases any resources allocated by the
6497 current instance and closes any underlying input. It
6498 returns 0 on failure and 1 on success. This method is
6499 automatically called by the destructor when the reader
6500 is forgotten, therefore you do not have to call it
6501 directly.</para>
6502 </listitem>
6503 </varlistentry>
6504 </variablelist>
6505 </sect1>
6506 <sect1>
6507 <title>METHODS EXTRACTING INFORMATION</title>
6508 <variablelist>
6509 <varlistentry>
6510 <term>name ()</term>
6511 <listitem>
6512 <para>Returns the qualified name of the current node, equal to (Prefix:)LocalName.</para>
6513 </listitem>
6514 </varlistentry>
6515 <varlistentry>
6516 <term>nodeType ()</term>
6517 <listitem>
6518 <para>Returns the type of the current node. See NODE TYPES below.</para>
6519 </listitem>
6520 </varlistentry>
6521 <varlistentry>
6522 <term>localName ()</term>
6523 <listitem>
6524 <para>Returns the local name of the node.</para>
6525 </listitem>
6526 </varlistentry>
6527 <varlistentry>
6528 <term>prefix ()</term>
6529 <listitem>
6530 <para>Returns the prefix of the namespace associated with the node.</para>
6531 </listitem>
6532 </varlistentry>
6533 <varlistentry>
6534 <term>namespaceURI ()</term>
6535 <listitem>
6536 <para>Returns the URI defining the namespace associated with the node.</para>
6537 </listitem>
6538 </varlistentry>
6539 <varlistentry>
6540 <term>isEmptyElement ()</term>
6541 <listitem>
6542 <para>Check if the current node is empty, this is a bit
6543 bizarre in the sense that &lt;a/&gt; will be considered
6544 empty while &lt;a&gt;&lt;/a&gt; will not.</para>
6545 </listitem>
6546 </varlistentry>
6547 <varlistentry>
6548 <term>hasValue ()</term>
6549 <listitem>
6550 <para>Returns true if the node can have a text value.</para>
6551 </listitem>
6552 </varlistentry>
6553 <varlistentry>
6554 <term>value ()</term>
6555 <listitem>
6556 <para>Provides the text value of the node if present or undef if not available.</para>
6557 </listitem>
6558 </varlistentry>
6559 <varlistentry>
6560 <term>readInnerXml ()</term>
6561 <listitem>
6562 <para>Reads the contents of the current node, including
6563 child nodes and markup. Returns a string containing the
6564 XML of the node's content, or undef if the current node
6565 is neither an element nor attribute, or has no child
6566 nodes.</para>
6567 </listitem>
6568 </varlistentry>
6569 <varlistentry>
6570 <term>readOuterXml ()</term>
6571 <listitem>
6572 <para>Reads the contents of the current node, including
6573 child nodes and markup.</para>
6574 <para>Returns a string containing the XML of the node
6575 including its content, or undef if the current node is
6576 neither an element nor attribute.</para>
6577 </listitem>
6578 </varlistentry>
6579 <varlistentry>
6580 <term>nodePath()</term>
6581 <listitem>
6582 <para>Returns a canonical location path to the current element
6583 from the root node to the current node. Namespaced
6584 elements are matched by '*', because there is no way to declare
6585 prefixes within XPath patterns. Unlike
6586 <literal>XML::LibXML::Node::nodePath()</literal>, this function
6587 does not provide sibling counts (i.e. instead of e.g. '/a/b[1]' and '/a/b[2]'
6588 you get '/a/b' for both matches).
6589 </para>
6590 </listitem>
6591 </varlistentry>
6592 <varlistentry>
6593 <term>matchesPattern(compiled_pattern)</term>
6594 <listitem>
6595 <para>Returns a true value if the current
6596 node matches a compiled pattern.
6597 See <xref linkend="XML-LibXML-Pattern"/> for information on
6598 compiled patterns. See also the <literal>nextPatternMatch</literal>
6599 method.</para>
6600 </listitem>
6601 </varlistentry>
6602 </variablelist>
6603 </sect1>
6604 <sect1>
6605 <title>METHODS EXTRACTING DOM NODES</title>
6606 <variablelist>
6607 <varlistentry>
6608 <term>document ()</term>
6609 <listitem>
6610 <para>Provides access to the document tree built by the
6611 reader. This function can be used to collect the
6612 preserved nodes (see <literal>preserveNode()</literal>
6613 and preservePattern).</para>
6614 <para>CAUTION: Never use this function to modify the tree
6615 unless reading of the whole document is
6616 completed!</para>
6617 </listitem>
6618 </varlistentry>
6619 <varlistentry>
6620 <term>copyCurrentNode (deep)</term>
6621 <listitem>
6622 <para>This function is similar a DOM function
6623 <literal>copyNode()</literal>. It returns a copy of the
6624 currently processed node as a corresponding DOM object.
6625 Use deep = 1 to obtain the full sub-tree.</para>
6626 </listitem>
6627 </varlistentry>
6628 <varlistentry>
6629 <term>preserveNode ()</term>
6630 <listitem>
6631 <para>This tells the XML Reader to preserve the current
6632 node in the document tree. A document tree consisting of
6633 the preserved nodes and their content can be obtained
6634 using the method <literal>document()</literal> once
6635 parsing is finished.</para>
6636 <para>Returns the node or NULL in case of error.</para>
6637 </listitem>
6638 </varlistentry>
6639 <varlistentry>
6640 <term>preservePattern (pattern,\%ns_map)</term>
6641 <listitem>
6642 <para>This tells the XML Reader to preserve all nodes
6643 matched by the pattern (which is a streaming XPath
6644 subset). A document tree consisting of the preserved
6645 nodes and their content can be obtained using the method
6646 <literal>document()</literal> once parsing is
6647 finished.</para>
6648 <para>An optional second argument can be used to provide a
6649 HASH reference mapping prefixes used by the XPath to
6650 namespace URIs.</para>
6651 <para>The XPath subset available with this function is
6652 described at</para>
6653 <programlisting>http://www.w3.org/TR/xmlschema-1/#Selector</programlisting>
6654 <para>and matches the production</para>
6655 <programlisting>Path ::= ('.//')? ( Step '/' )* ( Step | '@' NameTest )</programlisting>
6656 <para>Returns a positive number in case of success and -1
6657 in case of error</para>
6658 </listitem>
6659 </varlistentry>
6660 </variablelist>
6661 </sect1>
6662 <sect1>
6663 <title>METHODS PROCESSING ATTRIBUTES</title>
6664 <variablelist>
6665 <varlistentry>
6666 <term>attributeCount ()</term>
6667 <listitem>
6668 <para>Provides the number of attributes of the current
6669 node.</para>
6670 </listitem>
6671 </varlistentry>
6672 <varlistentry>
6673 <term>hasAttributes ()</term>
6674 <listitem>
6675 <para>Whether the node has attributes.</para>
6676 </listitem>
6677 </varlistentry>
6678 <varlistentry>
6679 <term>getAttribute (name)</term>
6680 <listitem>
6681 <para>Provides the value of the attribute with the
6682 specified qualified name.</para>
6683 <para>Returns a string containing the value of the
6684 specified attribute, or undef in case of error.</para>
6685 </listitem>
6686 </varlistentry>
6687 <varlistentry>
6688 <term>getAttributeNs (localName, namespaceURI)</term>
6689 <listitem>
6690 <para>Provides the value of the specified
6691 attribute.</para>
6692 <para>Returns a string containing the value of the
6693 specified attribute, or undef in case of error.</para>
6694 </listitem>
6695 </varlistentry>
6696 <varlistentry>
6697 <term>getAttributeNo (no)</term>
6698 <listitem>
6699 <para>Provides the value of the attribute with the
6700 specified index relative to the containing
6701 element.</para>
6702 <para>Returns a string containing the value of the
6703 specified attribute, or undef in case of error.</para>
6704 </listitem>
6705 </varlistentry>
6706 <varlistentry>
6707 <term>isDefault ()</term>
6708 <listitem>
6709 <para>Returns true if the current attribute node was
6710 generated from the default value defined in the
6711 DTD.</para>
6712 </listitem>
6713 </varlistentry>
6714 <varlistentry>
6715 <term>moveToAttribute (name)</term>
6716 <listitem>
6717 <para>Moves the position to the attribute with the
6718 specified local name and namespace URI.</para>
6719 <para>Returns 1 in case of success, -1 in case of error, 0
6720 if not found</para>
6721 </listitem>
6722 </varlistentry>
6723 <varlistentry>
6724 <term>moveToAttributeNo (no)</term>
6725 <listitem>
6726 <para>Moves the position to the attribute with the
6727 specified index relative to the containing
6728 element.</para>
6729 <para>Returns 1 in case of success, -1 in case of error, 0
6730 if not found</para>
6731 </listitem>
6732 </varlistentry>
6733 <varlistentry>
6734 <term>moveToAttributeNs (localName,namespaceURI)</term>
6735 <listitem>
6736 <para>Moves the position to the attribute with the
6737 specified local name and namespace URI.</para>
6738 <para>Returns 1 in case of success, -1 in case of error, 0
6739 if not found</para>
6740 </listitem>
6741 </varlistentry>
6742 <varlistentry>
6743 <term>moveToFirstAttribute ()</term>
6744 <listitem>
6745 <para>Moves the position to the first attribute associated
6746 with the current node.</para>
6747 <para>Returns 1 in case of success, -1 in case of error, 0
6748 if not found</para>
6749 </listitem>
6750 </varlistentry>
6751 <varlistentry>
6752 <term>moveToNextAttribute ()</term>
6753 <listitem>
6754 <para>Moves the position to the next attribute associated
6755 with the current node.</para>
6756 <para>Returns 1 in case of success, -1 in case of error, 0
6757 if not found</para>
6758 </listitem>
6759 </varlistentry>
6760 <varlistentry>
6761 <term>moveToElement ()</term>
6762 <listitem>
6763 <para>Moves the position to the node that contains the
6764 current attribute node.</para>
6765 <para>Returns 1 in case of success, -1 in case of error, 0
6766 if not moved</para>
6767 </listitem>
6768 </varlistentry>
6769 <varlistentry>
6770 <term>isNamespaceDecl ()</term>
6771 <listitem>
6772 <para>Determine whether the current node is a namespace
6773 declaration rather than a regular attribute.</para>
6774 <para>Returns 1 if the current node is a namespace
6775 declaration, 0 if it is a regular attribute or other
6776 type of node, or -1 in case of error.</para>
6777 </listitem>
6778 </varlistentry>
6779 </variablelist>
6780 </sect1>
6781 <sect1>
6782 <title>OTHER METHODS</title>
6783 <variablelist>
6784 <varlistentry>
6785 <term>lookupNamespace (prefix)</term>
6786 <listitem>
6787 <para>Resolves a namespace prefix in the scope of the
6788 current element.</para>
6789 <para>Returns a string containing the namespace URI to
6790 which the prefix maps or undef in case of error.</para>
6791 </listitem>
6792 </varlistentry>
6793 <varlistentry>
6794 <term>encoding ()</term>
6795 <listitem>
6796 <para>Returns a string containing the encoding of the
6797 document or undef in case of error.</para>
6798 </listitem>
6799 </varlistentry>
6800 <varlistentry>
6801 <term>standalone ()</term>
6802 <listitem>
6803 <para>Determine the standalone status of the document
6804 being read. Returns 1 if the document was declared to be
6805 standalone, 0 if it was declared to be not standalone,
6806 or -1 if the document did not specify its standalone
6807 status or in case of error.</para>
6808 </listitem>
6809 </varlistentry>
6810 <varlistentry>
6811 <term>xmlVersion ()</term>
6812 <listitem>
6813 <para>Determine the XML version of the document being
6814 read. Returns a string containing the XML version of the
6815 document or undef in case of error.</para>
6816 </listitem>
6817 </varlistentry>
6818 <varlistentry>
6819 <term>baseURI ()</term>
6820 <listitem>
6821 <para>Returns the base URI of a given node.</para>
6822 </listitem>
6823 </varlistentry>
6824 <varlistentry>
6825 <term>isValid ()</term>
6826 <listitem>
6827 <para>Retrieve the validity status from the parser.</para>
6828 <para>Returns 1 if valid, 0 if no, and -1 in case of
6829 error.</para>
6830 </listitem>
6831 </varlistentry>
6832 <varlistentry>
6833 <term>xmlLang ()</term>
6834 <listitem>
6835 <para>The xml:lang scope within which the node
6836 resides.</para>
6837 </listitem>
6838 </varlistentry>
6839 <varlistentry>
6840 <term>lineNumber ()</term>
6841 <listitem>
6842 <para>Provide the line number of the current parsing
6843 point.</para>
6844 </listitem>
6845 </varlistentry>
6846 <varlistentry>
6847 <term>columnNumber ()</term>
6848 <listitem>
6849 <para>Provide the column number of the current parsing
6850 point.</para>
6851 </listitem>
6852 </varlistentry>
6853 <varlistentry>
6854 <term>byteConsumed ()</term>
6855 <listitem>
6856 <para>This function provides the current index of the
6857 parser relative to the start of the current entity. This
6858 function is computed in bytes from the beginning
6859 starting at zero and finishing at the size in bytes of
6860 the file if parsing a file. The function is of constant
6861 cost if the input is UTF-8 but can be costly if run on
6862 non-UTF-8 input.</para>
6863 </listitem>
6864 </varlistentry>
6865 <varlistentry>
6866 <term>setParserProp (prop =&gt; value, ...)</term>
6867 <listitem>
6868 <para>Change the parser processing behaviour by changing
6869 some of its internal properties. The following
6870 properties are available with this function:
6871 ``load_ext_dtd'', ``complete_attributes'',
6872 ``validation'', ``expand_entities''.</para>
6873 <para>Since some of the properties can only be changed
6874 before any read has been done, it is best to set the
6875 parsing properties at the constructor.</para>
6876 <para>Returns 0 if the call was successful, or -1 in case
6877 of error</para>
6878 </listitem>
6879 </varlistentry>
6880 <varlistentry>
6881 <term>getParserProp (prop)</term>
6882 <listitem>
6883 <para>Get value of an parser internal property. The
6884 following property names can be used: ``load_ext_dtd'',
6885 ``complete_attributes'', ``validation'',
6886 ``expand_entities''.</para>
6887 <para>Returns the value, usually 0 or 1, or -1 in case of
6888 error.</para>
6889 </listitem>
6890 </varlistentry>
6891 </variablelist>
6892 </sect1>
6893 <sect1>
6894 <title>DESTRUCTION</title>
6895 <para>XML::LibXML takes care of the reader object destruction
6896 when the last reference to the reader object goes out of
6897 scope. The document tree is preserved, though, if either of
6898 $reader-&gt;document or $reader-&gt;preserveNode was used and
6899 references to the document tree exist.</para>
6900 </sect1>
6901 <sect1>
6902 <title>NODE TYPES</title>
6903 <para>The reader interface provides the following constants for
6904 node types (the constant symbols are exported by default or if
6905 tag <literal>:types</literal> is used).</para>
6906 <programlisting>XML_READER_TYPE_NONE =&gt; 0
6907 XML_READER_TYPE_ELEMENT =&gt; 1
6908 XML_READER_TYPE_ATTRIBUTE =&gt; 2
6909 XML_READER_TYPE_TEXT =&gt; 3
6910 XML_READER_TYPE_CDATA =&gt; 4
6911 XML_READER_TYPE_ENTITY_REFERENCE =&gt; 5
6912 XML_READER_TYPE_ENTITY =&gt; 6
6913 XML_READER_TYPE_PROCESSING_INSTRUCTION =&gt; 7
6914 XML_READER_TYPE_COMMENT =&gt; 8
6915 XML_READER_TYPE_DOCUMENT =&gt; 9
6916 XML_READER_TYPE_DOCUMENT_TYPE =&gt; 10
6917 XML_READER_TYPE_DOCUMENT_FRAGMENT =&gt; 11
6918 XML_READER_TYPE_NOTATION =&gt; 12
6919 XML_READER_TYPE_WHITESPACE =&gt; 13
6920 XML_READER_TYPE_SIGNIFICANT_WHITESPACE =&gt; 14
6921 XML_READER_TYPE_END_ELEMENT =&gt; 15
6922 XML_READER_TYPE_END_ENTITY =&gt; 16
6923 XML_READER_TYPE_XML_DECLARATION =&gt; 17
6924 </programlisting>
6925 </sect1>
6926 <sect1>
6927 <title>STATES</title>
6928 <para>The following constants represent the values returned by
6929 <literal>readState()</literal>. They are exported by default,
6930 or if tag <literal>:states</literal> is used:</para>
6931 <programlisting>XML_READER_NONE =&gt; -1
6932 XML_READER_START =&gt; 0
6933 XML_READER_ELEMENT =&gt; 1
6934 XML_READER_END =&gt; 2
6935 XML_READER_EMPTY =&gt; 3
6936 XML_READER_BACKTRACK =&gt; 4
6937 XML_READER_DONE =&gt; 5
6938 XML_READER_ERROR =&gt; 6
6939 </programlisting>
6940 </sect1>
6941 <sect1>
6942 <title>SEE ALSO</title>
6943 <para><xref linkend="XML-LibXML-Pattern"/> for information about compiled patterns.</para>
6944 <para>http://xmlsoft.org/html/libxml-xmlreader.html</para>
6945 <para>http://dotgnu.org/pnetlib-doc/System/Xml/XmlTextReader.html</para>
6946 </sect1>
6947 <sect1>
6948 <title>ORIGINAL IMPLEMENTATION</title>
6949 <para>Heiko Klein, &lt;[email protected]&lt;gt&gt; and Petr Pajas</para>
6950 </sect1>
6951 </chapter>
6952 <chapter id="XML-LibXML-XPathExpression">
6953 <title>XML::LibXML::XPathExpression - interface to libxml2 pre-compiled XPath expressions</title>
6954 <titleabbrev>XML::LibXML::XPathExpression</titleabbrev>
6955 <sect1>
6956 <title>Synopsis</title>
6957 <programlisting>use XML::LibXML;
6958 my $compiled_xpath = XML::LibXML::XPathExpression-&gt;new('//foo[@bar="baz"][position()&lt;4]');
6959
6960 # interface from XML::LibXML::Node
6961
6962 my $result = $node->find($compiled_xpath);
6963 my @nodes = $node->findnodes($compiled_xpath);
6964 my $value = $node->findvalue($compiled_xpath);
6965
6966 # interface from XML::LibXML::XPathContext
6967
6968 my $result = $xpc->find($compiled_xpath,$node);
6969 my @nodes = $xpc->findnodes($compiled_xpath,$node);
6970 my $value = $xpc->findvalue($compiled_xpath,$node);
6971 </programlisting>
6972 </sect1>
6973 <sect1>
6974 <title>Description</title>
6975 <para>This is a perl interface to libxml2's pre-compiled XPath expressions.
6976 Pre-compiling an XPath expression can give in some performance
6977 benefit if the same XPath query is evaluated many times.
6978 <function>XML::LibXML::XPathExpression</function> objects
6979 can be passed to all <function>find...</function>
6980 functions <function>XML::LibXML</function>
6981 that expect an XPath expression.
6982 </para>
6983 <variablelist>
6984 <varlistentry>
6985 <term>new()</term>
6986 <listitem>
6987 <funcsynopsis>
6988 <funcsynopsisinfo>$compiled = XML::LibXML::XPathExpression-&gt;new( xpath_string );</funcsynopsisinfo>
6989 </funcsynopsis>
6990 <para>The constructor takes an XPath 1.0 expression as a string
6991 and returns an object representing the pre-compiled
6992 expressions (the actual data structure is internal to libxml2).
6993 </para>
6994 </listitem>
6995 </varlistentry>
6996 </variablelist>
6997 </sect1>
6998 </chapter>
6999 <chapter id="XML-LibXML-Pattern">
7000 <title>XML::LibXML::Pattern - interface to libxml2 XPath patterns</title>
7001 <titleabbrev>XML::LibXML::Pattern</titleabbrev>
7002 <sect1>
7003 <title>Synopsis</title>
7004 <programlisting>use XML::LibXML;
7005 my $pattern = XML::LibXML::Pattern-&gt;new('/x:html/x:body//x:div', { 'x' => 'http://www.w3.org/1999/xhtml' });
7006 # test a match on an XML::LibXML::Node $node
7007
7008 if ($pattern->matchesNode($node)) { ... }
7009
7010 # or on an XML::LibXML::Reader
7011
7012 if ($reader->matchesPattern($pattern)) { ... }
7013
7014 # or skip reading all nodes that do not match
7015
7016 print $reader->nodePath while $reader->nextPatternMatch($pattern);
7017 </programlisting>
7018 </sect1>
7019 <sect1>
7020 <title>Description</title>
7021 <para>This is a perl interface to libxml2's pattern matching support
7022 <emphasis>http://xmlsoft.org/html/libxml-pattern.html</emphasis>.
7023 This feature requires recent versions of libxml2.</para>
7024 <para>Patterns are a small subset of XPath language, which is limited
7025 to (disjunctions of) location paths involving the child and descendant axes in abbreviated form
7026 as described by the extended BNF given below:
7027 </para>
7028 <programlisting>Selector ::= Path ( '|' Path )*
7029 Path ::= ('.//' | '//' | '/' )? Step ( '/' Step )*
7030 Step ::= '.' | NameTest
7031 NameTest ::= QName | '*' | NCName ':' '*'</programlisting>
7032 <para>For readability, whitespace may be used in selector XPath expressions even though not explicitly allowed by the grammar: whitespace may be freely added within patterns before or after any token, where</para>
7033 <programlisting>token ::= '.' | '/' | '//' | '|' | NameTest</programlisting>
7034 <para>Note that no predicates or attribute tests are allowed.</para>
7035 <para>Patterns are particularly useful for stream parsing provided via the <literal>XML::LibXML::Reader</literal> interface.</para>
7036 <variablelist>
7037 <varlistentry>
7038 <term>new()</term>
7039 <listitem>
7040 <funcsynopsis>
7041 <funcsynopsisinfo>$pattern = XML::LibXML::Pattern-&gt;new( pattern, { prefix => namespace_URI, ... } );</funcsynopsisinfo>
7042 </funcsynopsis>
7043 <para>The constructor of a pattern takes a pattern expression (as described
7044 by the BNF grammar above) and an optional HASH reference mapping
7045 prefixes to namespace URIs. The method returns a compiled pattern object.
7046 </para>
7047 <para>
7048 Note that if the document
7049 has a default namespace, it must still be given an prefix in order
7050 to be matched (as demanded by the XPath 1.0 specification). For example,
7051 to match an element <literal>&lt;a xmlns="http://foo.bar"&lt;/a></literal>, one
7052 should use a pattern like this:
7053 </para>
7054 <programlisting>$pattern = XML::LibXML::Pattern-&gt;new( 'foo:a', { foo => 'http://foo.bar' });</programlisting>
7055 </listitem>
7056 </varlistentry>
7057 <varlistentry>
7058 <term>matchesNode($node)</term>
7059 <listitem>
7060 <funcsynopsis>
7061 <funcsynopsisinfo>$bool = $pattern->matchesNode($node);</funcsynopsisinfo>
7062 </funcsynopsis>
7063 <para>Given an XML::LibXML::Node object, returns a true value if
7064 the node is matched by the compiled pattern expression.</para>
7065 </listitem>
7066 </varlistentry>
7067 </variablelist>
7068 </sect1>
7069 <sect1>
7070 <title>SEE ALSO</title>
7071 <para><xref linkend="XML-LibXML-Reader"/> for other methods involving compiled patterns.</para>
7072 </sect1>
7073 </chapter>
7074 <chapter id="XML-LibXML-RegExp">
7075 <title>XML::LibXML::RegExp - interface to libxml2 regular expressions</title>
7076 <titleabbrev>XML::LibXML::RegExp</titleabbrev>
7077 <sect1>
7078 <title>Synopsis</title>
7079 <programlisting>use XML::LibXML;
7080 my $compiled_re = XML::LibXML::RegExp-&gt;new('[0-9]{5}(-[0-9]{4})?');
7081 if ($compiled_re->isDeterministic()) { ... }
7082 if ($compiled_re->matches($string)) { ... }
7083 </programlisting>
7084 </sect1>
7085 <sect1>
7086 <title>Description</title>
7087 <para>This is a perl interface to libxml2's implementation of regular expressions, which are used e.g. for validation of XML Schema simple types (pattern facet).</para>
7088 <variablelist>
7089 <varlistentry>
7090 <term>new()</term>
7091 <listitem>
7092 <funcsynopsis>
7093 <funcsynopsisinfo>$compiled_re = XML::LibXML::RegExp-&gt;new( $regexp_str );</funcsynopsisinfo>
7094 </funcsynopsis>
7095 <para>The constructor takes a string containing a regular expression
7096 and returns a compiled regexp object.
7097 </para>
7098 </listitem>
7099 </varlistentry>
7100 <varlistentry>
7101 <term>matches($string)</term>
7102 <listitem>
7103 <funcsynopsis>
7104 <funcsynopsisinfo>$bool = $compiled_re->matches($string);</funcsynopsisinfo>
7105 </funcsynopsis>
7106 <para>Given a string value, returns a true value if
7107 the value is matched by the compiled regular expression.</para>
7108 </listitem>
7109 </varlistentry>
7110 <varlistentry>
7111 <term>isDeterministic()</term>
7112 <listitem>
7113 <funcsynopsis>
7114 <funcsynopsisinfo>$bool = $compiled_re->isDeterministic();</funcsynopsisinfo>
7115 </funcsynopsis>
7116 <para>Returns a true value if the regular expression is deterministic; returns false otherwise. (See the definition of determinism in the <ulink url="http://www.w3.org/TR/REC-xml/#determinism">XML spec</ulink>)</para>
7117 </listitem>
7118 </varlistentry>
7119 </variablelist>
7120 </sect1>
7121 </chapter>
7122 <chapter id="XML-LibXML-NamedNodeMap">
7123 <title>A map for named nodes</title>
7124
7125 <titleabbrev>XML::LibXML::NamedNodeMap</titleabbrev>
7126 <sect1>
7127 <title>Synopsis</title>
7128 <programlisting>use XML::LibXML;
7129 my $map = XML::LibXML::NamedNodeMap->new(@nodes);
7130
7131 my $nodes_list = $map->nodes();
7132
7133 my $node_with_index_2 = $map->item(2);
7134 </programlisting>
7135 </sect1>
7136 <sect1>
7137 <title>Description</title>
7138
7139 <para>XML::LibXML::NamedNodeMap maps nodes' names to nodes.</para>
7140 </sect1>
7141 <sect1>
7142 <title>Methods</title>
7143
7144 <variablelist>
7145 <varlistentry>
7146 <term>length</term>
7147
7148 <listitem>
7149 <funcsynopsis>
7150 <funcsynopsisinfo>my $length = $map-&gt;length;</funcsynopsisinfo>
7151 </funcsynopsis>
7152
7153 <para>Returns the number of nodes in the map.</para>
7154 </listitem>
7155 </varlistentry>
7156
7157 <varlistentry>
7158 <term>nodes</term>
7159
7160 <listitem>
7161 <funcsynopsis>
7162 <funcsynopsisinfo>my $nodes_ref = $node->nodes()</funcsynopsisinfo>
7163 </funcsynopsis>
7164
7165 <para>Returns a reference to the list of nodes.</para>
7166 </listitem>
7167 </varlistentry>
7168
7169 <varlistentry>
7170 <term>item</term>
7171
7172 <listitem>
7173 <funcsynopsis>
7174 <funcsynopsisinfo>my $node_2 = $map->item(2);</funcsynopsisinfo>
7175 </funcsynopsis>
7176
7177 <para>Returns the node with the index of the argument
7178 (starting from 0)</para>
7179 </listitem>
7180 </varlistentry>
7181
7182 <varlistentry>
7183 <term>getNamedItem</term>
7184
7185 <listitem>
7186 <funcsynopsis>
7187 <funcsynopsisinfo>my $node = $map->getNamedItem('phone_number');</funcsynopsisinfo>
7188 </funcsynopsis>
7189
7190 <para>Returns the node with the name.</para>
7191
7192 </listitem>
7193 </varlistentry>
7194
7195 <varlistentry>
7196 <term>setNamedItem</term>
7197
7198 <listitem>
7199 <funcsynopsis>
7200 <funcsynopsisinfo>$map->setNamedItem($new_node)</funcsynopsisinfo>
7201 </funcsynopsis>
7202
7203 <para>Sets the node with the same name as
7204 <literal>$new_node</literal> to
7205 <literal>$new_node</literal>.</para>
7206 </listitem>
7207 </varlistentry>
7208
7209 <varlistentry>
7210 <term>removeNamedItem</term>
7211
7212 <listitem>
7213 <funcsynopsis>
7214 <funcsynopsisinfo>$map->removeNamedItem($name)</funcsynopsisinfo>
7215 </funcsynopsis>
7216
7217 <para>Remove the item with the name
7218 <literal>$name</literal>.</para>
7219 </listitem>
7220 </varlistentry>
7221
7222 <varlistentry>
7223 <term>getNamedItemNS</term>
7224
7225 <listitem><para>
7226 <emphasis>Not implemented yet.</emphasis>.
7227 </para></listitem>
7228 </varlistentry>
7229 <varlistentry>
7230 <term>setNamedItemNS</term>
7231
7232 <listitem><para>
7233 <emphasis>Not implemented yet.</emphasis>.
7234 </para></listitem>
7235 </varlistentry>
7236 <varlistentry>
7237 <term>removeNamedItemNS</term>
7238
7239 <listitem><para>
7240 <emphasis>Not implemented yet.</emphasis>.
7241 </para></listitem>
7242 </varlistentry>
7243 </variablelist>
7244 </sect1>
7245 </chapter>
7246 <chapter id="XML-LibXML-Error">
7247 <title>Structured Errors</title>
7248 <titleabbrev>XML::LibXML::Error</titleabbrev>
7249 <sect1>
7250 <title>Synopsis</title>
7251 <programlisting>
7252 eval { ... };
7253 if (ref($@)) {
7254 # handle a structured error (XML::LibXML::Error object)
7255 } elsif ($@) {
7256 # error, but not an XML::LibXML::Error object
7257 } else {
7258 # no error
7259 }
7260 </programlisting>
7261 </sect1>
7262 <sect1>
7263 <title>Description</title>
7264 <para>The
7265 XML::LibXML::Error class is a tiny frontend to
7266 <emphasis>libxml2</emphasis>&#39;s structured error support. If
7267 XML::LibXML is compiled with structured error support, all errors
7268 reported by libxml2 are transformed to XML::LibXML::Error
7269 objects. These objects automatically serialize to the
7270 corresponding error messages when printed or used in a string
7271 operation, but as objects, can also be used to get a detailed and
7272 structured information about the error that occurred.
7273 </para>
7274 <para>Unlike most other XML::LibXML objects, XML::LibXML::Error
7275 doesn't wrap an underlying <emphasis>libxml2</emphasis>
7276 structure directly, but rather transforms it to a blessed Perl
7277 hash reference containing the individual fields of the
7278 structured error information as hash key-value pairs. Individual
7279 items (fields) of a structured error can either be
7280 obtained directly as $@-&#62;{field}, or using autoloaded
7281 methods such as $@-&#62;field() (where field is the field
7282 name). XML::LibXML::Error objects have the following fields:
7283 domain, code, level, file, line, nodename, message, str1, str2,
7284 str3, num1, num2, and _prev (some of them may be undefined).
7285 </para>
7286 <variablelist>
7287 <varlistentry>
7288 <term>$XML::LibXML::Error::WARNINGS</term>
7289 <listitem>
7290 <funcsynopsis>
7291 <funcsynopsisinfo>$XML::LibXML::Error::WARNINGS=1;</funcsynopsisinfo>
7292 </funcsynopsis>
7293 <para>Traditionally, XML::LibXML was suppressing parser
7294 warnings by setting libxml2's global variable
7295 xmlGetWarningsDefaultValue to 0. Since
7296 1.70 we do not change libxml2's global
7297 variables anymore; for backward compatibility,
7298 XML::LibXML suppresses warnings.
7299 This variable can be set to 1
7300 to enable reporting of these warnings via
7301 Perl <literal>warn</literal>
7302 and to 2 to report hem via <literal>die</literal>.
7303 </para>
7304 </listitem>
7305 </varlistentry>
7306 <varlistentry>
7307 <term>as_string</term>
7308 <listitem>
7309 <funcsynopsis>
7310 <funcsynopsisinfo>$message = $@-&#62;as_string();</funcsynopsisinfo>
7311 </funcsynopsis>
7312 <para>This function serializes an XML::LibXML::Error
7313 object to a string containing the full error message
7314 close to the message produced by <emphasis>libxml2</emphasis> default error
7315 handlers and tools like xmllint. This method is also used
7316 to overload "" operator on XML::LibXML::Error, so it is
7317 automatically called whenever XML::LibXML::Error object
7318 is treated as a string (e.g. in print $@).
7319 </para>
7320 </listitem>
7321 </varlistentry>
7322 <varlistentry>
7323 <term>dump</term>
7324 <listitem>
7325 <funcsynopsis>
7326 <funcsynopsisinfo>print $@->dump();</funcsynopsisinfo>
7327 </funcsynopsis>
7328 <para>This function serializes an XML::LibXML::Error to a
7329 string displaying all fields of the error structure
7330 individually on separate lines of the form 'name' => 'value'.
7331 </para>
7332 </listitem>
7333 </varlistentry>
7334 <varlistentry>
7335 <term>domain</term>
7336 <listitem>
7337 <funcsynopsis>
7338 <funcsynopsisinfo>$error_domain = $@->domain();</funcsynopsisinfo>
7339 </funcsynopsis>
7340 <para>Returns string containing information about what part
7341 of the library raised the error. Can be one of:
7342 "parser", "tree", "namespace", "validity", "HTML parser",
7343 "memory", "output", "I/O", "ftp", "http",
7344 "XInclude", "XPath", "xpointer", "regexp", "Schemas
7345 datatype",
7346 "Schemas parser", "Schemas validity",
7347 "Relax-NG parser", "Relax-NG validity", "Catalog",
7348 "C14N", "XSLT", "validity".
7349 </para>
7350 </listitem>
7351 </varlistentry>
7352 <varlistentry>
7353 <term>code</term>
7354 <listitem>
7355 <funcsynopsis>
7356 <funcsynopsisinfo>$error_code = $@->code();</funcsynopsisinfo>
7357 </funcsynopsis>
7358 <para>Returns the actual libxml2 error code.
7359 The XML::LibXML::ErrNo module defines
7360 constants for individual error codes. Currently
7361 libxml2 uses over 480 different error codes.
7362 </para>
7363 </listitem>
7364 </varlistentry>
7365 <varlistentry>
7366 <term>message</term>
7367 <listitem>
7368 <funcsynopsis>
7369 <funcsynopsisinfo>$error_message = $@->message();</funcsynopsisinfo>
7370 </funcsynopsis>
7371 <para>Returns a human-readable informative error
7372 message.</para>
7373 </listitem>
7374 </varlistentry>
7375 <varlistentry>
7376 <term>level</term>
7377 <listitem>
7378 <funcsynopsis>
7379 <funcsynopsisinfo>$error_level = $@->level();</funcsynopsisinfo>
7380 </funcsynopsis>
7381 <para>Returns an integer value describing how consequent is
7382 the error. XML::LibXML::Error defines the following
7383 constants:
7384 </para>
7385 <itemizedlist>
7386 <listitem>
7387 <para>XML_ERR_NONE = 0</para>
7388 </listitem>
7389 <listitem>
7390 <para>XML_ERR_WARNING = 1 : A simple warning.</para>
7391 </listitem>
7392 <listitem>
7393 <para>XML_ERR_ERROR = 2 : A recoverable error.</para>
7394 </listitem>
7395 <listitem>
7396 <para>XML_ERR_FATAL = 3 : A fatal error.</para>
7397 </listitem>
7398 </itemizedlist>
7399 </listitem>
7400 </varlistentry>
7401 <varlistentry>
7402 <term>file</term>
7403 <listitem>
7404 <funcsynopsis>
7405 <funcsynopsisinfo>$filename = $@->file();</funcsynopsisinfo>
7406 </funcsynopsis>
7407 <para>Returns the filename of the file being processed while
7408 the error occurred.
7409 </para>
7410 </listitem>
7411 </varlistentry>
7412 <varlistentry>
7413 <term>line</term>
7414 <listitem>
7415 <funcsynopsis>
7416 <funcsynopsisinfo>$line = $@->line();</funcsynopsisinfo>
7417 </funcsynopsis>
7418 <para>The line number, if available.</para>
7419 </listitem>
7420 </varlistentry>
7421 <varlistentry>
7422 <term>nodename</term>
7423 <listitem>
7424 <funcsynopsis>
7425 <funcsynopsisinfo>$nodename = $@->nodename();</funcsynopsisinfo>
7426 </funcsynopsis>
7427 <para>Name of the node where error occurred, if available.
7428 When this field is non-empty, libxml2 actually returned a
7429 physical pointer to the specified node. Due to memory
7430 management issues, it is very difficult to implement a
7431 way to expose the pointer to the Perl level as a
7432 XML::LibXML::Node. For this reason, XML::LibXML::Error
7433 currently only exposes the name the node.
7434 </para>
7435 </listitem>
7436 </varlistentry>
7437 <varlistentry>
7438 <term>str1</term>
7439 <listitem>
7440 <funcsynopsis>
7441 <funcsynopsisinfo>$error_str1 = $@->str1();</funcsynopsisinfo>
7442 </funcsynopsis>
7443 <para>Error specific. Extra string information.</para>
7444 </listitem>
7445 </varlistentry>
7446 <varlistentry>
7447 <term>str2</term>
7448 <listitem>
7449 <funcsynopsis>
7450 <funcsynopsisinfo>$error_str2 = $@->str2();</funcsynopsisinfo>
7451 </funcsynopsis>
7452 <para>Error specific. Extra string information.</para>
7453 </listitem>
7454 </varlistentry>
7455 <varlistentry>
7456 <term>str3</term>
7457 <listitem>
7458 <funcsynopsis>
7459 <funcsynopsisinfo>$error_str3 = $@->str3();</funcsynopsisinfo>
7460 </funcsynopsis>
7461 <para>Error specific. Extra string information.</para>
7462 </listitem>
7463 </varlistentry>
7464 <varlistentry>
7465 <term>num1</term>
7466 <listitem>
7467 <funcsynopsis>
7468 <funcsynopsisinfo>$error_num1 = $@->num1();</funcsynopsisinfo>
7469 </funcsynopsis>
7470 <para>Error specific. Extra numeric information.</para>
7471 </listitem>
7472 </varlistentry>
7473 <varlistentry>
7474 <term>num2</term>
7475 <listitem>
7476 <funcsynopsis>
7477 <funcsynopsisinfo>$error_num2 = $@->num2();</funcsynopsisinfo>
7478 </funcsynopsis>
7479 <para>In recent libxml2 versions, this
7480 value contains a column number of the error or 0 if N/A.</para>
7481 </listitem>
7482 </varlistentry>
7483 <varlistentry>
7484 <term>context</term>
7485 <listitem>
7486 <funcsynopsis>
7487 <funcsynopsisinfo>$string = $@->context();</funcsynopsisinfo>
7488 </funcsynopsis>
7489 <para>For parsing errors, this field contains
7490 about 80 characters of the XML near the place
7491 where the error occurred. The field
7492 <function>$@->column()</function>
7493 contains the corresponding offset.
7494 Where N/A, the field is undefined.
7495 </para>
7496 </listitem>
7497 </varlistentry>
7498 <varlistentry>
7499 <term>column</term>
7500 <listitem>
7501 <funcsynopsis>
7502 <funcsynopsisinfo>$offset = $@->column();</funcsynopsisinfo>
7503 </funcsynopsis>
7504 <para>See <function>$@->column()</function> above.
7505 </para>
7506 </listitem>
7507 </varlistentry>
7508 <varlistentry>
7509 <term>_prev</term>
7510 <listitem>
7511 <funcsynopsis>
7512 <funcsynopsisinfo>$previous_error = $@->_prev();</funcsynopsisinfo>
7513 </funcsynopsis>
7514 <para>This field can possibly hold a reference to another
7515 XML::LibXML::Error object representing an error which
7516 occurred just before this error.</para>
7517 </listitem>
7518 </varlistentry>
7519 </variablelist>
7520 </sect1>
7521 </chapter>
7522 <chapter id="XML-LibXML-ErrNo">
7523 <title>Structured Errors</title>
7524 <titleabbrev>XML::LibXML::ErrNo</titleabbrev>
7525 <sect1>
7526 <title>Description</title>
7527 <para>This module is based on xmlerror.h libxml2 C header file.
7528 It defines symbolic constants for all libxml2 error codes.
7529 Currently libxml2 uses over 480 different error codes.
7530 See also XML::LibXML::Error.
7531 </para>
7532 </sect1>
7533 </chapter>
7534 <chapter id="XML-LibXML-Common">
7535 <title>Constants and Character Encoding Routines</title>
7536 <titleabbrev>XML::LibXML::Common</titleabbrev>
7537 <sect1>
7538 <title>Synopsis</title>
7539 <programlisting>use XML::LibXML::Common;</programlisting>
7540 </sect1>
7541 <sect1>
7542 <title>Description</title>
7543 <para>
7544 XML::LibXML::Common defines constants for all node types
7545 and provides interface to libxml2 charset conversion
7546 functions.
7547 </para>
7548 <para>Since XML::LibXML use their own node type definitions,
7549 one may want to use XML::LibXML::Common in its compatibility
7550 mode:
7551 </para>
7552 <sect2>
7553 <title>Exporter TAGS</title>
7554 <programlisting>use XML::LibXML::Common qw(:libxml);</programlisting>
7555 <para><literal>:libxml</literal> tag will use the XML::LibXML Compatibility mode, which defines the
7556 old 'XML_' node-type definitions.</para>
7557 <programlisting>use XML::LibXML::Common qw(:gdome);</programlisting>
7558 <para><literal>:gdome</literal> tag will use the XML::GDOME Compatibility mode, which defines the
7559 old 'GDOME_' node-type definitions.</para>
7560 <programlisting>use XML::LibXML::Common qw(:w3c);</programlisting>
7561 <para>This uses the nodetype definition names as specified for DOM.</para>
7562 <programlisting>use XML::LibXML::Common qw(:encoding);</programlisting>
7563 <para>
7564 This tag can be used to export only the charset encoding functions of XML::LibXML::Common.
7565 </para>
7566 </sect2>
7567 <sect2>
7568 <title>Exports</title>
7569 <para>
7570 By default the W3 definitions as defined in the DOM specifications and
7571 the encoding functions are exported by XML::LibXML::Common.
7572 </para>
7573 </sect2>
7574 <sect2>
7575 <title>Encoding functions</title>
7576 <para>
7577 To encode or decode a string to or from UTF-8, XML::LibXML::Common exports
7578 two functions, which provide an interface to the encoding support in <literal>libxml2</literal>.
7579 Which encodings are supported by these functions depends
7580 on how <literal>libxml2</literal> was compiled. UTF-16 is
7581 always supported and on most installations, ISO encodings are
7582 supported as well.
7583 </para>
7584 <para>
7585 This interface was useful for older versions of Perl.
7586 Since Perl >= 5.8 provides similar functions via the <literal>Encode</literal> module,
7587 it is probably a good idea to use those instead.
7588 </para>
7589 <variablelist>
7590 <varlistentry>
7591 <term>encodeToUTF8</term>
7592 <listitem>
7593 <funcsynopsis>
7594 <funcsynopsisinfo>$encodedstring = encodeToUTF8( $name_of_encoding, $sting_to_encode );</funcsynopsisinfo>
7595 </funcsynopsis>
7596 <para>The function will convert a byte string from the specified encoding to an UTF-8 encoded character string.</para>
7597 </listitem>
7598 </varlistentry>
7599 <varlistentry>
7600 <term>decodeToUTF8</term>
7601 <listitem>
7602 <funcsynopsis>
7603 <funcsynopsisinfo>$decodedstring = decodeFromUTF8($name_of_encoding, $string_to_decode );</funcsynopsisinfo>
7604 </funcsynopsis>
7605 <para>
7606 This function converts an UTF-8 encoded character string to a specified
7607 encoding. Note that the conversion can raise an error if the
7608 given string contains characters that cannot be represented in the target encoding.
7609 </para>
7610 </listitem>
7611 </varlistentry>
7612 </variablelist>
7613 <para>Both these functions report their errors on the standard
7614 error. If an error occurs the function will croak(). To catch
7615 the error information it is required to call the encoding
7616 function from within an eval block in order to prevent the
7617 entire script from being stopped on encoding error.</para>
7618 </sect2>
7619 <sect2>
7620 <title>A note on history</title>
7621 <para>
7622 Before XML::LibXML 1.70, this class was available as a
7623 separate CPAN distribution, intended to provide functionality
7624 shared between XML::LibXML, XML::GDOME, and possibly other
7625 modules. Since there seems to be no progress in this
7626 direction, we decided to merge XML::LibXML::Common 0.13 and
7627 XML::LibXML 1.70 to one CPAN distribution.
7628 </para>
7629 <para>The merge also naturally eliminates a practical and
7630 urgent problem experienced by many XML::LibXML users on certain
7631 platforms, namely mysterious misbehavior of XML::LibXML
7632 occurring if the installed (often pre-packaged) version of
7633 XML::LibXML::Common was compiled against an older version of
7634 libxml2 than XML::LibXML.
7635 </para>
7636 </sect2>
7637 </sect1>
7638 </chapter>
7639 </book>
7640
7641