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 # 6 import setup_test 7 import libxml2 8 import os 9 import sys 10 11 # Memory debug specific 12 libxml2.debugMemory(1) 13 14 expect="""<bibl id="Aho" key="Aho/Ullman">Aho, Alfred V., 15 Ravi Sethi, and Jeffrey D. Ullman. 16 <emph>Compilers: Principles, Techniques, and Tools</emph>. 17 Reading: Addison-Wesley, 1986, rpt. corr. 1988.</bibl>""" 18 19 basedir = os.path.dirname(os.path.realpath(__file__)) 20 f = open(os.path.join(basedir, '../../test/valid/REC-xml-19980210.xml'), 'rb') 21 input = libxml2.inputBuffer(f) 22 reader = input.newTextReader("REC") 23 res="" 24 while 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 32 if 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 # 41 del input 42 del reader 43 44 # Memory debug specific 45 libxml2.cleanupParser() 46 if libxml2.debugMemory(1) == 0: 47 print("OK") 48 else: 49 print("Memory leak %d bytes" % (libxml2.debugMemory(1))) 50