1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
|
====================================
Python extensions for XPath and XSLT
====================================
This document describes how to use Python extension functions in XPath
and XSLT like this:
.. sourcecode:: xml
<xsl:value-of select="f:myPythonFunction(.//sometag)" />
and extension elements in XSLT as in the following example:
.. sourcecode:: xml
<xsl:template match="*">
<my:python-extension>
<some-content />
</my:python-extension>
</xsl:template>
.. contents::
..
1 XPath Extension functions
1.1 The FunctionNamespace
1.2 Global prefix assignment
1.3 The XPath context
1.4 Evaluators and XSLT
1.5 Evaluator-local extensions
1.6 What to return from a function
2 XSLT extension elements
2.1 Declaring extension elements
2.2 Applying XSL templates
2.3 Working with read-only elements
..
>>> try: from StringIO import StringIO
... except ImportError:
... from io import BytesIO
... def StringIO(s):
... if isinstance(s, str): s = s.encode("UTF-8")
... return BytesIO(s)
XPath Extension functions
=========================
Here is how an extension function looks like. As the first argument,
it always receives a context object (see below). The other arguments
are provided by the respective call in the XPath expression, one in
the following examples. Any number of arguments is allowed:
.. sourcecode:: pycon
>>> def hello(dummy, a):
... return "Hello %s" % a
>>> def ola(dummy, a):
... return "Ola %s" % a
>>> def loadsofargs(dummy, *args):
... return "Got %d arguments." % len(args)
The FunctionNamespace
---------------------
In order to use a function in XPath or XSLT, it needs to have a
(namespaced) name by which it can be called during evaluation. This
is done using the FunctionNamespace class. For simplicity, we choose
the empty namespace (None):
.. sourcecode:: pycon
>>> from lxml import etree
>>> ns = etree.FunctionNamespace(None)
>>> ns['hello'] = hello
>>> ns['countargs'] = loadsofargs
This registers the function `hello` with the name `hello` in the default
namespace (None), and the function `loadsofargs` with the name `countargs`.
Now we're going to create a document that we can run XPath expressions
against:
.. sourcecode:: pycon
>>> root = etree.XML('<a><b>Haegar</b></a>')
>>> doc = etree.ElementTree(root)
Done. Now we can have XPath expressions call our new function:
.. sourcecode:: pycon
>>> print(root.xpath("hello('Dr. Falken')"))
Hello Dr. Falken
>>> print(root.xpath('hello(local-name(*))'))
Hello b
>>> print(root.xpath('hello(string(b))'))
Hello Haegar
>>> print(root.xpath('countargs(., b, ./*)'))
Got 3 arguments.
Note how we call both a Python function (`hello`) and an XPath built-in
function (`string`) in exactly the same way. Normally, however, you would
want to separate the two in different namespaces. The FunctionNamespace class
allows you to do this:
.. sourcecode:: pycon
>>> ns = etree.FunctionNamespace('http://mydomain.org/myfunctions')
>>> ns['hello'] = hello
>>> prefixmap = {'f' : 'http://mydomain.org/myfunctions'}
>>> print(root.xpath('f:hello(local-name(*))', namespaces=prefixmap))
Hello b
Global prefix assignment
------------------------
In the last example, you had to specify a prefix for the function namespace.
If you always use the same prefix for a function namespace, you can also
register it with the namespace:
.. sourcecode:: pycon
>>> ns = etree.FunctionNamespace('http://mydomain.org/myother/functions')
>>> ns.prefix = 'es'
>>> ns['hello'] = ola
>>> print(root.xpath('es:hello(local-name(*))'))
Ola b
This is a global assignment, so take care not to assign the same prefix to
more than one namespace. The resulting behaviour in that case is completely
undefined. It is always a good idea to consistently use the same meaningful
prefix for each namespace throughout your application.
The prefix assignment only works with functions and FunctionNamespace objects,
not with the general Namespace object that registers element classes. The
reasoning is that elements in lxml do not care about prefixes anyway, so it
would rather complicate things than be of any help.
The XPath context
-----------------
Functions get a context object as first parameter. In lxml 1.x, this value
was None, but since lxml 2.0 it provides two properties: ``eval_context`` and
``context_node``. The context node is the Element where the current function
is called:
.. sourcecode:: pycon
>>> def print_tag(context, nodes):
... print("%s: %s" % (context.context_node.tag, [ n.tag for n in nodes ]))
>>> ns = etree.FunctionNamespace('http://mydomain.org/printtag')
>>> ns.prefix = "pt"
>>> ns["print_tag"] = print_tag
>>> ignore = root.xpath("//*[pt:print_tag(.//*)]")
a: ['b']
b: []
The ``eval_context`` is a dictionary that is local to the evaluation. It
allows functions to keep state:
.. sourcecode:: pycon
>>> def print_context(context):
... context.eval_context[context.context_node.tag] = "done"
... entries = list(context.eval_context.items())
... entries.sort()
... print(entries)
>>> ns["print_context"] = print_context
>>> ignore = root.xpath("//*[pt:print_context()]")
[('a', 'done')]
[('a', 'done'), ('b', 'done')]
Evaluators and XSLT
-------------------
Extension functions work for all ways of evaluating XPath expressions and for
XSL transformations:
.. sourcecode:: pycon
>>> e = etree.XPathEvaluator(doc)
>>> print(e('es:hello(local-name(/a))'))
Ola a
>>> namespaces = {'f' : 'http://mydomain.org/myfunctions'}
>>> e = etree.XPathEvaluator(doc, namespaces=namespaces)
>>> print(e('f:hello(local-name(/a))'))
Hello a
>>> xslt = etree.XSLT(etree.XML('''
... <stylesheet version="1.0"
... xmlns="http://www.w3.org/1999/XSL/Transform"
... xmlns:es="http://mydomain.org/myother/functions">
... <output method="text" encoding="ASCII"/>
... <template match="/">
... <value-of select="es:hello(string(//b))"/>
... </template>
... </stylesheet>
... '''))
>>> print(xslt(doc))
Ola Haegar
It is also possible to register namespaces with a single evaluator after its
creation. While the following example involves no functions, the idea should
still be clear:
.. sourcecode:: pycon
>>> f = StringIO('<a xmlns="http://mydomain.org/myfunctions" />')
>>> ns_doc = etree.parse(f)
>>> e = etree.XPathEvaluator(ns_doc)
>>> e('/a')
[]
This returns nothing, as we did not ask for the right namespace. When we
register the namespace with the evaluator, however, we can access it via a
prefix:
.. sourcecode:: pycon
>>> e.register_namespace('foo', 'http://mydomain.org/myfunctions')
>>> e('/foo:a')[0].tag
'{http://mydomain.org/myfunctions}a'
Note that this prefix mapping is only known to this evaluator, as opposed to
the global mapping of the FunctionNamespace objects:
.. sourcecode:: pycon
>>> e2 = etree.XPathEvaluator(ns_doc)
>>> e2('/foo:a')
Traceback (most recent call last):
...
lxml.etree.XPathEvalError: Undefined namespace prefix
Evaluator-local extensions
--------------------------
Apart from the global registration of extension functions, there is also a way
of making extensions known to a single Evaluator or XSLT. All evaluators and
the XSLT object accept a keyword argument ``extensions`` in their constructor.
The value is a dictionary mapping (namespace, name) tuples to functions:
.. sourcecode:: pycon
>>> extensions = {('local-ns', 'local-hello') : hello}
>>> namespaces = {'l' : 'local-ns'}
>>> e = etree.XPathEvaluator(doc, namespaces=namespaces, extensions=extensions)
>>> print(e('l:local-hello(string(b))'))
Hello Haegar
For larger numbers of extension functions, you can define classes or modules
and use the ``Extension`` helper:
.. sourcecode:: pycon
>>> class MyExt:
... def function1(self, _, arg):
... return '1'+arg
... def function2(self, _, arg):
... return '2'+arg
... def function3(self, _, arg):
... return '3'+arg
>>> ext_module = MyExt()
>>> functions = ('function1', 'function2')
>>> extensions = etree.Extension( ext_module, functions, ns='local-ns' )
>>> e = etree.XPathEvaluator(doc, namespaces=namespaces, extensions=extensions)
>>> print(e('l:function1(string(b))'))
1Haegar
The optional second argument to ``Extension`` can either be be a
sequence of names to select from the module, a dictionary that
explicitly maps function names to their XPath alter-ego or ``None``
(explicitly passed) to take all available functions under their
original name (if their name does not start with '_').
The additional ``ns`` keyword argument takes a namespace URI or
``None`` (also if left out) for the default namespace. The following
examples will therefore all do the same thing:
.. sourcecode:: pycon
>>> functions = ('function1', 'function2', 'function3')
>>> extensions = etree.Extension( ext_module, functions )
>>> e = etree.XPathEvaluator(doc, extensions=extensions)
>>> print(e('function1(function2(function3(string(b))))'))
123Haegar
>>> extensions = etree.Extension( ext_module, functions, ns=None )
>>> e = etree.XPathEvaluator(doc, extensions=extensions)
>>> print(e('function1(function2(function3(string(b))))'))
123Haegar
>>> extensions = etree.Extension(ext_module)
>>> e = etree.XPathEvaluator(doc, extensions=extensions)
>>> print(e('function1(function2(function3(string(b))))'))
123Haegar
>>> functions = {
... 'function1' : 'function1',
... 'function2' : 'function2',
... 'function3' : 'function3'
... }
>>> extensions = etree.Extension(ext_module, functions)
>>> e = etree.XPathEvaluator(doc, extensions=extensions)
>>> print(e('function1(function2(function3(string(b))))'))
123Haegar
For convenience, you can also pass a sequence of extensions:
.. sourcecode:: pycon
>>> extensions1 = etree.Extension(ext_module)
>>> extensions2 = etree.Extension(ext_module, ns='local-ns')
>>> e = etree.XPathEvaluator(doc, extensions=[extensions1, extensions2],
... namespaces=namespaces)
>>> print(e('function1(l:function2(function3(string(b))))'))
123Haegar
What to return from a function
------------------------------
.. _`XPath return values`: xpathxslt.html#xpath-return-values
Extension functions can return any data type for which there is an XPath
equivalent (see the documentation on `XPath return values`). This includes
numbers, boolean values, elements and lists of elements. Note that integers
will also be returned as floats:
.. sourcecode:: pycon
>>> def returnsFloat(_):
... return 1.7
>>> def returnsInteger(_):
... return 1
>>> def returnsBool(_):
... return True
>>> def returnFirstNode(_, nodes):
... return nodes[0]
>>> ns = etree.FunctionNamespace(None)
>>> ns['float'] = returnsFloat
>>> ns['int'] = returnsInteger
>>> ns['bool'] = returnsBool
>>> ns['first'] = returnFirstNode
>>> e = etree.XPathEvaluator(doc)
>>> e("float()")
1.7
>>> e("int()")
1.0
>>> int( e("int()") )
1
>>> e("bool()")
True
>>> e("count(first(//b))")
1.0
As the last example shows, you can pass the results of functions back into
the XPath expression. Elements and sequences of elements are treated as
XPath node-sets:
.. sourcecode:: pycon
>>> def returnsNodeSet(_):
... results1 = etree.Element('results1')
... etree.SubElement(results1, 'result').text = "Alpha"
... etree.SubElement(results1, 'result').text = "Beta"
...
... results2 = etree.Element('results2')
... etree.SubElement(results2, 'result').text = "Gamma"
... etree.SubElement(results2, 'result').text = "Delta"
...
... results3 = etree.SubElement(results2, 'subresult')
... return [results1, results2, results3]
>>> ns['new-node-set'] = returnsNodeSet
>>> e = etree.XPathEvaluator(doc)
>>> r = e("new-node-set()/result")
>>> print([ t.text for t in r ])
['Alpha', 'Beta', 'Gamma', 'Delta']
>>> r = e("new-node-set()")
>>> print([ t.tag for t in r ])
['results1', 'results2', 'subresult']
>>> print([ len(t) for t in r ])
[2, 3, 0]
>>> r[0][0].text
'Alpha'
>>> etree.tostring(r[0])
b'<results1><result>Alpha</result><result>Beta</result></results1>'
>>> etree.tostring(r[1])
b'<results2><result>Gamma</result><result>Delta</result><subresult/></results2>'
>>> etree.tostring(r[2])
b'<subresult/>'
The current implementation deep-copies newly created elements in node-sets.
Only the elements and their children are passed on, no outlying parents or
tail texts will be available in the result. This also means that in the above
example, the `subresult` elements in `results2` and `results3` are no longer
identical within the node-set, they belong to independent trees:
.. sourcecode:: pycon
>>> print("%s - %s" % (r[1][-1].tag, r[2].tag))
subresult - subresult
>>> print(r[1][-1] == r[2])
False
>>> print(r[1][-1].getparent().tag)
results2
>>> print(r[2].getparent())
None
This is an implementation detail that you should be aware of, but you should
avoid relying on it in your code. Note that elements taken from the source
document (the most common case) do not suffer from this restriction. They
will always be passed unchanged.
XSLT extension elements
=======================
Just like the XPath extension functions described above, lxml supports
custom extension *elements* in XSLT. This means, you can write XSLT
code like this:
.. sourcecode:: xml
<xsl:template match="*">
<my:python-extension>
<some-content />
</my:python-extension>
</xsl:template>
And then you can implement the element in Python like this:
.. sourcecode:: pycon
>>> class MyExtElement(etree.XSLTExtension):
... def execute(self, context, self_node, input_node, output_parent):
... print("Hello from XSLT!")
... output_parent.text = "I did it!"
... # just copy own content input to output
... output_parent.extend( list(self_node) )
The arguments passed to the ``.execute()`` method are
context
The opaque evaluation context. You need this when calling back
into the XSLT processor.
self_node
A read-only Element object that represents the extension element
in the stylesheet.
input_node
The current context Element in the input document (also read-only).
output_parent
The current insertion point in the output document. You can
append elements or set the text value (not the tail). Apart from
that, the Element is read-only.
Declaring extension elements
----------------------------
In XSLT, extension elements can be used like any other XSLT element,
except that they must be declared as extensions using the standard
XSLT ``extension-element-prefixes`` option:
.. sourcecode:: pycon
>>> xslt_ext_tree = etree.XML('''
... <xsl:stylesheet version="1.0"
... xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
... xmlns:my="testns"
... extension-element-prefixes="my">
... <xsl:template match="/">
... <foo><my:ext><child>XYZ</child></my:ext></foo>
... </xsl:template>
... <xsl:template match="child">
... <CHILD>--xyz--</CHILD>
... </xsl:template>
... </xsl:stylesheet>''')
To register the extension, add its namespace and name to the extension
mapping of the XSLT object:
.. sourcecode:: pycon
>>> my_extension = MyExtElement()
>>> extensions = { ('testns', 'ext') : my_extension }
>>> transform = etree.XSLT(xslt_ext_tree, extensions = extensions)
Note how we pass an instance here, not the class of the extension.
Now we can run the transformation and see how our extension is
called:
.. sourcecode:: pycon
>>> root = etree.XML('<dummy/>')
>>> result = transform(root)
Hello from XSLT!
>>> str(result)
'<?xml version="1.0"?>\n<foo>I did it!<child>XYZ</child></foo>\n'
Applying XSL templates
----------------------
XSLT extensions are a very powerful feature that allows you to
interact directly with the XSLT processor. You have full read-only
access to the input document and the stylesheet, and you can even call
back into the XSLT processor to process templates. Here is an example
that passes an Element into the ``.apply_templates()`` method of the
``XSLTExtension`` instance:
.. sourcecode:: pycon
>>> class MyExtElement(etree.XSLTExtension):
... def execute(self, context, self_node, input_node, output_parent):
... child = self_node[0]
... results = self.apply_templates(context, child)
... output_parent.append(results[0])
>>> my_extension = MyExtElement()
>>> extensions = { ('testns', 'ext') : my_extension }
>>> transform = etree.XSLT(xslt_ext_tree, extensions = extensions)
>>> root = etree.XML('<dummy/>')
>>> result = transform(root)
>>> str(result)
'<?xml version="1.0"?>\n<foo><CHILD>--xyz--</CHILD></foo>\n'
Note how we applied the templates to a child of the extension element
itself, i.e. to an element inside the stylesheet instead of an element
of the input document.
Working with read-only elements
-------------------------------
There is one important thing to keep in mind: all Elements that the
``execute()`` method gets to deal with are read-only Elements, so you
cannot modify them. They also will not easily work in the API. For
example, you cannot pass them to the ``tostring()`` function or wrap
them in an ``ElementTree``.
What you can do, however, is to deepcopy them to make them normal
Elements, and then modify them using the normal etree API. So this
will work:
.. sourcecode:: pycon
>>> from copy import deepcopy
>>> class MyExtElement(etree.XSLTExtension):
... def execute(self, context, self_node, input_node, output_parent):
... child = deepcopy(self_node[0])
... child.text = "NEW TEXT"
... output_parent.append(child)
>>> my_extension = MyExtElement()
>>> extensions = { ('testns', 'ext') : my_extension }
>>> transform = etree.XSLT(xslt_ext_tree, extensions = extensions)
>>> root = etree.XML('<dummy/>')
>>> result = transform(root)
>>> str(result)
'<?xml version="1.0"?>\n<foo><child>NEW TEXT</child></foo>\n'
|