Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 751 lines (543 sloc) 23.029 kb
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 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
========================
XPath and XSLT with lxml
========================

lxml supports XPath 1.0, XSLT 1.0 and the EXSLT extensions through
libxml2 and libxslt in a standards compliant way.

.. contents::
..
   1 XPath
     1.1 The ``xpath()`` method
     1.2 Namespaces and prefixes
     1.3 XPath return values
     1.4 Generating XPath expressions
     1.5 The ``XPath`` class
     1.6 Regular expressions in XPath
     1.7 The ``XPathEvaluator`` classes
     1.8 ``ETXPath``
     1.9 Error handling
   2 XSLT
     2.1 XSLT result objects
     2.2 Stylesheet parameters
     2.3 The ``xslt()`` tree method
     2.4 Dealing with stylesheet complexity
     2.5 Profiling

The usual setup procedure:

.. sourcecode:: pycon

  >>> from lxml import etree

..
  >>> 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)

  >>> try: unicode = __builtins__["unicode"]
  ... except (NameError, KeyError): unicode = str


XPath
=====

lxml.etree supports the simple path syntax of the `find, findall and
findtext`_ methods on ElementTree and Element, as known from the original
ElementTree library (ElementPath_). As an lxml specific extension, these
classes also provide an ``xpath()`` method that supports expressions in the
complete XPath syntax, as well as `custom extension functions`_.

.. _ElementPath: http://effbot.org/zone/element-xpath.htm
.. _`find, findall and findtext`: http://effbot.org/zone/element.htm#searching-for-subelements
.. _`custom extension functions`: extensions.html#xpath-extension-functions
.. _`XSLT extension elements`: extensions.html#xslt-extension-elements

There are also specialized XPath evaluator classes that are more efficient for
frequent evaluation: ``XPath`` and ``XPathEvaluator``. See the `performance
comparison`_ to learn when to use which. Their semantics when used on
Elements and ElementTrees are the same as for the ``xpath()`` method described
here.

.. _`performance comparison`: performance.html#xpath


The ``xpath()`` method
----------------------

For ElementTree, the xpath method performs a global XPath query against the
document (if absolute) or against the root node (if relative):

.. sourcecode:: pycon

  >>> f = StringIO('<foo><bar></bar></foo>')
  >>> tree = etree.parse(f)

  >>> r = tree.xpath('/foo/bar')
  >>> len(r)
  1
  >>> r[0].tag
  'bar'

  >>> r = tree.xpath('bar')
  >>> r[0].tag
  'bar'

When ``xpath()`` is used on an Element, the XPath expression is evaluated
against the element (if relative) or against the root tree (if absolute):

.. sourcecode:: pycon

  >>> root = tree.getroot()
  >>> r = root.xpath('bar')
  >>> r[0].tag
  'bar'

  >>> bar = root[0]
  >>> r = bar.xpath('/foo/bar')
  >>> r[0].tag
  'bar'

  >>> tree = bar.getroottree()
  >>> r = tree.xpath('/foo/bar')
  >>> r[0].tag
  'bar'

The ``xpath()`` method has support for XPath variables:

.. sourcecode:: pycon

  >>> expr = "//*[local-name() = $name]"

  >>> print(root.xpath(expr, name = "foo")[0].tag)
  foo

  >>> print(root.xpath(expr, name = "bar")[0].tag)
  bar

  >>> print(root.xpath("$text", text = "Hello World!"))
  Hello World!


Namespaces and prefixes
-----------------------

If your XPath expression uses namespace prefixes, you must define them
in a prefix mapping. To this end, pass a dictionary to the
``namespaces`` keyword argument that maps the namespace prefixes used
in the XPath expression to namespace URIs:

.. sourcecode:: pycon

  >>> f = StringIO('''\
  ... <a:foo xmlns:a="http://codespeak.net/ns/test1"
  ... xmlns:b="http://codespeak.net/ns/test2">
  ... <b:bar>Text</b:bar>
  ... </a:foo>
  ... ''')
  >>> doc = etree.parse(f)

  >>> r = doc.xpath('/t:foo/b:bar',
  ... namespaces={'t': 'http://codespeak.net/ns/test1',
  ... 'b': 'http://codespeak.net/ns/test2'})
  >>> len(r)
  1
  >>> r[0].tag
  '{http://codespeak.net/ns/test2}bar'
  >>> r[0].text
  'Text'

The prefixes you choose here are not linked to the prefixes used
inside the XML document. The document may define whatever prefixes it
likes, including the empty prefix, without breaking the above code.

