python - How to transform XML to text -


following on earlier question (how transform xml?), have nicely structured xml doc, this..

<?xml version="1.0" encoding="utf-8"?> <root> <employee id="1" reportsto="1" title="ceo">     <employee id="2" reportsto="1" title="director of operations">         <employee id="3" reportsto="2" title="human resources manager" />     </employee> </employee> </root> 

now need convert javascript this..

var treedata = [ { "name": "ceo", "parent": "null", "children": [   {     "name": "director of operations",     "parent": "top level",     "children": [       {         "name": "human resources manager",         "parent": "level 2: a"       }    ]   } ] } ]; 

i've started writing xslt, looks this..

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format"> <xsl:output method="text" omit-xml-declaration="yes" indent="yes"/>  <xsl:template match="root">     <xsl:apply-templates select="employee" /> </xsl:template>  <xsl:template match="employee">     <xsl:param name="eid" select="@id" />     <xsl:param name="ereports" select="@reportsto" />     <xsl:param name="etitle" select="@title" />     <xsl:value-of select="concat( $etitle, ' , ', $eid )" />           <xsl:apply-templates select="employee" /> </xsl:template>  </xsl:stylesheet> 

but when apply transform (via pythons lxml library), message "none". (in case helps, here's lxml command i'm using...)

dom = et.parse("input.xml") xslt = et.parse("transform.xslt") transform = et.xslt(xslt) newdom = transform(dom) print(et.tostring(newdom, pretty_print=true)) 

i know xslt near complete, why aren't getting any output? shouldn't @ least getting job title printed?

edit: updated op's included python code.

your problem lxml.etree.tostring , .write method meaningful on xml, not on xslt result output method="text" might not have single root element xml does. confusing reason, functions do have method= keyword argument not useful.

this should do:

import lxml.etree etree data = etree.parse('data.xml') transform = etree.xslt(etree.parse('txt.xslt')) res = transform(data) bytes(res) 
b'\nceo , 1director of operations , 2human resources manager , 3\n' 

if you're interested in real world example, i made patch.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -