Thomas Schraitle schrieb am 21.05.2015 um 10:28:
assume I have the following Python 3 code:
------------------------------ import io from lxml import etree
source = """<?xml version="1.0"?> <article version="5.0" xml:lang="en" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"> <title>...</title> <para>...</para> </article> """
tree = etree.parse(io.StringIO(source)) root = tree.getroot() print(root.sourceline) ------------------------------
When I run the above code, I get "4" as a result. This is a bit unexpected.
It seems, root.sourceline returns the line number where the start tag _ends_. However, I need to get the line number where <article> _starts_ (here in this example "2").
It seems that this behaviour applies only to the root element, though:
"""
In [15]: source = '''<?xml version="1.0"?>
....: