[lxml-dev] Keep track of file URL in Elements
Frederik Elwert
felwert at uni-bremen.de
Sun Aug 26 17:13:27 CEST 2007
Hello!
XForms turns out to be quite challenging... Sorry to bother you again.
I need to access a document given by a link in an attribute. When using
XSLT, this would be a typical usecase for document(). But when not using
XSLT, I need to mimic it's behaviour, since document() doesn't work in
ordinary XPath expressions.
So I have something like this in an XML document:
<instance resource="default-scales.xml"/>
Since the link is relative to the document's location, and not to the
current working directory, I need to get some absolute path for this.
But the information for the document's location is only stored in the
ElementTree initially generated by etree.parse(). The descendants know
neither about the document's location, nor about their "parent"
ElementTree, as far as I could see.
So would it be anyhow possible to get the document's URL information in
a custom xpath function? The only thing I could think of is searching
for ElementTrees using dir(), seeing if the current element is in that
tree using getpath(), and then using the trees docinfo. But that's
really ugly.
Maybe another possibility would be to use a custom Element class and add
an attribute containing this information to each element during parsing
the XML file. But (a) I'm not sure if this is possible at all, and (b)
all elements would have a new, arbitrary attribute that I would have to
get rid of before writing the tree back.
So maybe anyone of you has an idea how this could be done using lxml?
Kind regards,
Frederik
More information about the lxml-dev
mailing list