如何在XSLT中使用python函数和变量?

时间:2015-08-06 16:54:52

标签: python xml xslt lxml

在PHP中,您可以使用registerPHPFunctions在XSLT文件中使用PHP函数,如下所示:

 <?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);
?> 

什么是Python等价物?这就是我尝试过的事情

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)

这是我的XSLT的简化版本。

2 个答案:

答案 0 :(得分:4)

以下是扩展函数(不是元素)如何给出我认为你想要的结果:

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

输出:

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

请参阅http://lxml.de/extensions.html#xpath-extension-functions

答案 1 :(得分:0)

变量和函数有单独的答案。我只是熟悉变量的一半。

对于变量,您可以将它们作为关键字参数传递给调用,将它们作为xsl:param传递。例如:

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

请注意,参数是XPath表达式,因此需要引用字符串。有一个函数不透明地执行此操作:

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

如果要传递XML片段,可以使用exslt:node-set()函数。

对于函数,可以将它们公开为xpath函数或元素。有很多种,我自己也没有这样做,所以请阅读下面的文档和/或编辑这个答案。

Docs for basic use and variables.

Docs for adding functions.