Trying to find a elements Xpath and store it as a attribute
provowallis
gshepherd181 at gmail.com
Sun Oct 1 21:32:33 EDT 2006
Hi all,
I've been struggling with this for a while so I'm hoping that someone
could point me in the right direction. Here's my problem: I'm trying to
get the XPath for a given node in my document and then store that XPath
as an attribute of the element itself. If anyone has a recommendation
I'd be happy to hear it.
Thanks,
Provo
For instance, I would take this XML
###before
<?xml version="1.0"?>
<hello-world>
<greeter>An XSLT Programmer</greeter>
<greeting>Hello, World!</greeting>
</hello-world>
###after
<?xml version="1.0"?>
<hello-world>
<greeter xp="hello-word[1]/greeter[1]">An XSLT Programmer</greeter>
<greeting xp="hello-world[1]/greeting[1]">Hello, World!</greeting>
</hello-world>
###
import sets
import amara
from amara import binderytools
doc = amara.parse('hello.xml')
elems = {}
for e in doc.xml_xpath('//*'):
paths = elems.setdefault((e.namespaceURI, e.localName),
sets.Set())
path = u'/'.join([n.nodeName for n in
e.xml_xpath(u'ancestor::*')])
paths.add(u'/' + path)
for name in elems:
doc.name.km = elems[name]
More information about the Python-list
mailing list