xref: /aosp_15_r20/external/libxml2/python/tests/reader5.py (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1#!/usr/bin/env python3
2#
3# this tests the Expand() API of the xmlTextReader interface
4# this extract the Dragon bibliography entries from the XML specification
5#
6import setup_test
7import libxml2
8import os
9import sys
10
11# Memory debug specific
12libxml2.debugMemory(1)
13
14expect="""<bibl id="Aho" key="Aho/Ullman">Aho, Alfred V.,
15Ravi Sethi, and Jeffrey D. Ullman.
16<emph>Compilers:  Principles, Techniques, and Tools</emph>.
17Reading:  Addison-Wesley, 1986, rpt. corr. 1988.</bibl>"""
18
19basedir = os.path.dirname(os.path.realpath(__file__))
20f = open(os.path.join(basedir, '../../test/valid/REC-xml-19980210.xml'), 'rb')
21input = libxml2.inputBuffer(f)
22reader = input.newTextReader("REC")
23res=""
24while reader.Read() > 0:
25    while reader.Name() == 'bibl':
26        node = reader.Expand()            # expand the subtree
27        if node.xpathEval("@id = 'Aho'"): # use XPath on it
28            res = res + node.serialize()
29        if reader.Next() != 1:            # skip the subtree
30            break;
31
32if res != expect:
33    print("Error: didn't get the expected output")
34    print("got '%s'" % (res))
35    print("expected '%s'" % (expect))
36
37
38#
39# cleanup
40#
41del input
42del reader
43
44# Memory debug specific
45libxml2.cleanupParser()
46if libxml2.debugMemory(1) == 0:
47    print("OK")
48else:
49    print("Memory leak %d bytes" % (libxml2.debugMemory(1)))
50