Skip to content

Instantly share code, notes, and snippets.

@Eugeny
Created September 13, 2010 12:51
Show Gist options
  • Save Eugeny/577238 to your computer and use it in GitHub Desktop.
Save Eugeny/577238 to your computer and use it in GitHub Desktop.
Python:
def attr(_, v, d):
return d if v == '' else v
def render(templ):
global xslt
ex = {('local-ns', 'attr') : attr}
ns = {'ext' : 'local-ns'}
return etree.tostring(xslt(templ, namespaces=ns, extensions=ex))
XSLT:
<xsl:template match="somenode">
<a href="{ext:attr(string('111'),string('222'))}"/>
</xsl:template>
XML:
<somenode/>
Output:
Exception TypeError: TypeError(u'Argument must be string or unicode.',) in 'lxml.etree.XSLT._run_transform' ignored
Traceback (most recent call last):
File "/home/eugeny/Work/ajenti/ajenti/app/application.py", line 88, in dispatcher
self.start_response)
...
bla-bla-bla
...
File "/home/eugeny/Work/ajenti/ajenti/ui/template.py", line 41, in render
return xslt.render(self._dom.getroot())
File "/home/eugeny/Work/ajenti/ajenti/ui/xslt.py", line 28, in render
return etree.tostring(xslt(templ, namespaces=ns, extensions=ex))
File "xslt.pxi", line 556, in lxml.etree.XSLT.__call__ (src/lxml/lxml.etree.c:110044)
XSLTApplyError: Error applying stylesheet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment