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