xpathxslt.txt 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. ========================
  2. XPath and XSLT with lxml
  3. ========================
  4. lxml supports XPath 1.0, XSLT 1.0 and the EXSLT extensions through
  5. libxml2 and libxslt in a standards compliant way.
  6. .. contents::
  7. ..
  8. 1 XPath
  9. 1.1 The ``xpath()`` method
  10. 1.2 Namespaces and prefixes
  11. 1.3 XPath return values
  12. 1.4 Generating XPath expressions
  13. 1.5 The ``XPath`` class
  14. 1.6 Regular expressions in XPath
  15. 1.7 The ``XPathEvaluator`` classes
  16. 1.8 ``ETXPath``
  17. 1.9 Error handling
  18. 2 XSLT
  19. 2.1 XSLT result objects
  20. 2.2 Stylesheet parameters
  21. 2.3 The ``xslt()`` tree method
  22. 2.4 Dealing with stylesheet complexity
  23. 2.5 Profiling
  24. The usual setup procedure:
  25. .. sourcecode:: pycon
  26. >>> from lxml import etree
  27. ..
  28. >>> try: from StringIO import StringIO
  29. ... except ImportError:
  30. ... from io import BytesIO
  31. ... def StringIO(s):
  32. ... if isinstance(s, str): s = s.encode("UTF-8")
  33. ... return BytesIO(s)
  34. >>> import sys
  35. >>> if sys.version_info[0] == 2:
  36. ... from __builtin__ import unicode as str
  37. XPath
  38. =====
  39. lxml.etree supports the simple path syntax of the `find, findall and
  40. findtext`_ methods on ElementTree and Element, as known from the original
  41. ElementTree library (ElementPath_). As an lxml specific extension, these
  42. classes also provide an ``xpath()`` method that supports expressions in the
  43. complete XPath syntax, as well as `custom extension functions`_.
  44. .. _ElementPath: http://effbot.org/zone/element-xpath.htm
  45. .. _`find, findall and findtext`: http://effbot.org/zone/element.htm#searching-for-subelements
  46. .. _`custom extension functions`: extensions.html#xpath-extension-functions
  47. .. _`XSLT extension elements`: extensions.html#xslt-extension-elements
  48. There are also specialized XPath evaluator classes that are more efficient for
  49. frequent evaluation: ``XPath`` and ``XPathEvaluator``. See the `performance
  50. comparison`_ to learn when to use which. Their semantics when used on
  51. Elements and ElementTrees are the same as for the ``xpath()`` method described
  52. here.
  53. .. note::
  54. The ``.find*()`` methods are usually *faster* than the full-blown XPath
  55. support. They also support incremental tree processing through the
  56. ``.iterfind()`` method, whereas XPath always collects all results before
  57. returning them. They are therefore recommended over XPath for both speed
  58. and memory reasons, whenever there is no need for highly selective XPath
  59. queries.
  60. .. _`performance comparison`: performance.html#xpath
  61. The ``xpath()`` method
  62. ----------------------
  63. For ElementTree, the xpath method performs a global XPath query against the
  64. document (if absolute) or against the root node (if relative):
  65. .. sourcecode:: pycon
  66. >>> f = StringIO('<foo><bar></bar></foo>')
  67. >>> tree = etree.parse(f)
  68. >>> r = tree.xpath('/foo/bar')
  69. >>> len(r)
  70. 1
  71. >>> r[0].tag
  72. 'bar'
  73. >>> r = tree.xpath('bar')
  74. >>> r[0].tag
  75. 'bar'
  76. When ``xpath()`` is used on an Element, the XPath expression is evaluated
  77. against the element (if relative) or against the root tree (if absolute):
  78. .. sourcecode:: pycon
  79. >>> root = tree.getroot()
  80. >>> r = root.xpath('bar')
  81. >>> r[0].tag
  82. 'bar'
  83. >>> bar = root[0]
  84. >>> r = bar.xpath('/foo/bar')
  85. >>> r[0].tag
  86. 'bar'
  87. >>> tree = bar.getroottree()
  88. >>> r = tree.xpath('/foo/bar')
  89. >>> r[0].tag
  90. 'bar'
  91. The ``xpath()`` method has support for XPath variables:
  92. .. sourcecode:: pycon
  93. >>> expr = "//*[local-name() = $name]"
  94. >>> print(root.xpath(expr, name = "foo")[0].tag)
  95. foo
  96. >>> print(root.xpath(expr, name = "bar")[0].tag)
  97. bar
  98. >>> print(root.xpath("$text", text = "Hello World!"))
  99. Hello World!
  100. Namespaces and prefixes
  101. -----------------------
  102. If your XPath expression uses namespace prefixes, you must define them
  103. in a prefix mapping. To this end, pass a dictionary to the
  104. ``namespaces`` keyword argument that maps the namespace prefixes used
  105. in the XPath expression to namespace URIs:
  106. .. sourcecode:: pycon
  107. >>> f = StringIO('''\
  108. ... <a:foo xmlns:a="http://codespeak.net/ns/test1"
  109. ... xmlns:b="http://codespeak.net/ns/test2">
  110. ... <b:bar>Text</b:bar>
  111. ... </a:foo>
  112. ... ''')
  113. >>> doc = etree.parse(f)
  114. >>> r = doc.xpath('/x:foo/b:bar',
  115. ... namespaces={'x': 'http://codespeak.net/ns/test1',
  116. ... 'b': 'http://codespeak.net/ns/test2'})
  117. >>> len(r)
  118. 1
  119. >>> r[0].tag
  120. '{http://codespeak.net/ns/test2}bar'
  121. >>> r[0].text
  122. 'Text'
  123. The prefixes you choose here are not linked to the prefixes used
  124. inside the XML document. The document may define whatever prefixes it
  125. likes, including the empty prefix, without breaking the above code.
  126. Note that XPath does not have a notion of a default namespace. The
  127. empty prefix is therefore undefined for XPath and cannot be used in
  128. namespace prefix mappings.
  129. There is also an optional ``extensions`` argument which is used to
  130. define `custom extension functions`_ in Python that are local to this
  131. evaluation. The namespace prefixes that they use in the XPath
  132. expression must also be defined in the namespace prefix mapping.
  133. XPath return values
  134. -------------------
  135. The return value types of XPath evaluations vary, depending on the
  136. XPath expression used:
  137. * True or False, when the XPath expression has a boolean result
  138. * a float, when the XPath expression has a numeric result (integer or float)
  139. * a 'smart' string (as described below), when the XPath expression has
  140. a string result.
  141. * a list of items, when the XPath expression has a list as result.
  142. The items may include Elements (also comments and processing
  143. instructions), strings and tuples. Text nodes and attributes in the
  144. result are returned as 'smart' string values. Namespace
  145. declarations are returned as tuples of strings: ``(prefix, URI)``.
  146. XPath string results are 'smart' in that they provide a
  147. ``getparent()`` method that knows their origin:
  148. * for attribute values, ``result.getparent()`` returns the Element
  149. that carries them. An example is ``//foo/@attribute``, where the
  150. parent would be a ``foo`` Element.
  151. * for the ``text()`` function (as in ``//text()``), it returns the
  152. Element that contains the text or tail that was returned.
  153. You can distinguish between different text origins with the boolean
  154. properties ``is_text``, ``is_tail`` and ``is_attribute``.
  155. Note that ``getparent()`` may not always return an Element. For
  156. example, the XPath functions ``string()`` and ``concat()`` will
  157. construct strings that do not have an origin. For them,
  158. ``getparent()`` will return None.
  159. There are certain cases where the smart string behaviour is
  160. undesirable. For example, it means that the tree will be kept alive
  161. by the string, which may have a considerable memory impact in the case
  162. that the string value is the only thing in the tree that is actually
  163. of interest. For these cases, you can deactivate the parental
  164. relationship using the keyword argument ``smart_strings``.
  165. .. sourcecode:: pycon
  166. >>> root = etree.XML("<root><a>TEXT</a></root>")
  167. >>> find_text = etree.XPath("//text()")
  168. >>> text = find_text(root)[0]
  169. >>> print(text)
  170. TEXT
  171. >>> print(text.getparent().text)
  172. TEXT
  173. >>> find_text = etree.XPath("//text()", smart_strings=False)
  174. >>> text = find_text(root)[0]
  175. >>> print(text)
  176. TEXT
  177. >>> hasattr(text, 'getparent')
  178. False
  179. Generating XPath expressions
  180. ----------------------------
  181. ElementTree objects have a method ``getpath(element)``, which returns a
  182. structural, absolute XPath expression to find that element:
  183. .. sourcecode:: pycon
  184. >>> a = etree.Element("a")
  185. >>> b = etree.SubElement(a, "b")
  186. >>> c = etree.SubElement(a, "c")
  187. >>> d1 = etree.SubElement(c, "d")
  188. >>> d2 = etree.SubElement(c, "d")
  189. >>> tree = etree.ElementTree(c)
  190. >>> print(tree.getpath(d2))
  191. /c/d[2]
  192. >>> tree.xpath(tree.getpath(d2)) == [d2]
  193. True
  194. The ``XPath`` class
  195. -------------------
  196. The ``XPath`` class compiles an XPath expression into a callable function:
  197. .. sourcecode:: pycon
  198. >>> root = etree.XML("<root><a><b/></a><b/></root>")
  199. >>> find = etree.XPath("//b")
  200. >>> print(find(root)[0].tag)
  201. b
  202. The compilation takes as much time as in the ``xpath()`` method, but it is
  203. done only once per class instantiation. This makes it especially efficient
  204. for repeated evaluation of the same XPath expression.
  205. Just like the ``xpath()`` method, the ``XPath`` class supports XPath
  206. variables:
  207. .. sourcecode:: pycon
  208. >>> count_elements = etree.XPath("count(//*[local-name() = $name])")
  209. >>> print(count_elements(root, name = "a"))
  210. 1.0
  211. >>> print(count_elements(root, name = "b"))
  212. 2.0
  213. This supports very efficient evaluation of modified versions of an XPath
  214. expression, as compilation is still only required once.
  215. Prefix-to-namespace mappings can be passed as second parameter:
  216. .. sourcecode:: pycon
  217. >>> root = etree.XML("<root xmlns='NS'><a><b/></a><b/></root>")
  218. >>> find = etree.XPath("//n:b", namespaces={'n':'NS'})
  219. >>> print(find(root)[0].tag)
  220. {NS}b
  221. Regular expressions in XPath
  222. ----------------------------
  223. By default, ``XPath`` supports regular expressions in the EXSLT_ namespace:
  224. .. sourcecode:: pycon
  225. >>> regexpNS = "http://exslt.org/regular-expressions"
  226. >>> find = etree.XPath("//*[re:test(., '^abc$', 'i')]",
  227. ... namespaces={'re':regexpNS})
  228. >>> root = etree.XML("<root><a>aB</a><b>aBc</b></root>")
  229. >>> print(find(root)[0].text)
  230. aBc
  231. .. _EXSLT: http://www.exslt.org/
  232. You can disable this with the boolean keyword argument ``regexp`` which
  233. defaults to True.
  234. The ``XPathEvaluator`` classes
  235. ------------------------------
  236. lxml.etree provides two other efficient XPath evaluators that work on
  237. ElementTrees or Elements respectively: ``XPathDocumentEvaluator`` and
  238. ``XPathElementEvaluator``. They are automatically selected if you use the
  239. XPathEvaluator helper for instantiation:
  240. .. sourcecode:: pycon
  241. >>> root = etree.XML("<root><a><b/></a><b/></root>")
  242. >>> xpatheval = etree.XPathEvaluator(root)
  243. >>> print(isinstance(xpatheval, etree.XPathElementEvaluator))
  244. True
  245. >>> print(xpatheval("//b")[0].tag)
  246. b
  247. This class provides efficient support for evaluating different XPath
  248. expressions on the same Element or ElementTree.
  249. ``ETXPath``
  250. -----------
  251. ElementTree supports a language named ElementPath_ in its ``find*()`` methods.
  252. One of the main differences between XPath and ElementPath is that the XPath
  253. language requires an indirection through prefixes for namespace support,
  254. whereas ElementTree uses the Clark notation (``{ns}name``) to avoid prefixes
  255. completely. The other major difference regards the capabilities of both path
  256. languages. Where XPath supports various sophisticated ways of restricting the
  257. result set through functions and boolean expressions, ElementPath only
  258. supports pure path traversal without nesting or further conditions. So, while
  259. the ElementPath syntax is self-contained and therefore easier to write and
  260. handle, XPath is much more powerful and expressive.
  261. lxml.etree bridges this gap through the class ``ETXPath``, which accepts XPath
  262. expressions with namespaces in Clark notation. It is identical to the
  263. ``XPath`` class, except for the namespace notation. Normally, you would
  264. write:
  265. .. sourcecode:: pycon
  266. >>> root = etree.XML("<root xmlns='ns'><a><b/></a><b/></root>")
  267. >>> find = etree.XPath("//p:b", namespaces={'p' : 'ns'})
  268. >>> print(find(root)[0].tag)
  269. {ns}b
  270. ``ETXPath`` allows you to change this to:
  271. .. sourcecode:: pycon
  272. >>> find = etree.ETXPath("//{ns}b")
  273. >>> print(find(root)[0].tag)
  274. {ns}b
  275. Error handling
  276. --------------
  277. lxml.etree raises exceptions when errors occur while parsing or evaluating an
  278. XPath expression:
  279. .. sourcecode:: pycon
  280. >>> find = etree.XPath("\\")
  281. Traceback (most recent call last):
  282. ...
  283. lxml.etree.XPathSyntaxError: Invalid expression
  284. lxml will also try to give you a hint what went wrong, so if you pass a more
  285. complex expression, you may get a somewhat more specific error:
  286. .. sourcecode:: pycon
  287. >>> find = etree.XPath("//*[1.1.1]")
  288. Traceback (most recent call last):
  289. ...
  290. lxml.etree.XPathSyntaxError: Invalid predicate
  291. During evaluation, lxml will emit an XPathEvalError on errors:
  292. .. sourcecode:: pycon
  293. >>> find = etree.XPath("//ns:a")
  294. >>> find(root)
  295. Traceback (most recent call last):
  296. ...
  297. lxml.etree.XPathEvalError: Undefined namespace prefix
  298. This works for the ``XPath`` class, however, the other evaluators (including
  299. the ``xpath()`` method) are one-shot operations that do parsing and evaluation
  300. in one step. They therefore raise evaluation exceptions in all cases:
  301. .. sourcecode:: pycon
  302. >>> root = etree.Element("test")
  303. >>> find = root.xpath("//*[1.1.1]")
  304. Traceback (most recent call last):
  305. ...
  306. lxml.etree.XPathEvalError: Invalid predicate
  307. >>> find = root.xpath("//ns:a")
  308. Traceback (most recent call last):
  309. ...
  310. lxml.etree.XPathEvalError: Undefined namespace prefix
  311. >>> find = root.xpath("\\")
  312. Traceback (most recent call last):
  313. ...
  314. lxml.etree.XPathEvalError: Invalid expression
  315. Note that lxml versions before 1.3 always raised an ``XPathSyntaxError`` for
  316. all errors, including evaluation errors. The best way to support older
  317. versions is to except on the superclass ``XPathError``.
  318. XSLT
  319. ====
  320. lxml.etree introduces a new class, lxml.etree.XSLT. The class can be
  321. given an ElementTree or Element object to construct an XSLT
  322. transformer:
  323. .. sourcecode:: pycon
  324. >>> xslt_root = etree.XML('''\
  325. ... <xsl:stylesheet version="1.0"
  326. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  327. ... <xsl:template match="/">
  328. ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  329. ... </xsl:template>
  330. ... </xsl:stylesheet>''')
  331. >>> transform = etree.XSLT(xslt_root)
  332. You can then run the transformation on an ElementTree document by simply
  333. calling it, and this results in another ElementTree object:
  334. .. sourcecode:: pycon
  335. >>> f = StringIO('<a><b>Text</b></a>')
  336. >>> doc = etree.parse(f)
  337. >>> result_tree = transform(doc)
  338. By default, XSLT supports all extension functions from libxslt and
  339. libexslt as well as Python regular expressions through the `EXSLT
  340. regexp functions`_. Also see the documentation on `custom extension
  341. functions`_, `XSLT extension elements`_ and `document resolvers`_.
  342. There is a separate section on `controlling access`_ to external
  343. documents and resources.
  344. .. _`EXSLT regexp functions`: http://www.exslt.org/regexp/
  345. .. _`document resolvers`: resolvers.html
  346. .. _`controlling access`: resolvers.html#i-o-access-control-in-xslt
  347. .. note::
  348. Due to a bug in libxslt the usage of ``<xsl:strip-space elements="*"/>``
  349. in an XSLT stylesheet can lead to crashes or memory failures. It is therefore
  350. advised not to use ``xsl:strip-space`` in stylesheets used with lxml.
  351. For details see: https://gitlab.gnome.org/GNOME/libxslt/-/issues/14
  352. XSLT result objects
  353. -------------------
  354. The result of an XSL transformation can be accessed like a normal ElementTree
  355. document:
  356. .. sourcecode:: pycon
  357. >>> root = etree.XML('<a><b>Text</b></a>')
  358. >>> result = transform(root)
  359. >>> result.getroot().text
  360. 'Text'
  361. but, as opposed to normal ElementTree objects, can also be turned into an (XML
  362. or text) string by applying the ``bytes()`` function (``str()`` in Python 2):
  363. .. sourcecode:: pycon
  364. >>> bytes(result)
  365. b'<?xml version="1.0"?>\n<foo>Text</foo>\n'
  366. The result is always a plain string, encoded as requested by the ``xsl:output``
  367. element in the stylesheet. If you want a Python Unicode/Text string instead,
  368. you should set this encoding to ``UTF-8`` (unless the `ASCII` default
  369. is sufficient). This allows you to call the builtin ``str()`` function on
  370. the result (``unicode()`` in Python 2):
  371. .. sourcecode:: pycon
  372. >>> str(result)
  373. u'<?xml version="1.0"?>\n<foo>Text</foo>\n'
  374. You can use other encodings at the cost of multiple recoding. Encodings that
  375. are not supported by Python will result in an error:
  376. .. sourcecode:: pycon
  377. >>> xslt_tree = etree.XML('''\
  378. ... <xsl:stylesheet version="1.0"
  379. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  380. ... <xsl:output encoding="UCS4"/>
  381. ... <xsl:template match="/">
  382. ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  383. ... </xsl:template>
  384. ... </xsl:stylesheet>''')
  385. >>> transform = etree.XSLT(xslt_tree)
  386. >>> result = transform(doc)
  387. >>> str(result)
  388. Traceback (most recent call last):
  389. ...
  390. LookupError: unknown encoding: UCS4
  391. While it is possible to use the ``.write()`` method (known from ``ElementTree``
  392. objects) to serialise the XSLT result into a file, it is better to use the
  393. ``.write_output()`` method. The latter knows about the ``<xsl:output>`` tag
  394. and writes the expected data into the output file.
  395. .. sourcecode:: pycon
  396. >>> xslt_root = etree.XML('''\
  397. ... <xsl:stylesheet version="1.0"
  398. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  399. ... <xsl:output method="text" encoding="utf8" />
  400. ... <xsl:template match="/">
  401. ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  402. ... </xsl:template>
  403. ... </xsl:stylesheet>''')
  404. >>> transform = etree.XSLT(xslt_root)
  405. >>> result = transform(doc)
  406. >>> result.write_output("output.txt.gz", compression=9) # doctest: +SKIP
  407. ..
  408. >>> from io import BytesIO
  409. >>> out = BytesIO()
  410. >>> result.write_output(out)
  411. >>> data = out.getvalue()
  412. >>> b'Text' in data
  413. True
  414. Stylesheet parameters
  415. ---------------------
  416. It is possible to pass parameters, in the form of XPath expressions, to the
  417. XSLT template:
  418. .. sourcecode:: pycon
  419. >>> xslt_tree = etree.XML('''\
  420. ... <xsl:stylesheet version="1.0"
  421. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  422. ... <xsl:param name="a" />
  423. ... <xsl:template match="/">
  424. ... <foo><xsl:value-of select="$a" /></foo>
  425. ... </xsl:template>
  426. ... </xsl:stylesheet>''')
  427. >>> transform = etree.XSLT(xslt_tree)
  428. >>> doc_root = etree.XML('<a><b>Text</b></a>')
  429. The parameters are passed as keyword parameters to the transform call.
  430. First, let's try passing in a simple integer expression:
  431. .. sourcecode:: pycon
  432. >>> result = transform(doc_root, a="5")
  433. >>> bytes(result)
  434. b'<?xml version="1.0"?>\n<foo>5</foo>\n'
  435. You can use any valid XPath expression as parameter value:
  436. .. sourcecode:: pycon
  437. >>> result = transform(doc_root, a="/a/b/text()")
  438. >>> bytes(result)
  439. b'<?xml version="1.0"?>\n<foo>Text</foo>\n'
  440. It's also possible to pass an XPath object as a parameter:
  441. .. sourcecode:: pycon
  442. >>> result = transform(doc_root, a=etree.XPath("/a/b/text()"))
  443. >>> bytes(result)
  444. b'<?xml version="1.0"?>\n<foo>Text</foo>\n'
  445. Passing a string expression looks like this:
  446. .. sourcecode:: pycon
  447. >>> result = transform(doc_root, a="'A'")
  448. >>> bytes(result)
  449. b'<?xml version="1.0"?>\n<foo>A</foo>\n'
  450. To pass a string that (potentially) contains quotes, you can use the
  451. ``.strparam()`` class method. Note that it does not escape the
  452. string. Instead, it returns an opaque object that keeps the string
  453. value.
  454. .. sourcecode:: pycon
  455. >>> plain_string_value = etree.XSLT.strparam(
  456. ... """ It's "Monty Python" """)
  457. >>> result = transform(doc_root, a=plain_string_value)
  458. >>> bytes(result)
  459. b'<?xml version="1.0"?>\n<foo> It\'s "Monty Python" </foo>\n'
  460. If you need to pass parameters that are not legal Python identifiers,
  461. pass them inside of a dictionary:
  462. .. sourcecode:: pycon
  463. >>> transform = etree.XSLT(etree.XML('''\
  464. ... <xsl:stylesheet version="1.0"
  465. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  466. ... <xsl:param name="non-python-identifier" />
  467. ... <xsl:template match="/">
  468. ... <foo><xsl:value-of select="$non-python-identifier" /></foo>
  469. ... </xsl:template>
  470. ... </xsl:stylesheet>'''))
  471. >>> result = transform(doc_root, **{'non-python-identifier': '5'})
  472. >>> bytes(result)
  473. b'<?xml version="1.0"?>\n<foo>5</foo>\n'
  474. Errors and messages
  475. -------------------
  476. Like most of the processing oriented objects in lxml.etree, ``XSLT``
  477. provides an error log that lists messages and error output from the
  478. last run. See the `parser documentation`_ for a description of the
  479. error log.
  480. .. _`parser documentation`: parsing.html#error-log
  481. .. sourcecode:: pycon
  482. >>> xslt_root = etree.XML('''\
  483. ... <xsl:stylesheet version="1.0"
  484. ... xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  485. ... <xsl:template match="/">
  486. ... <xsl:message terminate="no">STARTING</xsl:message>
  487. ... <foo><xsl:value-of select="/a/b/text()" /></foo>
  488. ... <xsl:message terminate="no">DONE</xsl:message>
  489. ... </xsl:template>
  490. ... </xsl:stylesheet>''')
  491. >>> transform = etree.XSLT(xslt_root)
  492. >>> doc_root = etree.XML('<a><b>Text</b></a>')
  493. >>> result = transform(doc_root)
  494. >>> bytes(result)
  495. b'<?xml version="1.0"?>\n<foo>Text</foo>\n'
  496. >>> print(transform.error_log)
  497. <string>:0:0:ERROR:XSLT:ERR_OK: STARTING
  498. <string>:0:0:ERROR:XSLT:ERR_OK: DONE
  499. >>> for entry in transform.error_log:
  500. ... print('message from line %s, col %s: %s' % (
  501. ... entry.line, entry.column, entry.message))
  502. ... print('domain: %s (%d)' % (entry.domain_name, entry.domain))
  503. ... print('type: %s (%d)' % (entry.type_name, entry.type))
  504. ... print('level: %s (%d)' % (entry.level_name, entry.level))
  505. ... print('filename: %s' % entry.filename)
  506. message from line 0, col 0: STARTING
  507. domain: XSLT (22)
  508. type: ERR_OK (0)
  509. level: ERROR (2)
  510. filename: <string>
  511. message from line 0, col 0: DONE
  512. domain: XSLT (22)
  513. type: ERR_OK (0)
  514. level: ERROR (2)
  515. filename: <string>
  516. Note that there is no way in XSLT to distinguish between user
  517. messages, warnings and error messages that occurred during the
  518. run. ``libxslt`` simply does not provide this information. You can
  519. partly work around this limitation by making your own messages
  520. uniquely identifiable, e.g. with a common text prefix.
  521. The ``xslt()`` tree method
  522. --------------------------
  523. There's also a convenience method on ElementTree objects for doing XSL
  524. transformations. This is less efficient if you want to apply the same XSL
  525. transformation to multiple documents, but is shorter to write for one-shot
  526. operations, as you do not have to instantiate a stylesheet yourself:
  527. .. sourcecode:: pycon
  528. >>> result = doc.xslt(xslt_tree, a="'A'")
  529. >>> bytes(result)
  530. b'<?xml version="1.0"?>\n<foo>A</foo>\n'
  531. This is a shortcut for the following code:
  532. .. sourcecode:: pycon
  533. >>> transform = etree.XSLT(xslt_tree)
  534. >>> result = transform(doc, a="'A'")
  535. >>> bytes(result)
  536. b'<?xml version="1.0"?>\n<foo>A</foo>\n'
  537. Dealing with stylesheet complexity
  538. ----------------------------------
  539. Some applications require a larger set of rather diverse stylesheets.
  540. lxml.etree allows you to deal with this in a number of ways. Here are
  541. some ideas to try.
  542. The most simple way to reduce the diversity is by using XSLT
  543. parameters that you pass at call time to configure the stylesheets.
  544. The ``partial()`` function in the ``functools`` module
  545. may come in handy here. It allows you to bind a set of keyword
  546. arguments (i.e. stylesheet parameters) to a reference of a callable
  547. stylesheet. The same works for instances of the ``XPath()``
  548. evaluator, obviously.
  549. You may also consider creating stylesheets programmatically. Just
  550. create an XSL tree, e.g. from a parsed template, and then add or
  551. replace parts as you see fit. Passing an XSL tree into the ``XSLT()``
  552. constructor multiple times will create independent stylesheets, so
  553. later modifications of the tree will not be reflected in the already
  554. created stylesheets. This makes stylesheet generation very straight
  555. forward.
  556. A third thing to remember is the support for `custom extension
  557. functions`_ and `XSLT extension elements`_. Some things are much
  558. easier to express in XSLT than in Python, while for others it is the
  559. complete opposite. Finding the right mixture of Python code and XSL
  560. code can help a great deal in keeping applications well designed and
  561. maintainable.
  562. Profiling
  563. ---------
  564. If you want to know how your stylesheet performed, pass the ``profile_run``
  565. keyword to the transform:
  566. .. sourcecode:: pycon
  567. >>> result = transform(doc, a="/a/b/text()", profile_run=True)
  568. >>> profile = result.xslt_profile
  569. The value of the ``xslt_profile`` property is an ElementTree with profiling
  570. data about each template, similar to the following:
  571. .. sourcecode:: xml
  572. <profile>
  573. <template rank="1" match="/" name="" mode="" calls="1" time="1" average="1"/>
  574. </profile>
  575. Note that this is a read-only document. You must not move any of its elements
  576. to other documents. Please deep-copy the document if you need to modify it.
  577. If you want to free it from memory, just do:
  578. .. sourcecode:: pycon
  579. >>> del result.xslt_profile