Federico
Federico

Reputation: 3920

How to use python functions and variables inside XSLT?

In PHP, you can use registerPHPFunctions to use a PHP function inside an XSLT file like this:

 <?php
$xml = <<<EOB
<allusers>
 <user>
  <uid>bob</uid>
  <id>1</id>
 </user>
 <user>
  <uid>joe</uid>
  <id>2</id>
 </user>
</allusers>
EOB;

$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:php="http://php.net/xsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table>
    <xsl:for-each select="user">
      <tr><td>
        <xsl:value-of
             select="php:function('ucfirst',concat(string(uid), string(id)))"/>
      </td></tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
EOB;
$xmldoc = DOMDocument::loadXML($xml);
$xsldoc = DOMDocument::loadXML($xsl);

$proc = new XSLTProcessor();
$proc->registerPHPFunctions('ucfirst');
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?> 

What is the Python equivalent? This is what I've tried

from lxml import etree 

xml = etree.XML('''
<allusers>
 <user>
  <uid>bob</uid>
  <id>1</id>
 </user>
 <user>
  <uid>joe</uid>
  <id>2</id>
 </user>
</allusers>''')

xsl = etree.XML('''
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:f="mynamespace"
     extension-element-prefixes="f">

<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table>
    <xsl:for-each select="user">
      <tr><td>
        <f:ucfirst>
        <xsl:value-of select="concat(string(uid), string(id))"/>
        </f:ucfirst>
      </td></tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
''')
extension = Ucase()
extensions = { ('mynamespace', 'ucfirst') : extension }
proc = etree.XSLT(xsl, extensions=extensions)
str(proc(xml))

class Ucase(etree.XSLTExtension):
  def execute(self, context, self_node, input_node, output_parent):
    title = self_node[0].text.capitalize()
    output_parent.text(title)

This is a simplified version of my XSLT.

Upvotes: 3

Views: 2868

Answers (2)

mzjn
mzjn

Reputation: 51002

Here is how an extension function (not an element) can give the result that I think you want:

from lxml import etree

def ucfirst(context, s):
    return s.capitalize()

ns = etree.FunctionNamespace("mynamespace")
ns['ucfirst'] = ucfirst

xml = etree.XML('''
<allusers>
 <user>
  <uid>bob</uid>
  <id>1</id>
 </user>
 <user>
  <uid>joe</uid>
  <id>2</id>
 </user>
</allusers>''')

xsl = etree.XML('''\
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:f="mynamespace" exclude-result-prefixes="f">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
 <xsl:template match="allusers">
  <html><body>
    <h2>Users</h2>
    <table>
    <xsl:for-each select="user">
      <tr><td>
        <xsl:value-of select="f:ucfirst(concat(string(uid), string(id)))"/>
      </td></tr>
    </xsl:for-each>
    </table>
  </body></html>
 </xsl:template>
</xsl:stylesheet>
''')

transform = etree.XSLT(xsl)
result = transform(xml)
print result

Output:

<html><body>
<h2>Users</h2>
<table>
<tr><td>Bob1</td></tr>
<tr><td>Joe2</td></tr>
</table>
</body></html>

See http://lxml.de/extensions.html#xpath-extension-functions.

Upvotes: 4

o11c
o11c

Reputation: 16116

There are separate answers for variables and functions. I'm only really familiar with the variable half.


For variables, you can pass them as an xsl:param by passing them as keyword arguments to the call. For example:

transform = etree.XSLT(xslt_tree)
result = transform(doc_root, a="5")

Note that the argument is an XPath expression, so strings need to be quoted. There is a function that does this opaquely:

result = transform(doc_root, a=etree.XSLT.strparam(""" It's "Monty Python" """))

If you want to pass an XML fragment you could use the exslt:node-set() function.


For functions, you can expose them either as an xpath function or as an element. There is a bunch of variety and I haven't done this myself so read the docs below and/or edit this answer.


Docs for basic use and variables.

Docs for adding functions.

Upvotes: 0

Related Questions