Post

Libxml2 and Python on Windows

For the purpose of using XPath and XQuery out of my python scripts for test statistics generation, I decided to try libxml2. I mostly decided against ElementTree because their website told me the XPath subset does not support queries like:

count(//event[evalresult/text()="FALSE"][warnlevel/text()="1"])

Also, I hope to be faster with the C-based implementation of libxml2. For installation, I only had to download this pre-bundled lxml windows binary from the python package repository; it comes with libxml2 included.

The following code is enough to get the above count:

1
from lxml import etree doc = etree.parse(filePath) result = doc.xpath('//event[evalresult/text()="FALSE"][warnlevel/text()="1"]') count = len(result) count2 = doc.xpath(count('//event[evalresult/text()="FALSE"][warnlevel/text()="1"]')) # alternative

About XQuery: lxml serves as frontend for libxml2 and libxslt, neither of which support XQuery.

Further information: lxml homepage SketchPath, a good XPath expression evaluation software

This post is licensed under CC BY-ND 4.0 by the author.