bidirectional python <-> xml transformations?
Hello,
I am experience an issue writing data python data that had been
generated by an XPath query. It appears that lxml uses some pythonic
magic to transform between Python objects and XML data. It also
appears, unfortunately that the same data that was generated by the
library cannot be written back to XML.
Specifically, I am seeking to develop a simple web page that is based on
lxml applying XSLT templates. Human readable text, as well as
high-level formatting information, is kept in XML documents. XSLT
templates transform the documents to HTML pages. Because
internationalization is necessary, text is kept separate documents. The
particular document used depends on the locale, which is determined at
run time by the Python application.
To satisfy these requirements, I have attempted to use the XSLT
extensions feature of lxml. Before processing begins, I parse the XML
document containing human-readable text. I then perform the
transformation on the main XML document, passing an extension callback
that is meant to read one of the attribute values of the extension XSLT
element, and use the value to generate an XPath query. The goal for the
extension element to evaluate to the result tree of the XPath query on
the auxiliary document. Additionally, markup in the results tree from
the query should be processed according to the template rules.
To illustrate, consider this snippet:
in_tree = etree.parse("document.xml")
tpl_tree = etree.parse("template.xslt")
strings_tree = etree.parse("aux_doc.xml")
class StringsElement(XSLTExtension):
def execute(self, context, self_node,
input_node, output_parent):
id_ = self_node.attrib["id"]
string = strings_tree.xpath(generate_xpath_query(id_))
results = self.apply_templates(context, string)
output_parent.append(results)
xslt_exts = { ( "mynamespace", "string" ) : StringElement() }
template = etree.XSLT(tpl_tree, extensions = xslt_exts)
rslt_tree = trans(in_tree)
rslt_tree.write(sys.stdout)
Currently, the apply_templates call is failing, with the following error:
TypeError: invalid argument type
participants (2)
-
Eric Levy
-
Simon Sapin