~ubuntu-branches/ubuntu/saucy/lxml/saucy-updates

« back to all changes in this revision

Viewing changes to doc/tutorial.txt

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-27 22:14:53 UTC
  • mto: (2.1.34 experimental) (1.4.1)
  • mto: This revision was merged to the branch mainline in revision 17.
  • Revision ID: package-import@ubuntu.com-20130127221453-2k7oc1crqc28802y
Tags: upstream-3.1~beta1
ImportĀ upstreamĀ versionĀ 3.1~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
171
171
Elements are lists
172
172
------------------
173
173
 
174
 
To make the access to these subelements as easy and straight forward as
175
 
possible, elements behave like normal Python lists:
 
174
To make the access to these subelements easy and straight forward,
 
175
elements mimic the behaviour of normal Python lists as closely as
 
176
possible:
176
177
 
177
178
.. sourcecode:: pycon
178
179
 
203
204
    >>> print(end[0].tag)
204
205
    child3
205
206
 
206
 
    >>> root[0] = root[-1] # this moves the element in lxml.etree!
207
 
    >>> for child in root:
208
 
    ...     print(child.tag)
209
 
    child3
210
 
    child1
211
 
    child2
212
 
 
213
207
Prior to ElementTree 1.3 and lxml 2.0, you could also check the truth value of
214
 
an Element to see if it has children, i.e. if the list of children is empty.
215
 
This is no longer supported as people tend to find it surprising that a
216
 
non-None reference to an existing Element can evaluate to False.  Instead, use
217
 
``len(element)``, which is both more explicit and less error prone.
218
 
 
219
 
Note in the examples that the last element was *moved* to a different position
220
 
in the last example.  This is a difference from the original ElementTree (and
221
 
from lists), where elements can sit in multiple positions of any number of
222
 
trees.  In lxml.etree, elements can only sit in one position of one tree at a
223
 
time.
224
 
 
225
 
If you want to *copy* an element to a different position, consider creating an
226
 
independent *deep copy* using the ``copy`` module from Python's standard
227
 
library:
 
208
an Element to see if it has children, i.e. if the list of children is empty:
 
209
 
 
210
.. sourcecode:: python
 
211
 
 
212
   if root:   # this no longer works!
 
213
       print("The root element has children")
 
214
 
 
215
This is no longer supported as people tend to expect that a "something"
 
216
evaluates to True and expect Elements to be "something", may they have
 
217
children or not.  So, many users find it surprising that any Element
 
218
would evaluate to False in an if-statement like the above.  Instead,
 
219
use ``len(element)``, which is both more explicit and less error prone.
 
220
 
 
221
.. sourcecode:: pycon
 
222
 
 
223
   >>> print(etree.iselement(root))  # test if it's some kind of Element
 
224
   True
 
225
   >>> if len(root):                 # test if it has children
 
226
   ...     print("The root element has children")
 
227
   The root element has children
 
228
 
 
229
There is another important case where the behaviour of Elements in lxml
 
230
(in 2.0 and later) deviates from that of lists and from that of the
 
231
original ElementTree (prior to version 1.3 or Python 2.7/3.2):
 
232
 
 
233
.. sourcecode:: pycon
 
234
 
 
235
    >>> for child in root:
 
236
    ...     print(child.tag)
 
237
    child0
 
238
    child1
 
239
    child2
 
240
    child3
 
241
    >>> root[0] = root[-1]  # this moves the element in lxml.etree!
 
242
    >>> for child in root:
 
243
    ...     print(child.tag)
 
244
    child3
 
245
    child1
 
246
    child2
 
247
 
 
248
In this example, the last element is *moved* to a different position,
 
249
instead of being copied, i.e. it is automatically removed from its
 
250
previous position when it is put in a different place.  In lists,
 
251
objects can appear in multiple positions at the same time, and the
 
252
above assignment would just copy the item reference into the first
 
253
position, so that both contain the exact same item:
 
254
 
 
255
.. sourcecode:: pycon
 
256
 
 
257
   >>> l = [0, 1, 2, 3]
 
258
   >>> l[0] = l[-1]
 
259
   >>> l
 
260
   [3, 1, 2, 3]
 
261
 
 
262
Note that in the original ElementTree, a single Element object can sit
 
263
in any number of places in any number of trees, which allows for the same
 
264
copy operation as with lists.  The obvious drawback is that modifications
 
265
to such an Element will apply to all places where it appears in a tree,
 
266
which may or may not be intended.
 
267
 
 
268
The upside of this difference is that an Element in lxml.etree always
 
269
has exactly one parent, which can be queried through the ``getparent()``
 
270
method.  This is not supported in the original ElementTree.
 
271
 
 
272
.. sourcecode:: pycon
 
273
 
 
274
    >>> root is root[0].getparent()  # lxml.etree only!
 
275
    True
 
276
 
 
277
If you want to *copy* an element to a different position in lxml.etree,
 
278
consider creating an independent *deep copy* using the ``copy`` module
 
279
from Python's standard library:
228
280
 
229
281
.. sourcecode:: pycon
230
282
 
238
290
    >>> print([ c.tag for c in root ])
239
291
    ['child3', 'child1', 'child2']
240
292
 
241
 
The way up in the tree is provided through the ``getparent()`` method:
242
 
 
243
 
.. sourcecode:: pycon
244
 
 
245
 
    >>> root is root[0].getparent()  # lxml.etree only!
246
 
    True
247
 
 
248
293
The siblings (or neighbours) of an element are accessed as next and previous
249
294
elements:
250
295
 
256
301
    True
257
302
 
258
303
 
259
 
Elements carry attributes
260
 
-------------------------
 
304
Elements carry attributes as a dict
 
305
-----------------------------------
261
306
 
262
307
XML elements support attributes.  You can create them directly in the Element
263
308
factory:
268
313
    >>> etree.tostring(root)
269
314
    b'<root interesting="totally"/>'
270
315
 
271
 
Fast and direct access to these attributes is provided by the ``set()`` and
272
 
``get()`` methods of elements:
 
316
Attributes are just unordered name-value pairs, so a very convenient way
 
317
of dealing with them is through the dictionary-like interface of Elements:
273
318
 
274
319
.. sourcecode:: pycon
275
320
 
276
321
    >>> print(root.get("interesting"))
277
322
    totally
278
323
 
279
 
    >>> root.set("interesting", "somewhat")
280
 
    >>> print(root.get("interesting"))
281
 
    somewhat
282
 
 
283
 
However, a very convenient way of dealing with them is through the dictionary
284
 
interface of the ``attrib`` property:
 
324
    >>> print(root.get("hello"))
 
325
    None
 
326
    >>> root.set("hello", "Huhu")
 
327
    >>> print(root.get("hello"))
 
328
    Huhu
 
329
 
 
330
    >>> etree.tostring(root)
 
331
    b'<root interesting="totally" hello="Huhu"/>'
 
332
 
 
333
    >>> sorted(root.keys())
 
334
    ['hello', 'interesting']
 
335
 
 
336
    >>> for name, value in sorted(root.items()):
 
337
    ...     print('%s = %r' % (name, value))
 
338
    hello = 'Huhu'
 
339
    interesting = 'totally'
 
340
 
 
341
For the cases where you want to do item lookup or have other reasons for
 
342
getting a 'real' dictionary-like object, e.g. for passing it around,
 
343
you can use the ``attrib`` property:
285
344
 
286
345
.. sourcecode:: pycon
287
346
 
288
347
    >>> attributes = root.attrib
289
348
 
290
349
    >>> print(attributes["interesting"])
291
 
    somewhat
292
 
 
293
 
    >>> print(attributes.get("hello"))
 
350
    totally
 
351
    >>> print(attributes.get("no-such-attribute"))
294
352
    None
295
353
 
296
354
    >>> attributes["hello"] = "Guten Tag"
297
 
    >>> print(attributes.get("hello"))
 
355
    >>> print(attributes["hello"])
298
356
    Guten Tag
