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 <type 'list'> I have been struggling with this issue and appreciate your help. Sincerely, Eric Levy
Hi, I'm still wondering about this issue. Essentially, what I am trying to understand is whether an XPath result can be written as part of new XML document. Incidentally, it appears that the code snippet did not come out formatted properly. The body of the execute() function should be indented one more level. Thanks. Sincerely, Eric Levy On 05/02/2013 04:28 PM, Eric Levy wrote:
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 <type 'list'>
I have been struggling with this issue and appreciate your help.
Sincerely, Eric Levy
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] -- Simon Sapin
participants (2)
-
Eric Levy
-
Simon Sapin