The proper way to find xml node using libxml2 and xpath in python

BioHazRd

New Member
I'm new to xml parsing in python. I've been playing around with libxml2 and xpath but unfortunately the documentation for that isn't very good. Suppose I have an xml doc which looks something like following:\[code\]<?xml version="1.0" encoding="UTF-8"?><root root_attr="foo"> <a> <b> <name>test1</name> <value>foo1</value> </b> <b> <name>test2</name> <value>foo2</value> </b> </a></root>\[/code\]Now the "documentation" for libxml2 python bindings suggest that in order to use xpath I should first create a context for xpath as such:\[code\]doc = libxml2.parseFile("tst.xml")ctxt = doc.xpathNewContext()node_b = ctxt.xpathEval("/root/a/b")\[/code\]However, when searching online, I find that a lot of people simply retrieve the node without creating a context like this:\[code\]node_b=doc.xpathEval("/root/a/b")\[/code\]I have the following questions regarding this:
a) What is the proper way to retrieve the node using xpath in python?
b) What is a xpath context and more specifically, why do I need to free the context (as shown in the document, ctxt.xpathFreeContext())?
c) Are there any better documentation/examples/tutorial than the ones listed on the documentation page?
I realize that I'm asking a lot of broad questions but I can't find any good resources online. Thanks in advance!
 
Back
Top