<DIV>&nbsp;</DIV><PRE>2009-07-20,"Stefan Behnel" &lt;stefan_ml@behnel.de&gt; :
&gt;
&gt;Nicholas Dudfield wrote:
&gt;&gt; Wow, someone else with this requirement. I was meaning to post to the
&gt;&gt; list about this. I'm using lxml to implement a XPath / CSS selection
&gt;&gt; plugin for a python extensible editor. I'd like to have a mapping of
&gt;&gt; view buffer regions to xml nodes.
&gt;
&gt;At least the line is available from the "sourceline" property of an element
&gt;- although only up to 65536:
&gt;
&gt;http://bugzilla.gnome.org/show_bug.cgi?id=325533
&gt;
&gt;If you are in a position to whitespace-clean and pretty-print the XML
&gt;document, that would give you a simple mapping from elements to document
&gt;positions that you can exploit at the application level. Even if you can't,
&gt;that would still give you a usable model to work with that you could match
&gt;with the original stream to find the 'real' positions.
&gt;
&gt;
&gt;&gt; The workaround I used to get the
&gt;&gt; exact character position was to use the feed interface, a character at
&gt;&gt; a time and manually monitor bytestream position. It's fairly slow
&gt;&gt; though. I'd like to implement this in CYthon or use whatever
&gt;&gt; underlying facility there is to speed it up.
&gt;
&gt;Speeding up this approach is pretty much futile IMHO. The parser gains
&gt;speed from efficient I/O and memory management. Passing a byte at a time
&gt;totally counters that (and even then it's only a *byte* at a time, not a
&gt;*character* at a time).
&gt;
&gt;What I could imagine to do instead is to traverse the element tree and to
&gt;do an incremental text search for each element tag (i.e. the regexp
&gt;"&lt;tagname\s") to recover the exact original positions. That would also
&gt;allow you to work at the character level rather than the byte level
&gt;(assuming that the editor works at that level, too). Searching for the
&gt;above regexp is safe as "&lt;" cannot occur anywhere in the XML data stream
&gt;except for a tag start/end or comment/PI (ok, minus DTDs, but that's easy
&gt;to catch using the line number of the root element).
&gt;
&gt;I will also check if there is a way to provide the position at the (target)
&gt;parser level, but that needs to fit the current interface. And I currently
&gt;do not have much time to dig into this.
&gt;
&gt;Stefan
&gt;_______________________________________________
&gt;lxml-dev mailing list
&gt;lxml-dev@codespeak.net
&gt;http://codespeak.net/mailman/listinfo/lxml-dev
</PRE><PRE>Hi, all</PRE><PRE>   I have tried to alter the libxml2 source to add a callback telling the current position when an element parsed. Now I wan't to know this position in lxml target parser. I don't know where and how to alter lxml source to add target parser event defination and event handler defination, so that my libxml2 new event(callback) can be processed. and I have nerver compile cython source before. Can any body give me some suggestion? </PRE><PRE>    Thank you!</PRE><PRE>Best Regards</PRE><br><!-- footer --><br><span title="neteasefooter"/><hr/>
<a href="http://count.mail.163.com/redirect/footer.htm?f=http://gouwu.youdao.com">200万种商品,最低价格,疯狂诱惑你</a>
</span>