[lxml-dev] special string subclasses for XPath string results
Stefan Behnel
stefan_ml at behnel.de
Thu Jan 10 00:33:12 CET 2008
Hi Ian,
I now implemented the basic behaviour on the trunk. It first crashed because
of a bug in Cython 0.9.6.10b, hope my patch will find its way into a release soon.
Attributes and text nodes are now handled. String results (such as returned by
the string() function) will remain plain strings - no way to recover here.
Ian Bicking wrote:
> For something like ::first-letter, I didn't really expect it to be
> possible to compile that to XPath. Instead it would have to be
> something like:
>
> def first_letter_selector(xpath_expr):
> def selector(el):
> result = xpath_expr(el)
> return result.text_range(0, 1)
> return selector
Something like that, yes. You could do
for element in xpath_expr(el):
for text in element.itertext()
if text:
return text[0]
return None # or raise or ...
or wouldn't we have to return a list here? As in
return [ text[0] for element in xpath_expr(el)
for text in element.itertext()
if text ]
> For representing ranges I'd also like some text range (I don't have any
> immediate needs, so I'm personally in no rush here). But it's not
> something that would have to replace the current text/tail attributes.
> It's just that in some code it can be nice to have something similar to
> the DOM TextNode, and this kind of provides that (except more nicely I
> think, as it would be more like a view).
>
> Then the range might just be like:
>
> class TextRange(object):
[...]
You could file this as a feature request in the bug tracker so that we won't
forget about it. It's definitely not clear enough for 2.0.
Stefan
More information about the lxml-dev
mailing list