import libxml2 def Eval(ctx,xp): for p in xp: if p: print "Searching for ",p nodes = ctx.xpointerEval(p) print "Found:\n",nodes,"\n" if p.find("range") >=0: if p.find("-point")>=0: for tup in nodes: print "'" + tup[0].get_content().replace(' ','X') + "'" + "\n" else: for result in nodes: for tup in result: print "'" + tup[0].get_content().replace(' ','X') + "'" + "\n" def loadXML(XMLFile,QueryFile): doc=libxml2.parseFile(XMLFile) root=doc.getRootElement() ctx=root.xpointerNewContext(doc,None) doc.validateDocument(ctx) xp = [] file = open(QueryFile,"r") line = file.readline() while line: #print line xp.append(line[:-1]) line = file.readline() file.close() return ctx,xp #Testing: print "Running tests to check the string-range case for xpointerEval().\nSpaces in the returned nodes are replaced by X's to see the exact number of characters.\n" print "Loading xpointer queries from file 'tstXpointers' on xml 'm10035.cnxml'\n" ctx,xp = loadXML('m10035.cnxml',"tstXpointers") Eval(ctx,xp) print "Loading xpointer queries from file 'strpoint' on xml 'str'\n" ctx,xp = loadXML('../../test/XPath/docs/str','../../test/XPath/xptr/strpoint') Eval(ctx,xp) print "Loading xpointer queries from file 'strrange' on xml 'str'\n" ctx,xp = loadXML('../../test/XPath/docs/str','../../test/XPath/xptr/strrange') Eval(ctx,xp) print "Loading xpointer queries from file 'strrange2' on xml 'str'\n" ctx,xp = loadXML('../../test/XPath/docs/str','../../test/XPath/xptr/strrange2') Eval(ctx,xp) print "Loading xpointer queries from file 'strrange3' on xml 'str'\n" ctx,xp = loadXML('../../test/XPath/docs/str','../../test/XPath/xptr/strrange3') Eval(ctx,xp) #Below tests don't work, since the xpointers use id(), and the document has no DTD to validate against #print "Loading xpointer queries from file 'vidbase' on xml 'chapters'\n" #ctx,xp = loadXML('../test/XPath/docs/chapters','../test/XPath/xptr/vidbase') #ctx,xp = loadXML('../test/XPath/docs/chapters','vidbase2') #node = ctx.xpointerEval('xpointer(/1)') #print node[0] #Eval(ctx,xp) #print "Loading xpointer queries from file 'chaptersrange' on xml 'chapters'\n" #ctx,xp = loadXML('../test/XPath/docs/chapters','../test/XPath/xptr/chaptersrange') #Eval(ctx,xp)