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