| [283] | 1 | .. -*- mode: rst; encoding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | ===================== |
|---|
| [287] | 4 | Using XPath in Genshi |
|---|
| [283] | 5 | ===================== |
|---|
| 6 | |
|---|
| [287] | 7 | Genshi provides basic XPath_ support for matching and querying event streams. |
|---|
| [283] | 8 | |
|---|
| 9 | .. _xpath: http://www.w3.org/TR/xpath |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | .. contents:: Contents |
|---|
| 13 | :depth: 2 |
|---|
| 14 | .. sectnum:: |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | ----------- |
|---|
| 18 | Limitations |
|---|
| 19 | ----------- |
|---|
| 20 | |
|---|
| [287] | 21 | Due to the streaming nature of the processing model, Genshi uses only a subset |
|---|
| [283] | 22 | of the `XPath 1.0`_ language. |
|---|
| 23 | |
|---|
| 24 | .. _`XPath 1.0`: http://www.w3.org/TR/xpath |
|---|
| 25 | |
|---|
| 26 | In particular, only the following axes are supported: |
|---|
| 27 | |
|---|
| 28 | * ``attribute`` |
|---|
| 29 | * ``child`` |
|---|
| 30 | * ``descendant`` |
|---|
| 31 | * ``descendant-or-self`` |
|---|
| 32 | * ``self`` |
|---|
| 33 | |
|---|
| [287] | 34 | This means you can't use the ``parent``, ancestor, or sibling axes in Genshi |
|---|
| [283] | 35 | (the ``namespace`` axis isn't supported either, but what you'd ever need that |
|---|
| 36 | for I don't know). Basically, any path expression that would require buffering |
|---|
| 37 | of the stream is not supported. |
|---|
| 38 | |
|---|
| [478] | 39 | Predicates are of course supported, but path expressions *inside* predicates |
|---|
| [283] | 40 | are restricted to attribute lookups (again due to the lack of buffering). |
|---|
| 41 | |
|---|
| 42 | Most of the XPath functions and operators are supported, however they |
|---|
| 43 | (currently) only work inside predicates. The following functions are **not** |
|---|
| 44 | supported: |
|---|
| 45 | |
|---|
| 46 | * ``count()`` |
|---|
| 47 | * ``id()`` |
|---|
| 48 | * ``lang()`` |
|---|
| 49 | * ``last()`` |
|---|
| 50 | * ``position()`` |
|---|
| 51 | * ``string()`` |
|---|
| 52 | * ``sum()`` |
|---|
| 53 | |
|---|
| 54 | The mathematical operators (``+``, ``-``, ``*``, ``div``, and ``mod``) are not |
|---|
| [620] | 55 | yet supported, whereas sub-expressions and the various comparison and logical |
|---|
| 56 | operators should work as expected. |
|---|
| [283] | 57 | |
|---|
| 58 | You can also use XPath variable references (``$var``) inside predicates. |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | ---------------- |
|---|
| 62 | Querying Streams |
|---|
| 63 | ---------------- |
|---|
| 64 | |
|---|
| [614] | 65 | The ``Stream`` class provides a ``select(path)`` function that can be used to |
|---|
| 66 | retrieve subsets of the stream: |
|---|
| [283] | 67 | |
|---|
| [614] | 68 | .. code-block:: pycon |
|---|
| [283] | 69 | |
|---|
| [614] | 70 | >>> from genshi.input import XML |
|---|
| [620] | 71 | |
|---|
| [614] | 72 | >>> doc = XML('''<doc> |
|---|
| [620] | 73 | ... <items count="4"> |
|---|
| 74 | ... < item status="new"> |
|---|
| 75 | ... <summary>Foo</summary> |
|---|
| 76 | ... </item> |
|---|
| 77 | ... <item status="closed"> |
|---|
| 78 | . .. <summary>Bar</summary> |
|---|
| 79 | ... </item> |
|---|
| 80 | ... <item status="closed" re solution="invalid"> |
|---|
| 81 | ... <summary>Baz</summary> |
|---|
| 82 | ... </item> |
|---|
| 83 | ... <item status="closed" re solution="fixed"> |
|---|
| 84 | ... <summary>Waz</summary> |
|---|
| 85 | ... </item> |
|---|
| 86 | ... </items> |
|---|
| [614] | 87 | . .. </doc>''') |
|---|
| [283] | 88 | |
|---|
| [1076] | 89 | >>> print(doc.select('items/item[@status="closed" and ' |
|---|
| 90 | ... '(@resolution="invalid" or not(@resolution))]/summary/text()')) |
|---|
| [620] | 91 | BarBaz |
|---|
| [283] | 92 | |
|---|
| [620] | 93 | |
|---|
| 94 | |
|---|
| [283] | 95 | --------------------- |
|---|
| 96 | Matching in Templates |
|---|
| 97 | --------------------- |
|---|
| 98 | |
|---|
| 99 | See the directive ``py:match`` in the `XML Template Language Specification`_. |
|---|
| 100 | |
|---|
| 101 | .. _`XML Template Language Specification`: xml-templates.html |
|---|