299
357
    >>> print(root.get("hello"))
300
358
    Guten Tag
301
359
 
 
360
Note that ``attrib`` is a dict-like object backed by the Element itself.
 
361
This means that any changes to the Element are reflected in ``attrib``
 
362
and vice versa.  It also means that the XML tree stays alive in memory
 
363
as long as the ``attrib`` of one of its Elements is in use.  To get an
 
364
independent snapshot of the attributes that does not depend on the XML
 
365
tree, copy it into a dict:
 
366
 
 
367
.. sourcecode:: pycon
 
368
 
 
369
    >>> d = dict(root.attrib)
 
370
    >>> sorted(d.items())
 
371
    [('hello', 'Guten Tag'), ('interesting', 'totally')]
 
372
 
302
373
 
303
374
Elements contain text
304
375
---------------------
479
550
    another - Child 3
480
551
 
481
552
If you know you are only interested in a single tag, you can pass its name to
482
 
``iter()`` to have it filter for you.  Since lxml 3.0, you can also pass more
483
 
than one tag to intercept on multiple tags during iteration.
 
553
``iter()`` to have it filter for you.  Starting with lxml 3.0, you can also
 
554
pass more than one tag to intercept on multiple tags during iteration.
484
555
 
485
556
.. sourcecode:: pycon
486
557
 
571
642
 
572
643
Note that pretty printing appends a newline at the end.
573
644
 
574
 
Since lxml 2.0 (and ElementTree 1.3), the serialisation functions can
575
 
do more than XML serialisation.  You can serialise to HTML or extract
576
 
the text content by passing the ``method`` keyword:
 
645
In lxml 2.0 and later (as well as ElementTree 1.3), the serialisation
 
646
functions can do more than XML serialisation.  You can serialise to
 
647
HTML or extract the text content by passing the ``method`` keyword:
577
648
 
578
649
.. sourcecode:: pycon
579
650
 
623
694
   u'HelloW\xf6rld'
624
695
 
625
696
The W3C has a good `article about the Unicode character set and
626
 
character encodings`_.
627
 
 
628
 
.. _`article about the Unicode character set and character encodings`: http://www.w3.org/International/tutorials/tutorial-char-enc/
 
697
character encodings
 
698
<http://www.w3.org/International/tutorials/tutorial-char-enc/>`_.
629
699
 
630
700
 
631
701
The ElementTree class
632
702
=====================
633
703
 
634
704
An ``ElementTree`` is mainly a document wrapper around a tree with a
635
 
root node.  It provides a couple of methods for parsing, serialisation
636
 
and general document handling.  One of the bigger differences is that
637
 
it serialises as a complete document, as opposed to a single
638
 
``Element``.  This includes top-level processing instructions and
639
 
comments, as well as a DOCTYPE and other DTD content in the document:
 
705
root node.  It provides a couple of methods for serialisation and
 
706
general document handling.
640
707
 
641
708
.. sourcecode:: pycon
642
709
 
643
 
    >>> tree = etree.parse(StringIO('''\
 
710
    >>> root = etree.XML('''\
644
711
    ... <?xml version="1.0"?>
645
 
    ... <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "eggs"> ]>
 
712
    ... <!DOCTYPE root SYSTEM "test" [ <!ENTITY tasty "parsnips"> ]>
646
713
    ... <root>
647
714
    ...   <a>&tasty;</a>
648
715
    ... </root>
649
 
    ... '''))
 
716
    ... ''')
650
717
 
 
718
    >>> tree = etree.ElementTree(root)
 
719
    >>> print(tree.docinfo.xml_version)
 
720
    1.0
651
721
    >>> print(tree.docinfo.doctype)
652
722
    <!DOCTYPE root SYSTEM "test">
653
723
 
654
 
    >>> # lxml 1.3.4 and later
655
 
    >>> print(etree.tostring(tree))
656
 
    <!DOCTYPE root SYSTEM "test" [
657
 
    <!ENTITY tasty "eggs">
658
 
    ]>
659
 
    <root>
660
 
      <a>eggs</a>
