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