Edgewall Software

source: trunk/doc/xpath.txt

Last change on this file was 1076, checked in by cmlenz, 14 years ago

Convert a bunch of print statements to py3k compatible syntax.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-rst
File size: 2.5 KB
RevLine 
[283]1.. -*- mode: rst; encoding: utf-8 -*-
2
3=====================
[287]4Using XPath in Genshi
[283]5=====================
6
[287]7Genshi 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-----------
18Limitations
19-----------
20
[287]21Due to the streaming nature of the processing model, Genshi uses only a subset
[283]22of the `XPath 1.0`_ language.
23
24.. _`XPath 1.0`: http://www.w3.org/TR/xpath
25
26In particular, only the following axes are supported:
27
28* ``attribute``
29* ``child``
30* ``descendant``
31* ``descendant-or-self``
32* ``self``
33
[287]34This 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
36for I don't know). Basically, any path expression that would require buffering
37of the stream is not supported.
38
[478]39Predicates are of course supported, but path expressions *inside* predicates
[283]40are restricted to attribute lookups (again due to the lack of buffering).
41
42Most of the XPath functions and operators are supported, however they
43(currently) only work inside predicates. The following functions are **not**
44supported:
45
46* ``count()``
47* ``id()``
48* ``lang()``
49* ``last()``
50* ``position()``
51* ``string()``
52* ``sum()``
53
54The mathematical operators (``+``, ``-``, ``*``, ``div``, and ``mod``) are not
[620]55yet supported, whereas sub-expressions and the various comparison and logical
56operators should work as expected.
[283]57
58You can also use XPath variable references (``$var``) inside predicates.
59
60
61----------------
62Querying Streams
63----------------
64
[614]65The ``Stream`` class provides a ``select(path)`` function that can be used to
66retrieve 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---------------------
96Matching in Templates
97---------------------
98
99See the directive ``py:match`` in the `XML Template Language Specification`_.
100
101.. _`XML Template Language Specification`: xml-templates.html
Note: See TracBrowser for help on using the repository browser.