[lxml-dev] finding the line number of a parsed element

Ian Bicking ianb at colorstudy.com
Sun Apr 15 00:47:46 CEST 2007


Stefan Behnel wrote:
> Hi everyone,
> 
> Stefan Behnel wrote:
>> There is no API for it, but internally, we have this information for parsed
>> trees, at least the line number - note that exceptions contain the line number
>> already. So we could easily add a property "_line" to elements that returns
>> the line number at which the element was parsed (*if* it was parsed). I don't
>> like the fact so much that libxml2 puts a zero there
> 
> Sorry for the FUD. I just checked and found that libxml2 is actually smarter
> than I remembered from the last time I looked at this. It gives you a 1 for
> the first line in the parser. So it's actually easy to distinguish between "no
> line known" and "parsed in line x".
> 
> That makes "el.line" a perfectly working API. I called it "el.sourceline"
> though, to make it clearer that only parsing XML source produces it, not
> creating Elements in any other way. I also made it writable, just in case
> someone wants to add line numbers to generated trees or something.

Is there a file or resource name in there somewhere too?  This would be 
nice to have if, say, you were using xinclude to combine elements from 
different sources.


-- 
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org
             | Write code, do good | http://topp.openplans.org/careers


More information about the lxml-dev mailing list