1 | | {{{ |
2 | | #!rst |
3 | | ===================== |
4 | | Using XPath in Markup |
5 | | ===================== |
6 | | |
7 | | Markup provides basic XPath_ support for matching and querying event streams. |
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 | | |
21 | | Due to the streaming nature of the processing model, Markup uses only a subset |
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 | | |
34 | | This means you can't use the ``parent``, ancestor, or sibling axes in Markup |
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 | | |
39 | | Predicates are of course supported, but Path expressions *inside* predicates |
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 |
55 | | yet supported, whereas the various comparison and logical operators should work |
56 | | as expected. |
57 | | |
58 | | You can also use XPath variable references (``$var``) inside predicates. |
59 | | |
60 | | |
61 | | ---------------- |
62 | | Querying Streams |
63 | | ---------------- |
64 | | |
65 | | :: |
66 | | |
67 | | from markup.input import XML |
68 | | |
69 | | doc = XML('''<doc> |
70 | | <items count="2"> |
71 | | <item status="new"> |
72 | | <summary>Foo</summary> |
73 | | </item> |
74 | | <item status="closed"> |
75 | | <summary>Bar</summary> |
76 | | </item> |
77 | | </items> |
78 | | </doc>''') |
79 | | print doc.select('items/item[@status="closed"]/summary/text()') |
80 | | |
81 | | This would result in the following output:: |
82 | | |
83 | | Bar |
84 | | }}} |
| 1 | [[Include(trunk/doc/xpath.txt)]] |