Re: [lxml] bidirectional python <-> xml transformations?

I may have put the issue in a confusing way, because I used the term "string", but I am actually trying to capture a an entire results tree (or the first item if there is a list of results), and if possible, recursively apply XSLT templates to those results. Consequently, I cannot use the XPath text() function because this flattens the results, rather than keeping them as a tree. I should also note that in the line reading "results = self.apply_templates(context, string)", I tried changing "string" to "string[0]", but the effect was the same. I hope this makes clearer what I am trying to do. On 05/06/2013 03:37 PM, Simon Sapin wrote:
Le 06/05/2013 18:41, Eric Levy a écrit :
Currently, the apply_templates call is failing, with the following error:
TypeError: invalid argument type <type 'list'>
Hi,
That seems pretty self-explanatory. XPath returns a list of results, and according to the doc[1] example apply_templates() expects a single element object.
[1] http://lxml.de/extensions.html
If your XPath expression is constructed such that there is exactly one result, use `foo[0]` to extract it from the list. If you use `text()` in XPath or something such that results are strings rather than element objects, you might need to construct a dummy object to pass to apply_templates():
el = lxml.etree.Element('dummy') el.text = xpath_results[0]

Le 06/05/2013 21:51, Eric Levy a écrit :
I may have put the issue in a confusing way, because I used the term "string", but I am actually trying to capture a an entire results tree (or the first item if there is a list of results), and if possible, recursively apply XSLT templates to those results. Consequently, I cannot use the XPath text() function because this flattens the results, rather than keeping them as a tree. I should also note that in the line reading "results = self.apply_templates(context, string)", I tried changing "string" to "string[0]", but the effect was the same.
I hope this makes clearer what I am trying to do.
Ok, so forget the part about strings and text. Can you copy the full traceback you get with string[0]? Cheers, -- Simon Sapin
participants (2)
-
Eric Levy
-
Simon Sapin