Note that XPath does not have a notion of a default namespace. The
empty prefix is therefore undefined for XPath and cannot be used in
namespace prefix mappings.

There is also an optional ``extensions`` argument which is used to
define `custom extension functions`_ in Python that are local to this
evaluation. The namespace prefixes that they use in the XPath
expression must also be defined in the namespace prefix mapping.


XPath return values
-------------------

The return value types of XPath evaluations vary, depending on the
XPath expression used:

* True or False, when the XPath expression has a boolean result

* a float, when the XPath expression has a numeric result (integer or float)

* a 'smart' string (as described below), when the XPath expression has
  a string result.

* a list of items, when the XPath expression has a list as result.
  The items may include Elements (also comments and processing
  instructions), strings and tuples. Text nodes and attributes in the
  result are returned as 'smart' string values. Namespace
  declarations are returned as tuples of strings: ``(prefix, URI)``.

XPath string results are 'smart' in that they provide a
``getparent()`` method that knows their origin:

* for attribute values, ``result.getparent()`` returns the Element
  that carries them. An example is ``//foo/@attribute``, where the
  parent would be a ``foo`` Element.

* for the ``text()`` function (as in ``//text()``), it returns the
  Element that contains the text or tail that was returned.

You can distinguish between different text origins with the boolean
properties ``is_text``, ``is_tail`` and ``is_attribute``.

Note that ``getparent()`` may not always return an Element. For
example, the XPath functions ``string()`` and ``concat()`` will
construct strings that do not have an origin. For them,
``getparent()`` will return None.

There are certain cases where the smart string behaviour is
undesirable. For example, it means that the tree will be kept alive
by the string, which may have a considerable memory impact in the case
that the string value is the only thing in the tree that is actually
of interest. For these cases, you can deactivate the parental
relationship using the keyword argument ``smart_strings``.

.. sourcecode:: pycon

  >>> root = etree.XML("<root><a>TEXT</a></root>")

  >>> find_text = etree.XPath("//text()")
  >>> text = find_text(root)[0]
  >>> print(text)
  TEXT
  >>> print(text.getparent().text)
  TEXT

  >>> find_text = etree.XPath("//text()", smart_strings=False)
  >>> text = find_text(root)[0]
  >>> print(text)
  TEXT
  >>> hasattr(text, 'getparent')
  False


Generating XPath expressions
----------------------------

ElementTree objects have a method ``getpath(element)``, which returns a
structural, absolute XPath expression to find that element:

.. sourcecode:: pycon

  >>> a = etree.Element("a")
  >>> b = etree.SubElement(a, "b")
  >>> c = etree.SubElement(a, "c")
  >>> d1 = etree.SubElement(c, "d")
  >>> d2 = etree.SubElement(c, "d")

  >>> tree = etree.ElementTree(c)
  >>> print(tree.getpath(d2))
  /c/d[2]
  >>> tree.xpath(tree.getpath(d2)) == [d2]
  True


The ``XPath`` class
-------------------

The ``XPath`` class compiles an XPath expression into a callable function:

.. sourcecode:: pycon

  >>> root = etree.XML("<root><a><b/></a><b/></root>")

  >>> find = etree.XPath("//b")
  >>> print(find(root)[0].tag)
  b

The compilation takes as much time as in the ``xpath()`` method, but it is
done only once per class instantiation. This makes it especially efficient
for repeated evaluation of the same XPath expression.

Just like the ``xpath()`` method, the ``XPath`` class supports XPath
variables:

.. sourcecode:: pycon

  >>> count_elements = etree.XPath("count(//*[local-name() = $name])")

  >>> print(count_elements(root, name = "a"))
  1.0
  >>> print(count_elements(root, name = "b"))
  2.0

This supports very efficient evaluation of modified versions of an XPath
expression, as compilation is still only required once.

Prefix-to-namespace mappings can be passed as second parameter:

.. sourcecode:: pycon

  >>> root = etree.XML("<root xmlns='NS'><a><b/></a><b/></root>")

  >>> find = etree.XPath("//n:b", namespaces={'n':'NS'})
  >>> print(find(root)[0].tag)
  {NS}b


Regular expressions in XPath
----------------------------

By default, ``XPath`` supports regular expressions in the EXSLT_ namespace:

.. sourcecode:: pycon

  >>> regexpNS = "http://exslt.org/regular-expressions"
  >>> find = etree.XPath("//*[re:test(., '^abc$', 'i')]",
  ... namespaces={'re':regexpNS})

  >>> root = etree.XML("<root><a>aB</a><b>aBc</b></root>")
  >>> print(find(root)[0].text)
  aBc

.. _EXSLT: http://www.exslt.org/

You can disable this with the boolean keyword argument ``regexp`` which
defaults to True.


The ``XPathEvaluator`` classes
------------------------------

lxml.etree provides two other efficient XPath evaluators that work on
ElementTrees or Elements respectively: ``XPathDocumentEvaluator`` and
``XPathElementEvaluator``. They are automatically selected if you use the
XPathEvaluator helper for instantiation:

.. sourcecode:: pycon

  >>> root = etree.XML("<root><a><b/></a><b/></root>")
  >>> xpatheval = etree.XPathEvaluator(root)

  >>> print(isinstance(xpatheval, etree.XPathElementEvaluator))
  True

  >>> print(xpatheval("//b")[0].tag)
  b

This class provides efficient support for evaluating different XPath
expressions on the same Element or ElementTree.


``ETXPath``
-----------

ElementTree supports a language named ElementPath_ in its ``find*()`` methods.
One of the main differences between XPath and ElementPath is that the XPath
language requires an indirection through prefixes for namespace support,
whereas ElementTree uses the Clark notation (``{ns}name``) to avoid prefixes
completely. The other major difference regards the capabilities of both path
languages. Where XPath supports various sophisticated ways of restricting the
result set through functions and boolean expressions, ElementPath only
supports pure path traversal without nesting or further conditions. So, while
the ElementPath syntax is self-contained and therefore easier to write and
handle, XPath is much more powerful and expressive.

lxml.etree bridges this gap through the class ``ETXPath``, which accepts XPath
expressions with namespaces in Clark notation. It is identical to the
``XPath`` class, except for the namespace notation. Normally, you would
write:

.. sourcecode:: pycon

  >>> root = etree.XML("<root xmlns='ns'><a><b/></a><b/></root>")

  >>> find = etree.XPath("//p:b", namespaces={'p' : 'ns'})
  >>> print(find(root)[0].tag)
  {ns}b

``ETXPath`` allows you to change this to:

.. sourcecode:: pycon

  >>> find = etree.ETXPath("//{ns}b")
  >>> print(find(root)[0].tag)
  {ns}b


Error handling
--------------

lxml.etree raises exceptions when errors occur while parsing or evaluating an
XPath expression:

.. sourcecode:: pycon

  >>> find = etree.XPath("\\")
  Traceback (most recent call last):
    ...
  lxml.etree.XPathSyntaxError: Invalid expression

lxml will also try to give you a hint what went wrong, so if you pass a more
complex expression, you may get a somewhat more specific error:

.. sourcecode:: pycon

  >>> find = etree.XPath("//*[1.1.1]")
  Traceback (most recent call last):
    ...
  lxml.etree.XPathSyntaxError: Invalid predicate

During evaluation, lxml will emit an XPathEvalError on errors:

.. sourcecode:: pycon

  >>> find = etree.XPath("//ns:a")
  >>> find(root)
  Traceback (most recent call last):
    ...
  lxml.etree.XPathEvalError: Undefined namespace prefix

This works for the ``XPath`` class, however, the other evaluators (including
the ``xpath()`` method) are one-shot operations that do parsing and evaluation
in one step. They therefore raise evaluation exceptions in all cases:

.. sourcecode:: pycon

  >>> root = etree.Element("test")
  >>> find = root.xpath("//*[1.1.1]")
  Traceback (most recent call last):
    ...
  lxml.etree.XPathEvalError: Invalid predicate

  >>> find = root.xpath("//ns:a")
  Traceback (most recent call last):
    ...
  lxml.etree.XPathEvalError: Undefined namespace prefix

  >>> find = root.xpath("\\")
  Traceback (most recent call last):
    ...
  lxml.etree.XPathEvalError: Invalid expression

Note that lxml versions before 1.3 always raised an ``XPathSyntaxError`` for
all errors, including evaluation errors. The best way to support older
versions is to except on the superclass ``XPathError``.


XSLT
====

lxml.etree introduces a new class, lxml.etree.XSLT. The class can be
given an ElementTree or Element object to construct an XSLT
transformer:

.. sourcecode:: pycon

  >>> xslt_root = etree.XML('''\
  ... <xsl:stylesheet version="1.0"
  ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... <xsl:template match="/">
  ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  ... </xsl:template>
  ... </xsl:stylesheet>''')
  >>> transform = etree.XSLT(xslt_root)

You can then run the transformation on an ElementTree document by simply
calling it, and this results in another ElementTree object:

.. sourcecode:: pycon

  >>> f = StringIO('<a><b>Text</b></a>')
  >>> doc = etree.parse(f)
  >>> result_tree = transform(doc)

By default, XSLT supports all extension functions from libxslt and
libexslt as well as Python regular expressions through the `EXSLT
regexp functions`_. Also see the documentation on `custom extension
functions`_, `XSLT extension elements`_ and `document resolvers`_.
There is a separate section on `controlling access`_ to external
documents and resources.

.. _`EXSLT regexp functions`: http://www.exslt.org/regexp/
.. _`document resolvers`: resolvers.html
.. _`controlling access`: resolvers.html#i-o-access-control-in-xslt


XSLT result objects
-------------------

The result of an XSL transformation can be accessed like a normal ElementTree
document:

.. sourcecode:: pycon

  >>> root = etree.XML('<a><b>Text</b></a>')
  >>> result = transform(root)

  >>> result.getroot().text
  'Text'

but, as opposed to normal ElementTree objects, can also be turned into an (XML
or text) string by applying the str() function:

.. sourcecode:: pycon

  >>> str(result)
  '<?xml version="1.0"?>\n<foo>Text</foo>\n'

The result is always a plain string, encoded as requested by the
``xsl:output`` element in the stylesheet. If you want a Python unicode string
instead, you should set this encoding to ``UTF-8`` (unless the `ASCII` default
is sufficient). This allows you to call the builtin ``unicode()`` function on
the result:

.. sourcecode:: pycon

  >>> unicode(result)
  u'<?xml version="1.0"?>\n<foo>Text</foo>\n'

You can use other encodings at the cost of multiple recoding. Encodings that
are not supported by Python will result in an error:

.. sourcecode:: pycon

  >>> xslt_tree = etree.XML('''\
  ... <xsl:stylesheet version="1.0"
  ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... <xsl:output encoding="UCS4"/>
  ... <xsl:template match="/">
  ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  ... </xsl:template>
  ... </xsl:stylesheet>''')
  >>> transform = etree.XSLT(xslt_tree)

  >>> result = transform(doc)
  >>> unicode(result)
  Traceback (most recent call last):
    ...
  LookupError: unknown encoding: UCS4


Stylesheet parameters
---------------------

It is possible to pass parameters, in the form of XPath expressions, to the
XSLT template:

.. sourcecode:: pycon

  >>> xslt_tree = etree.XML('''\
  ... <xsl:stylesheet version="1.0"
  ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... <xsl:param name="a" />
  ... <xsl:template match="/">
  ... <foo><xsl:value-of select="$a" /></foo>
  ... </xsl:template>
  ... </xsl:stylesheet>''')
  >>> transform = etree.XSLT(xslt_tree)
  >>> doc_root = etree.XML('<a><b>Text</b></a>')

The parameters are passed as keyword parameters to the transform call.
First, let's try passing in a simple integer expression:

.. sourcecode:: pycon

  >>> result = transform(doc_root, a="5")
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>5</foo>\n'

You can use any valid XPath expression as parameter value:

.. sourcecode:: pycon

  >>> result = transform(doc_root, a="/a/b/text()")
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>Text</foo>\n'

It's also possible to pass an XPath object as a parameter:

.. sourcecode:: pycon

  >>> result = transform(doc_root, a=etree.XPath("/a/b/text()"))
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>Text</foo>\n'

Passing a string expression looks like this:

.. sourcecode:: pycon

  >>> result = transform(doc_root, a="'A'")
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>A</foo>\n'

To pass a string that (potentially) contains quotes, you can use the
``.strparam()`` class method. Note that it does not escape the
string. Instead, it returns an opaque object that keeps the string
value.

.. sourcecode:: pycon

  >>> plain_string_value = etree.XSLT.strparam(
  ... """ It's "Monty Python" """)
  >>> result = transform(doc_root, a=plain_string_value)
  >>> str(result)
  '<?xml version="1.0"?>\n<foo> It\'s "Monty Python" </foo>\n'

If you need to pass parameters that are not legal Python identifiers,
pass them inside of a dictionary:

.. sourcecode:: pycon

  >>> transform = etree.XSLT(etree.XML('''\
  ... <xsl:stylesheet version="1.0"
  ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... <xsl:param name="non-python-identifier" />
  ... <xsl:template match="/">
  ... <foo><xsl:value-of select="$non-python-identifier" /></foo>
  ... </xsl:template>
  ... </xsl:stylesheet>'''))

  >>> result = transform(doc_root, **{'non-python-identifier': '5'})
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>5</foo>\n'



Errors and messages
-------------------

Like most of the processing oriented objects in lxml.etree, ``XSLT``
provides an error log that lists messages and error output from the
last run. See the `parser documentation`_ for a description of the
error log.

.. _`parser documentation`: parsing.html#error-log

.. sourcecode:: pycon

  >>> xslt_root = etree.XML('''\
  ... <xsl:stylesheet version="1.0"
  ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  ... <xsl:template match="/">
  ... <xsl:message terminate="no">STARTING</xsl:message>
  ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  ... <xsl:message terminate="no">DONE</xsl:message>
  ... </xsl:template>
  ... </xsl:stylesheet>''')
  >>> transform = etree.XSLT(xslt_root)

  >>> doc_root = etree.XML('<a><b>Text</b></a>')
  >>> result = transform(doc_root)
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>Text</foo>\n'

  >>> print(transform.error_log)
  <string>:0:0:ERROR:XSLT:ERR_OK: STARTING
  <string>:0:0:ERROR:XSLT:ERR_OK: DONE

  >>> for entry in transform.error_log:
  ... print('message from line %s, col %s: %s' % (
  ... entry.line, entry.column, entry.message))
  ... print('domain: %s (%d)' % (entry.domain_name, entry.domain))
  ... print('type: %s (%d)' % (entry.type_name, entry.type))
  ... print('level: %s (%d)' % (entry.level_name, entry.level))
  ... print('filename: %s' % entry.filename)
  message from line 0, col 0: STARTING
  domain: XSLT (22)
  type: ERR_OK (0)
  level: ERROR (2)
  filename: <string>
  message from line 0, col 0: DONE
  domain: XSLT (22)
  type: ERR_OK (0)
  level: ERROR (2)
  filename: <string>

Note that there is no way in XSLT to distinguish between user
messages, warnings and error messages that occurred during the
run. ``libxslt`` simply does not provide this information. You can
partly work around this limitation by making your own messages
uniquely identifiable, e.g. with a common text prefix.


The ``xslt()`` tree method
--------------------------

There's also a convenience method on ElementTree objects for doing XSL
transformations. This is less efficient if you want to apply the same XSL
transformation to multiple documents, but is shorter to write for one-shot
operations, as you do not have to instantiate a stylesheet yourself:

.. sourcecode:: pycon

  >>> result = doc.xslt(xslt_tree, a="'A'")
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>A</foo>\n'

This is a shortcut for the following code:

.. sourcecode:: pycon

  >>> transform = etree.XSLT(xslt_tree)
  >>> result = transform(doc, a="'A'")
  >>> str(result)
  '<?xml version="1.0"?>\n<foo>A</foo>\n'


Dealing with stylesheet complexity
----------------------------------

Some applications require a larger set of rather diverse stylesheets.
lxml.etree allows you to deal with this in a number of ways. Here are
some ideas to try.

The most simple way to reduce the diversity is by using XSLT
parameters that you pass at call time to configure the stylesheets.
The ``partial()`` function in the ``functools`` module of Python 2.5
may come in handy here. It allows you to bind a set of keyword
arguments (i.e. stylesheet parameters) to a reference of a callable
stylesheet. The same works for instances of the ``XPath()``
evaluator, obviously.

You may also consider creating stylesheets programmatically. Just
create an XSL tree, e.g. from a parsed template, and then add or
replace parts as you see fit. Passing an XSL tree into the ``XSLT()``
constructor multiple times will create independent stylesheets, so
later modifications of the tree will not be reflected in the already
created stylesheets. This makes stylesheet generation very straight
forward.

A third thing to remember is the support for `custom extension
functions`_ and `XSLT extension elements`_. Some things are much
easier to express in XSLT than in Python, while for others it is the
complete opposite. Finding the right mixture of Python code and XSL
code can help a great deal in keeping applications well designed and
maintainable.


Profiling
---------

If you want to know how your stylesheet performed, pass the ``profile_run``
keyword to the transform:

.. sourcecode:: pycon

  >>> result = transform(doc, a="/a/b/text()", profile_run=True)
  >>> profile = result.xslt_profile

The value of the ``xslt_profile`` property is an ElementTree with profiling
data about each template, similar to the following:

.. sourcecode:: xml

  <profile>
    <template rank="1" match="/" name="" mode="" calls="1" time="1" average="1"/>
  </profile>

Note that this is a read-only document. You must not move any of its elements
to other documents. Please deep-copy the document if you need to modify it.
If you want to free it from memory, just do:

.. sourcecode:: pycon

  >>> del result.xslt_profile
Something went wrong with that request. Please try again.