PyPop.xslt#

Python XSLT extensions for handling things outside the scope of XSLT 1.0.

Attributes#

ns

Function namespace for custom PyPop XSLT extension functions.

Functions#

num_zeros(decimal)

Count zeroes.

exponent_len(num)

Calculate space taken for exponent.

format_number_fixed_width(_context, *args)

Format number to fixed width.

Package Contents#

ns#

Function namespace for custom PyPop XSLT extension functions.

This namespace allows registering Python functions that can be called directly from XSLT stylesheets.

prefix#

The namespace prefix used in XSLT stylesheets. Here it is set to "es", so extension functions are invoked as es:format_number_fixed_width(...). See example in format_number_fixed_width()

Type:

str

num_zeros(decimal)#

Count zeroes.

Parameters:

decimal (float) – number to check

Returns:

number of zeroes in floating point number, or inf if number is zero

Return type:

int

exponent_len(num)#

Calculate space taken for exponent.

Example

>>> exponent_len(1e-03)
2
>>> exponent_len(1e-10)
3
Parameters:

num (float) – input number

Returns:

length of exponent

Return type:

int

format_number_fixed_width(_context, *args)#

Format number to fixed width.

Example

>>> ns["format_number_fixed_width"] = format_number_fixed_width
>>> root = etree.XML("<a><b>0.0000043</b></a>")
>>> doc = etree.ElementTree(root)
>>> xslt = etree.XSLT(etree.XML('''
... <stylesheet version="1.0" xmlns="http://www.w3.org/1999/XSL/Transform" xmlns:es="http://pypop.org/lxml/functions">
...  <output method="text" encoding="ASCII"/>
...  <template match="/">
...   <text>Yep [</text>
...   <value-of select="es:format_number_fixed_width(string(/a/b), 5)"/>
...   <text>]</text>
...  </template>
... </stylesheet>
... '''))
>>>
>>> print(xslt(doc))
Yep [4.3e-6]

Note

arguments from XSLT file: num and places are encoded in *args.

Parameters:

_context (obj) – not used

Returns:

formatted number to fixed width

Return type:

str