661
 
    </root>
662
 
 
663
 
    >>> # lxml 1.3.4 and later
664
 
    >>> print(etree.tostring(etree.ElementTree(tree.getroot())))
665
 
    <!DOCTYPE root SYSTEM "test" [
666
 
    <!ENTITY tasty "eggs">
667
 
    ]>
668
 
    <root>
669
 
      <a>eggs</a>
670
 
    </root>
671
 
 
672
 
    >>> # ElementTree and lxml <= 1.3.3
 
724
An ``ElementTree`` is also what you get back when you call the
 
725
``parse()`` function to parse files or file-like objects (see the
 
726
parsing section below).
 
727
 
 
728
One of the important differences is that the ``ElementTree`` class
 
729
serialises as a complete document, as opposed to a single ``Element``.
 
730
This includes top-level processing instructions and comments, as well
 
731
as a DOCTYPE and other DTD content in the document:
 
732
 
 
733
.. sourcecode:: pycon
 
734
 
 
735
    >>> print(etree.tostring(tree))  # lxml 1.3.4 and later
 
736
    <!DOCTYPE root SYSTEM "test" [
 
737
    <!ENTITY tasty "parsnips">
 
738
    ]>
 
739
    <root>
 
740
      <a>parsnips</a>
 
741
    </root>
 
742
 
 
743
In the original xml.etree.ElementTree implementation and in lxml
 
744
up to 1.3.3, the output looks the same as when serialising only
 
745
the root Element:
 
746
 
 
747
.. sourcecode:: pycon
 
748
 
673
749
    >>> print(etree.tostring(tree.getroot()))
674
750
    <root>
675
 
      <a>eggs</a>
 
751
      <a>parsnips</a>
676
752
    </root>
677
753
 
678
 
Note that this has changed in lxml 1.3.4 to match the behaviour of
679
 
lxml 2.0.  Before, the examples were serialised without DTD content,
680
 
which made lxml loose DTD information in an input-output cycle.
 
754
This serialisation behaviour has changed in lxml 1.3.4.  Before,
 
755
the tree was serialised without DTD content, which made lxml
 
756
loose DTD information in an input-output cycle.
681
757
 
682
758
 
683
759
Parsing from strings and files
721
797
    >>> etree.tostring(root)
722
798
    b'<root>data</root>'
723
799
 
 
800
There is also a corresponding function ``HTML()`` for HTML literals.
 
801
 
724
802
 
725
803
The parse() function
726
804
--------------------
727
805
 
728
 
The ``parse()`` function is used to parse from files and file-like objects:
 
806
The ``parse()`` function is used to parse from files and file-like objects.
 
807
 
 
808
As an example of such a file-like object, the following code uses the
 
809
``StringIO`` class for reading from a string instead of an external file.
 
810
That class comes from the ``StringIO`` module in Python 2.  In Python 2.6
 
811
and later, including Python 3.x, you would rather use the ``BytesIO`` class
 
812
from the ``io`` module.  However, in real life, you would obviously avoid
 
813
doing this all together and use the string parsing functions above.
729
814
 
730
815
.. sourcecode:: pycon
731
816
 
732
 
    >>> some_file_like = StringIO("<root>data</root>")
 
817
    >>> some_file_like_object = StringIO("<root>data</root>")
733
818
 
734
 
    >>> tree = etree.parse(some_file_like)
 
819
    >>> tree = etree.parse(some_file_like_object)
735
820
 
736
821
    >>> etree.tostring(tree)
737
822
    b'<root>data</root>'
763
848
* an HTTP or FTP URL string
764
849
 
765
850
Note that passing a filename or URL is usually faster than passing an
766
 
open file.
 
851
open file or file-like object.  However, the HTTP/FTP client in libxml2
 
852
is rather simple, so things like HTTP authentication require a dedicated
 
853
URL request library, e.g. ``urllib2`` or ``request``.  These libraries
 
854
usually provide a file-like object for the result that you can parse
 
855
from while the response is streaming in.
767
856
 
768
857
 
