Mercurial > genshi > genshi-test
view doc/xpath.txt @ 311:01e2c48f6dfb
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
* The `TemplateLoader` class can now also be initialized from a string for the search path, for cases where the search path contains only a single directory.
author | cmlenz |
---|---|
date | Sun, 22 Oct 2006 14:57:40 +0000 |
parents | 24757b771651 |
children | ebc7c1a3bc4d |
line wrap: on
line source
.. -*- mode: rst; encoding: utf-8 -*- ===================== Using XPath in Genshi ===================== Genshi provides basic XPath_ support for matching and querying event streams. .. _xpath: http://www.w3.org/TR/xpath .. contents:: Contents :depth: 2 .. sectnum:: ----------- Limitations ----------- Due to the streaming nature of the processing model, Genshi uses only a subset of the `XPath 1.0`_ language. .. _`XPath 1.0`: http://www.w3.org/TR/xpath In particular, only the following axes are supported: * ``attribute`` * ``child`` * ``descendant`` * ``descendant-or-self`` * ``self`` This means you can't use the ``parent``, ancestor, or sibling axes in Genshi (the ``namespace`` axis isn't supported either, but what you'd ever need that for I don't know). Basically, any path expression that would require buffering of the stream is not supported. Predicates are of course supported, but Path expressions *inside* predicates are restricted to attribute lookups (again due to the lack of buffering). Most of the XPath functions and operators are supported, however they (currently) only work inside predicates. The following functions are **not** supported: * ``count()`` * ``id()`` * ``lang()`` * ``last()`` * ``position()`` * ``string()`` * ``sum()`` The mathematical operators (``+``, ``-``, ``*``, ``div``, and ``mod``) are not yet supported, whereas the various comparison and logical operators should work as expected. You can also use XPath variable references (``$var``) inside predicates. ---------------- Querying Streams ---------------- :: from genshi.input import XML doc = XML('''<doc> <items count="2"> <item status="new"> <summary>Foo</summary> </item> <item status="closed"> <summary>Bar</summary> </item> </items> </doc>''') print doc.select('items/item[@status="closed"]/summary/text()') This would result in the following output:: Bar --------------------- Matching in Templates --------------------- See the directive ``py:match`` in the `XML Template Language Specification`_. .. _`XML Template Language Specification`: xml-templates.html