Debian Codegraph @

lib/XML/LibXML/Document.pod

raw
1 =head1 NAME
2
3 XML::LibXML::Document - XML::LibXML DOM Document Class
4
5 =head1 SYNOPSIS
6
7
8
9 use XML::LibXML;
10 # Only methods specific to Document nodes are listed here,
11 # see the XML::LibXML::Node manpage for other methods
12
13 $dom = XML::LibXML::Document->new( $version, $encoding );
14 $dom = XML::LibXML::Document->createDocument( $version, $encoding );
15 $strURI = $doc->URI();
16 $doc->setURI($strURI);
17 $strEncoding = $doc->encoding();
18 $strEncoding = $doc->actualEncoding();
19 $doc->setEncoding($new_encoding);
20 $strVersion = $doc->version();
21 $doc->standalone
22 $doc->setStandalone($numvalue);
23 my $compression = $doc->compression;
24 $doc->setCompression($ziplevel);
25 $docstring = $dom->toString($format);
26 $c14nstr = $doc->toStringC14N($comment_flag, $xpath [, $xpath_context ]);
27 $ec14nstr = $doc->toStringEC14N($comment_flag, $xpath [, $xpath_context ], $inclusive_prefix_list);
28 $str = $doc->serialize($format);
29 $state = $doc->toFile($filename, $format);
30 $state = $doc->toFH($fh, $format);
31 $str = $document->toStringHTML();
32 $str = $document->serialize_html();
33 $bool = $dom->is_valid();
34 $dom->validate();
35 $root = $dom->documentElement();
36 $dom->setDocumentElement( $root );
37 $element = $dom->createElement( $nodename );
38 $element = $dom->createElementNS( $namespaceURI, $nodename );
39 $text = $dom->createTextNode( $content_text );
40 $comment = $dom->createComment( $comment_text );
41 $attrnode = $doc->createAttribute($name [,$value]);
42 $attrnode = $doc->createAttributeNS( namespaceURI, $name [,$value] );
43 $fragment = $doc->createDocumentFragment();
44 $cdata = $dom->createCDATASection( $cdata_content );
45 my $pi = $doc->createProcessingInstruction( $target, $data );
46 my $entref = $doc->createEntityReference($refname);
47 $dtd = $document->createInternalSubset( $rootnode, $public, $system);
48 $dtd = $document->createExternalSubset( $rootnode_name, $publicId, $systemId);
49 $document->importNode( $node );
50 $document->adoptNode( $node );
51 my $dtd = $doc->externalSubset;
52 my $dtd = $doc->internalSubset;
53 $doc->setExternalSubset($dtd);
54 $doc->setInternalSubset($dtd);
55 my $dtd = $doc->removeExternalSubset();
56 my $dtd = $doc->removeInternalSubset();
57 my @nodelist = $doc->getElementsByTagName($tagname);
58 my @nodelist = $doc->getElementsByTagNameNS($nsURI,$tagname);
59 my @nodelist = $doc->getElementsByLocalName($localname);
60 my $node = $doc->getElementById($id);
61 $dom->indexElements();
62
63 =head1 DESCRIPTION
64
65 The Document Class is in most cases the result of a parsing process. But
66 sometimes it is necessary to create a Document from scratch. The DOM Document
67 Class provides functions that conform to the DOM Core naming style.
68
69 It inherits all functions from L<<<<<< XML::LibXML::Node >>>>>> as specified in the DOM specification. This enables access to the nodes besides
70 the root element on document level - a C<<<<<< DTD >>>>>> for example. The support for these nodes is limited at the moment.
71
72 While generally nodes are bound to a document in the DOM concept it is
73 suggested that one should always create a node not bound to any document. There
74 is no need of really including the node to the document, but once the node is
75 bound to a document, it is quite safe that all strings have the correct
76 encoding. If an unbound text node with an ISO encoded string is created (e.g.
77 with $CLASS->new()), the C<<<<<< toString >>>>>> function may not return the expected result.
78
79 To prevent such problems, it is recommended to pass all data to XML::LibXML
80 methods as character strings (i.e. UTF-8 encoded, with the UTF8 flag on).
81
82
83 =head1 METHODS
84
85 Many functions listed here are extensively documented in the DOM Level 3 specification (L<<<<<< http://www.w3.org/TR/DOM-Level-3-Core/ >>>>>>). Please refer to the specification for extensive documentation.
86
87 =over 4
88
89 =item new
90
91 $dom = XML::LibXML::Document->new( $version, $encoding );
92
93 alias for createDocument()
94
95
96 =item createDocument
97
98 $dom = XML::LibXML::Document->createDocument( $version, $encoding );
99
100 The constructor for the document class. As Parameter it takes the version
101 string and (optionally) the encoding string. Simply calling I<<<<<< createDocument >>>>>>() will create the document:
102
103
104
105 <?xml version="your version" encoding="your encoding"?>
106
107 Both parameter are optional. The default value for I<<<<<< $version >>>>>> is C<<<<<< 1.0 >>>>>>, of course. If the I<<<<<< $encoding >>>>>> parameter is not set, the encoding will be left unset, which means UTF-8 is
108 implied.
109
110 The call of I<<<<<< createDocument >>>>>>() without any parameter will result the following code:
111
112
113
114 <?xml version="1.0"?>
115
116 Alternatively one can call this constructor directly from the XML::LibXML class
117 level, to avoid some typing. This will not have any effect on the class
118 instance, which is always XML::LibXML::Document.
119
120
121
122 my $document = XML::LibXML->createDocument( "1.0", "UTF-8" );
123
124 is therefore a shortcut for
125
126
127
128 my $document = XML::LibXML::Document->createDocument( "1.0", "UTF-8" );
129
130
131 =item URI
132
133 $strURI = $doc->URI();
134
135 Returns the URI (or filename) of the original document. For documents obtained
136 by parsing a string of a FH without using the URI parsing argument of the
137 corresponding C<<<<<< parse_* >>>>>> function, the result is a generated string unknown-XYZ where XYZ is some
138 number; for documents created with the constructor C<<<<<< new >>>>>>, the URI is undefined.
139
140 The value can be modified by calling C<<<<<< setURI >>>>>> method on the document node.
141
142
143 =item setURI
144
145 $doc->setURI($strURI);
146
147 Sets the URI of the document reported by the method URI (see also the URI
148 argument to the various C<<<<<< parse_* >>>>>> functions).
149
150
151 =item encoding
152
153 $strEncoding = $doc->encoding();
154
155 returns the encoding string of the document.
156
157
158
159 my $doc = XML::LibXML->createDocument( "1.0", "ISO-8859-15" );
160 print $doc->encoding; # prints ISO-8859-15
161
162
163 =item actualEncoding
164
165 $strEncoding = $doc->actualEncoding();
166
167 returns the encoding in which the XML will be returned by $doc->toString().
168 This is usually the original encoding of the document as declared in the XML
169 declaration and returned by $doc->encoding. If the original encoding is not
170 known (e.g. if created in memory or parsed from a XML without a declared
171 encoding), 'UTF-8' is returned.
172
173
174
175 my $doc = XML::LibXML->createDocument( "1.0", "ISO-8859-15" );
176 print $doc->encoding; # prints ISO-8859-15
177
178
179 =item setEncoding
180
181 $doc->setEncoding($new_encoding);
182
183 This method allows one to change the declaration of encoding in the XML
184 declaration of the document. The value also affects the encoding in which the
185 document is serialized to XML by $doc->toString(). Use setEncoding() to remove
186 the encoding declaration.
187
188
189 =item version
190
191 $strVersion = $doc->version();
192
193 returns the version string of the document
194
195 I<<<<<< getVersion() >>>>>> is an alternative form of this function.
196
197
198 =item standalone
199
200 $doc->standalone
201
202 This function returns the Numerical value of a documents XML declarations
203 standalone attribute. It returns I<<<<<< 1 >>>>>> if standalone="yes" was found, I<<<<<< 0 >>>>>> if standalone="no" was found and I<<<<<< -1 >>>>>> if standalone was not specified (default on creation).
204
205
206 =item setStandalone
207
208 $doc->setStandalone($numvalue);
209
210 Through this method it is possible to alter the value of a documents standalone
211 attribute. Set it to I<<<<<< 1 >>>>>> to set standalone="yes", to I<<<<<< 0 >>>>>> to set standalone="no" or set it to I<<<<<< -1 >>>>>> to remove the standalone attribute from the XML declaration.
212
213
214 =item compression
215
216 my $compression = $doc->compression;
217
218 libxml2 allows reading of documents directly from gzipped files. In this case
219 the compression variable is set to the compression level of that file (0-8). If
220 XML::LibXML parsed a different source or the file wasn't compressed, the
221 returned value will be I<<<<<< -1 >>>>>>.
222
223
224 =item setCompression
225
226 $doc->setCompression($ziplevel);
227
228 If one intends to write the document directly to a file, it is possible to set
229 the compression level for a given document. This level can be in the range from
230 0 to 8. If XML::LibXML should not try to compress use I<<<<<< -1 >>>>>> (default).
231
232 Note that this feature will I<<<<<< only >>>>>> work if libxml2 is compiled with zlib support and toFile() is used for output.
233
234
235 =item toString
236
237 $docstring = $dom->toString($format);
238
239 I<<<<<< toString >>>>>> is a DOM serializing function, so the DOM Tree is serialized into an XML
240 string, ready for output.
241
242 IMPORTANT: unlike toString for other nodes, on document nodes this function
243 returns the XML as a byte string in the original encoding of the document (see
244 the actualEncoding() method)! This means you can simply do:
245
246
247
248 open my $out_fh, '>', $file;
249 print {$out_fh} $doc->toString;
250
251 regardless of the actual encoding of the document. See the section on encodings
252 in L<<<<<< XML::LibXML >>>>>> for more details.
253
254 The optional I<<<<<< $format >>>>>> parameter sets the indenting of the output. This parameter is expected to be an C<<<<<< integer >>>>>> value, that specifies that indentation should be used. The format parameter can
255 have three different values if it is used:
256
257 If $format is 0, than the document is dumped as it was originally parsed
258
259 If $format is 1, libxml2 will add ignorable white spaces, so the nodes content
260 is easier to read. Existing text nodes will not be altered
261
262 If $format is 2 (or higher), libxml2 will act as $format == 1 but it add a
263 leading and a trailing line break to each text node.
264
265 libxml2 uses a hard-coded indentation of 2 space characters per indentation
266 level. This value can not be altered on run-time.
267
268
269 =item toStringC14N
270
271 $c14nstr = $doc->toStringC14N($comment_flag, $xpath [, $xpath_context ]);
272
273 See the documentation in L<<<<<< XML::LibXML::Node >>>>>>.
274
275
276 =item toStringEC14N
277
278 $ec14nstr = $doc->toStringEC14N($comment_flag, $xpath [, $xpath_context ], $inclusive_prefix_list);
279
280 See the documentation in L<<<<<< XML::LibXML::Node >>>>>>.
281
282
283 =item serialize
284
285 $str = $doc->serialize($format);
286
287 An alias for toString(). This function was name added to be more consistent
288 with libxml2.
289
290
291 =item serialize_c14n
292
293 An alias for toStringC14N().
294
295
296 =item serialize_exc_c14n
297
298 An alias for toStringEC14N().
299
300
301 =item toFile
302
303 $state = $doc->toFile($filename, $format);
304
305 This function is similar to toString(), but it writes the document directly
306 into a filesystem. This function is very useful, if one needs to store large
307 documents.
308
309 The format parameter has the same behaviour as in toString().
310
311
312 =item toFH
313
314 $state = $doc->toFH($fh, $format);
315
316 This function is similar to toString(), but it writes the document directly to
317 a filehandle or a stream. A byte stream in the document encoding is passed to
318 the file handle. Do NOT apply any C<<<<<< :encoding(...) >>>>>> or C<<<<<< :utf8 >>>>>> PerlIO layer to the filehandle! See the section on encodings in L<<<<<< XML::LibXML >>>>>> for more details.
319
320 The format parameter has the same behaviour as in toString().
321
322
323 =item toStringHTML
324
325 $str = $document->toStringHTML();
326
327 I<<<<<< toStringHTML >>>>>> serialize the tree to a byte string in the document encoding as HTML. With this
328 method indenting is automatic and managed by libxml2 internally.
329
330
331 =item serialize_html
332
333 $str = $document->serialize_html();
334
335 An alias for toStringHTML().
336
337
338 =item is_valid
339
340 $bool = $dom->is_valid();
341
342 Returns either TRUE or FALSE depending on whether the DOM Tree is a valid
343 Document or not.
344
345 You may also pass in a L<<<<<< XML::LibXML::Dtd >>>>>> object, to validate against an external DTD:
346
347
348
349 if (!$dom->is_valid($dtd)) {
350 warn("document is not valid!");
351 }
352
353
354 =item validate
355
356 $dom->validate();
357
358 This is an exception throwing equivalent of is_valid. If the document is not
359 valid it will throw an exception containing the error. This allows you much
360 better error reporting than simply is_valid or not.
361
362 Again, you may pass in a DTD object
363
364
365 =item documentElement
366
367 $root = $dom->documentElement();
368
369 Returns the root element of the Document. A document can have just one root
370 element to contain the documents data.
371
372 Optionally one can use I<<<<<< getDocumentElement >>>>>>.
373
374
375 =item setDocumentElement
376
377 $dom->setDocumentElement( $root );
378
379 This function enables you to set the root element for a document. The function
380 supports the import of a node from a different document tree, but does not
381 support a document fragment as $root.
382
383
384 =item createElement
385
386 $element = $dom->createElement( $nodename );
387
388 This function creates a new Element Node bound to the DOM with the name C<<<<<< $nodename >>>>>>.
389
390
391 =item createElementNS
392
393 $element = $dom->createElementNS( $namespaceURI, $nodename );
394
395 This function creates a new Element Node bound to the DOM with the name C<<<<<< $nodename >>>>>> and placed in the given namespace.
396
397
398 =item createTextNode
399
400 $text = $dom->createTextNode( $content_text );
401
402 As an equivalent of I<<<<<< createElement >>>>>>, but it creates a I<<<<<< Text Node >>>>>> bound to the DOM.
403
404
405 =item createComment
406
407 $comment = $dom->createComment( $comment_text );
408
409 As an equivalent of I<<<<<< createElement >>>>>>, but it creates a I<<<<<< Comment Node >>>>>> bound to the DOM.
410
411
412 =item createAttribute
413
414 $attrnode = $doc->createAttribute($name [,$value]);
415
416 Creates a new Attribute node.
417
418
419 =item createAttributeNS
420
421 $attrnode = $doc->createAttributeNS( namespaceURI, $name [,$value] );
422
423 Creates an Attribute bound to a namespace.
424
425
426 =item createDocumentFragment
427
428 $fragment = $doc->createDocumentFragment();
429
430 This function creates a DocumentFragment.
431
432
433 =item createCDATASection
434
435 $cdata = $dom->createCDATASection( $cdata_content );
436
437 Similar to createTextNode and createComment, this function creates a
438 CDataSection bound to the current DOM.
439
440
441 =item createProcessingInstruction
442
443 my $pi = $doc->createProcessingInstruction( $target, $data );
444
445 create a processing instruction node.
446
447 Since this method is quite long one may use its short form I<<<<<< createPI() >>>>>>.
448
449
450 =item createEntityReference
451
452 my $entref = $doc->createEntityReference($refname);
453
454 If a document has a DTD specified, one can create entity references by using
455 this function. If one wants to add a entity reference to the document, this
456 reference has to be created by this function.
457
458 An entity reference is unique to a document and cannot be passed to other
459 documents as other nodes can be passed.
460
461 I<<<<<< NOTE: >>>>>> A text content containing something that looks like an entity reference, will
462 not be expanded to a real entity reference unless it is a predefined entity
463
464
465
466 my $string = "&foo;";
467 $some_element->appendText( $string );
468 print $some_element->textContent; # prints "&amp;foo;"
469
470
471 =item createInternalSubset
472
473 $dtd = $document->createInternalSubset( $rootnode, $public, $system);
474
475 This function creates and adds an internal subset to the given document.
476 Because the function automatically adds the DTD to the document there is no
477 need to add the created node explicitly to the document.
478
479
480
481 my $document = XML::LibXML::Document->new();
482 my $dtd = $document->createInternalSubset( "foo", undef, "foo.dtd" );
483
484 will result in the following XML document:
485
486
487
488 <?xml version="1.0"?>
489 <!DOCTYPE foo SYSTEM "foo.dtd">
490
491 By setting the public parameter it is possible to set PUBLIC DTDs to a given
492 document. So
493
494
495
496 my $document = XML::LibXML::Document->new();
497 my $dtd = $document->createInternalSubset( "foo", "-//FOO//DTD FOO 0.1//EN", undef );
498
499 will cause the following declaration to be created on the document:
500
501
502
503 <?xml version="1.0"?>
504 <!DOCTYPE foo PUBLIC "-//FOO//DTD FOO 0.1//EN">
505
506
507 =item createExternalSubset
508
509 $dtd = $document->createExternalSubset( $rootnode_name, $publicId, $systemId);
510
511 This function is similar to C<<<<<< createInternalSubset() >>>>>> but this DTD is considered to be external and is therefore not added to the
512 document itself. Nevertheless it can be used for validation purposes.
513
514
515 =item importNode
516
517 $document->importNode( $node );
518
519 If a node is not part of a document, it can be imported to another document. As
520 specified in DOM Level 2 Specification the Node will not be altered or removed
521 from its original document (C<<<<<< $node-E<gt>cloneNode(1) >>>>>> will get called implicitly).
522
523 I<<<<<< NOTE: >>>>>> Don't try to use importNode() to import sub-trees that contain an entity
524 reference - even if the entity reference is the root node of the sub-tree. This
525 will cause serious problems to your program. This is a limitation of libxml2
526 and not of XML::LibXML itself.
527
528
529 =item adoptNode
530
531 $document->adoptNode( $node );
532
533 If a node is not part of a document, it can be imported to another document. As
534 specified in DOM Level 3 Specification the Node will not be altered but it will
535 removed from its original document.
536
537 After a document adopted a node, the node, its attributes and all its
538 descendants belong to the new document. Because the node does not belong to the
539 old document, it will be unlinked from its old location first.
540
541 I<<<<<< NOTE: >>>>>> Don't try to adoptNode() to import sub-trees that contain entity references -
542 even if the entity reference is the root node of the sub-tree. This will cause
543 serious problems to your program. This is a limitation of libxml2 and not of
544 XML::LibXML itself.
545
546
547 =item externalSubset
548
549 my $dtd = $doc->externalSubset;
550
551 If a document has an external subset defined it will be returned by this
552 function.
553
554 I<<<<<< NOTE >>>>>> Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in
555 XML::LibXML is still limited. In particular one may not want use common node
556 function on doctype declaration nodes!
557
558
559 =item internalSubset
560
561 my $dtd = $doc->internalSubset;
562
563 If a document has an internal subset defined it will be returned by this
564 function.
565
566 I<<<<<< NOTE >>>>>> Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in
567 XML::LibXML is still limited. In particular one may not want use common node
568 function on doctype declaration nodes!
569
570
571 =item setExternalSubset
572
573 $doc->setExternalSubset($dtd);
574
575 I<<<<<< EXPERIMENTAL! >>>>>>
576
577 This method sets a DTD node as an external subset of the given document.
578
579
580 =item setInternalSubset
581
582 $doc->setInternalSubset($dtd);
583
584 I<<<<<< EXPERIMENTAL! >>>>>>
585
586 This method sets a DTD node as an internal subset of the given document.
587
588
589 =item removeExternalSubset
590
591 my $dtd = $doc->removeExternalSubset();
592
593 I<<<<<< EXPERIMENTAL! >>>>>>
594
595 If a document has an external subset defined it can be removed from the
596 document by using this function. The removed dtd node will be returned.
597
598
599 =item removeInternalSubset
600
601 my $dtd = $doc->removeInternalSubset();
602
603 I<<<<<< EXPERIMENTAL! >>>>>>
604
605 If a document has an internal subset defined it can be removed from the
606 document by using this function. The removed dtd node will be returned.
607
608
609 =item getElementsByTagName
610
611 my @nodelist = $doc->getElementsByTagName($tagname);
612
613 Implements the DOM Level 2 function
614
615 In SCALAR context this function returns an L<<<<<< XML::LibXML::NodeList >>>>>> object.
616
617
618 =item getElementsByTagNameNS
619
620 my @nodelist = $doc->getElementsByTagNameNS($nsURI,$tagname);
621
622 Implements the DOM Level 2 function
623
624 In SCALAR context this function returns an L<<<<<< XML::LibXML::NodeList >>>>>> object.
625
626
627 =item getElementsByLocalName
628
629 my @nodelist = $doc->getElementsByLocalName($localname);
630
631 This allows the fetching of all nodes from a given document with the given
632 Localname.
633
634 In SCALAR context this function returns an L<<<<<< XML::LibXML::NodeList >>>>>> object.
635
636
637 =item getElementById
638
639 my $node = $doc->getElementById($id);
640
641 Returns the element that has an ID attribute with the given value. If no such
642 element exists, this returns undef.
643
644 Note: the ID of an element may change while manipulating the document. For
645 documents with a DTD, the information about ID attributes is only available if
646 DTD loading/validation has been requested. For HTML documents parsed with the
647 HTML parser ID detection is done automatically. In XML documents, all "xml:id"
648 attributes are considered to be of type ID. You can test ID-ness of an
649 attribute node with $attr->isId().
650
651 In versions 1.59 and earlier this method was called getElementsById() (plural)
652 by mistake. Starting from 1.60 this name is maintained as an alias only for
653 backward compatibility.
654
655
656 =item indexElements
657
658 $dom->indexElements();
659
660 This function causes libxml2 to stamp all elements in a document with their
661 document position index which considerably speeds up XPath queries for large
662 documents. It should only be used with static documents that won't be further
663 changed by any DOM methods, because once a document is indexed, XPath will
664 always prefer the index to other methods of determining the document order of
665 nodes. XPath could therefore return improperly ordered node-lists when applied
666 on a document that has been changed after being indexed. It is of course
667 possible to use this method to re-index a modified document before using it
668 with XPath again. This function is not a part of the DOM specification.
669
670 This function returns number of elements indexed, -1 if error occurred, or -2
671 if this feature is not available in the running libxml2.
672
673
674
675 =back
676
677 =head1 AUTHORS
678
679 Matt Sergeant,
680 Christian Glahn,
681 Petr Pajas
682
683
684 =head1 VERSION
685
686 2.0134
687
688 =head1 COPYRIGHT
689
690 2001-2007, AxKit.com Ltd.
691
692 2002-2006, Christian Glahn.
693
694 2006-2009, Petr Pajas.
695
696 =cut
697
698
699 =head1 LICENSE
700
701 This program is free software; you can redistribute it and/or modify it under
702 the same terms as Perl itself.
703