[XML-SIG] Using xpath with elements other than the documentElement

Chris Lambrou christopherlambrou at gmail.com
Sun Feb 25 18:35:53 CET 2007


I'm having trouble working out how to evaluate xpath expressions using
xml.xpath.Evaluate with elements other than a DOM's documentElement. I
have a file that contains the following:

<?xml version="1.0"?>
	<module name="Util" file="util/util.module"/>
	<configuration name="Debug" outputDir="dist/debug">
		<module include="*"/>
	<configuration name="Release" outputDir="dist/release">
		<module include="*"/>

I've loaded the XML from the file, and can iterate through the
'configuration' elements using the following:

dom = xml.dom.minidom.parse(projectPath, dom.documentElement)
for configElement in xml.xpath.Evaluate("/javascriptProject/configuration"):
    #do something with the configuration element

Within the loop, I'd like to be able to evaluate xpath expressions
with respect to the configElement (e.g. I'd like to evaluate
'./module' to deal with any module sub-elements for the current
configElement). I've tried various combinations of arguments passed to
the xml.xpath.Evaluate method, but all of them result in errors. I've
tried looking for examples online, but can only find examples that
pass a document's root element (dom.documentElement) into the Evaluate
method. In particular, the coverage of xpath in the HOWTO of the PyXML
project is disappointingly minimal
(http://pyxml.sourceforge.net/topics/howto/section-XPath.html), and
I'm afraid I'm not yet sufficiently familiar with Python to be able to
easily work out where I'm going wrong from the xml.xpath source.

The other closest match I found was this posting -
http://mail.python.org/pipermail/xml-sig/2006-January/011418.html -
but unfortunately there was no followup, and the suggested solution
there doesn't work for me.

Hopefully, I'm missing something simple here. Surely it must be
possible to evaluate xpath expressions against arbitrary elements
within an xml dom tree - this has been a breeze to achieve in other
languages I've worked with.

Could someone please help me with this?  I'd be happy to provide
self-contained working examples of the problem, if that would help.


More information about the XML-SIG mailing list