[XML-SIG] Using XML Namespaces in XPath Expressions from Python

Uche Ogbuji Uche.Ogbuji at fourthought.com
Thu Jun 9 14:26:53 CEST 2005


On Wed, 2005-06-08 at 10:27 -0400, JonTom Kittredge wrote:
> I'm having trouble using PyXML to evaluate XPath expressions that
> include namespace references.
> 
> Here is a sample Python snipped:
> 
>     #!/usr/bin/env pystart
> 
>     import sys
>     import xml.dom.minidom
> 
>     from xml.xpath.Context import Context
>     from xml.xpath import Evaluate
>     from xml.xpath.NamespaceNode import NamespaceNode
> 
> 
>     if __name__ == "__main__":
>       sys.stderr.write("XPathTest: 18\n")
> 
>       inputDom = xml.dom.minidom.parse(open("xpathtest-input.xml"))
> 
>       sys.stderr.write("Node: %s\n" % (inputDom.documentElement))
>       outContext = Context(inputDom.documentElement)

Where are you passing in the NS mapping here?  Did I miss a PyXML XPath
update that defaults mappings to in-scope namespaces on the node passed
in?

>       sys.stderr.write("NSes=%s\n" % (outContext.nss()))
> 
>       result = Evaluate("count(/dbq:Contract)",
>                         contextNode=inputDom.documentElement,

I think this line is your problem.  It overrides the next (probably not
sensible, which is why we reversed that priority in 4Suite ages ago).

>                         context=outContext)


-- 
Uche Ogbuji                               Fourthought, Inc.
http://uche.ogbuji.net                    http://fourthought.com
http://copia.ogbuji.net                   http://4Suite.org
Use CSS to display XML, part 2 - http://www-128.ibm.com/developerworks/edu/x-dw-x-xmlcss2-i.html
XML Output with 4Suite & Amara - http://www.xml.com/pub/a/2005/04/20/py-xml.html
Use XSLT to prepare XML for import into OpenOffice Calc - http://www.ibm.com/developerworks/xml/library/x-oocalc/
Schema standardization for top-down semantic transparency - http://www-128.ibm.com/developerworks/xml/library/x-think31.html



More information about the XML-SIG mailing list