769
858
Parser objects
1009
1098
Namespaces
1010
1099
==========
1011
1100
 
1012
 
The ElementTree API avoids `namespace prefixes`_ wherever possible and deploys
1013
 
the real namespaces instead:
 
1101
The ElementTree API avoids
 
1102
`namespace prefixes <http://www.w3.org/TR/xml-names/#ns-qualnames>`_
 
1103
wherever possible and deploys the real namespaces (the URI) instead:
1014
1104
 
1015
1105
.. sourcecode:: pycon
1016
1106
 
1023
1113
      <html:body>Hello World</html:body>
1024
1114
    </html:html>
1025
1115
 
1026
 
.. _`namespace prefixes`: http://www.w3.org/TR/xml-names/#ns-qualnames
1027
 
 
1028
 
The notation that ElementTree uses was originally brought up by `James
1029
 
Clark`_.  It has the major advantage of providing a universally
1030
 
qualified name for a tag, regardless of any prefixes that may or may
1031
 
not have been used or defined in a document.  By moving the
1032
 
indirection of prefixes out of the way, it makes namespace aware code
1033
 
much clearer and safer.
1034
 
 
1035
 
.. _`James Clark`: http://www.jclark.com/xml/xmlns.htm
 
1116
The notation that ElementTree uses was originally brought up by
 
1117
`James Clark <http://www.jclark.com/xml/xmlns.htm>`_.  It has the major
 
1118
advantage of providing a universally qualified name for a tag, regardless
 
1119
of any prefixes that may or may not have been used or defined in a document.
 
1120
By moving the indirection of prefixes out of the way, it makes namespace
 
1121
aware code much clearer and easier to get right.
1036
1122
 
1037
1123
As you can see from the example, prefixes only become important when
1038
1124
you serialise the result.  However, the above code looks somewhat
1058
1144
      <body>Hello World</body>
1059
1145
    </html>
1060
1146
 
 
1147
You can also use the ``QName`` helper class to build or split qualified
 
1148
tag names:
 
1149
 
 
1150
.. sourcecode:: pycon
 
1151
 
 
1152
    >>> tag = etree.QName('http://www.w3.org/1999/xhtml', 'html')
 
1153
    >>> print(tag.localname)
 
1154
    html
 
1155
    >>> print(tag.namespace)
 
1156
    http://www.w3.org/1999/xhtml
 
1157
    >>> print(tag.text)
 
1158
    {http://www.w3.org/1999/xhtml}html
 
1159
 
 
1160
    >>> tag = etree.QName('{http://www.w3.org/1999/xhtml}html')
 
1161
    >>> print(tag.localname)
 
1162
    html
 
1163
    >>> print(tag.namespace)
 
1164
    http://www.w3.org/1999/xhtml
 
1165
 
 
1166
    >>> root = etree.Element('{http://www.w3.org/1999/xhtml}html')
 
1167
    >>> tag = etree.QName(root)
 
1168
    >>> print(tag.localname)
 
1169
    html
 
1170
 
 
1171
    >>> tag = etree.QName(root, 'script')
 
1172
    >>> print(tag.text)
 
1173
    {http://www.w3.org/1999/xhtml}script
 
1174
    >>> tag = etree.QName('{http://www.w3.org/1999/xhtml}html', 'script')
 
1175
    >>> print(tag.text)
 
1176
    {http://www.w3.org/1999/xhtml}script
 
1177
 
1061
1178
lxml.etree allows you to look up the current namespaces defined for a
1062
1179
node through the ``.nsmap`` property:
1063
1180
 
1086
1203
Therefore, modifying the returned dict cannot have any meaningful
1087
1204
impact on the Element.  Any changes to it are ignored.
1088
1205
 
1089
 
Namespaces on attributes work alike, but since version 2.3, lxml.etree
 
1206
Namespaces on attributes work alike, but as of version 2.3, lxml.etree
1090
1207
will make sure that the attribute uses a prefixed namespace
1091
1208
declaration.  This is because unprefixed attribute names are not
1092
1209
considered being in a namespace by the XML namespace specification