position attribute of XMLSyntaxError seems wrong
Hi, I'm catching XMLSyntaxError's in my app and displaying information regarding the error. In particular, the line and column of the error form the 'position' attribute of the exception. The line is fine, but the column doesn't seem to correspond to the point where I would consider the error to have occurred. Here's a small Python 2 that shows the problem: xml = b"""<test> <tag foo="some text" namespace:attr="value" /> </test> """ from lxml import etree import io lines = xml.splitlines() try: root = etree.parse(io.BytesIO(xml)).getroot() except Exception as e: line, col = e.position print lines[line - 1] print " " * (col - 1) + '^' I get the following output from that: <tag foo="some text" namespace:attr="value" /> ^ Any help would be appreciated... Will McGugan -- Will McGugan http://www.willmcgugan.com
Hi Will, Am Freitag, 22. August 2014, 17:49:03 schrieb Will McGugan:
[...] Here's a small Python 2 that shows the problem:
xml = b"""<test> <tag foo="some text" namespace:attr="value" /> </test>
[...]
I get the following output from that:
<tag foo="some text" namespace:attr="value" /> ^ Any help would be appreciated...
I haven't tested it, but your XML misses the namespace declaration (the attribute xmlns). Something like this: <test xmlns:namespace="urn:x-foo:will"> <tag foo="some text" namespace:attr="value"/> </test> The xmlns line contains the prefix (in the above example, that is "namespace") and the namespace URL (that is "urn:x-foo:will", a constant which depends on your XML format). What matters is the namespace URL, not the prefix. You can choose whatever you like for your prefix. For example, in XML terms, this would be equivalently the same: <test xmlns:x="urn:x-foo:will"> <tag foo="some text" x:attr="value"/> </test> For better readability, use one or two character for your prefix. Plus avoid meaningless prefixes like "namespace". ;) -- Gruß/Regards Thomas Schraitle
Hi, Has anyone got any feedback on this? I know the XML is broken, my problem is that the 'position' attribute on XMLSyntaxError is wrong. I would like this information in order to generate a error message for the user. I can file a bug report, unless anyone has any suggestions? Will McGugan On Sat, Aug 23, 2014 at 9:16 AM, Thomas Schraitle <tom_schr@web.de> wrote:
Hi Will,
Am Freitag, 22. August 2014, 17:49:03 schrieb Will McGugan:
[...] Here's a small Python 2 that shows the problem:
xml = b"""<test> <tag foo="some text" namespace:attr="value" /> </test>
[...]
I get the following output from that:
<tag foo="some text" namespace:attr="value" /> ^ Any help would be appreciated...
I haven't tested it, but your XML misses the namespace declaration (the attribute xmlns). Something like this:
<test xmlns:namespace="urn:x-foo:will"> <tag foo="some text" namespace:attr="value"/> </test>
The xmlns line contains the prefix (in the above example, that is "namespace") and the namespace URL (that is "urn:x-foo:will", a constant which depends on your XML format).
What matters is the namespace URL, not the prefix. You can choose whatever you like for your prefix. For example, in XML terms, this would be equivalently the same:
<test xmlns:x="urn:x-foo:will"> <tag foo="some text" x:attr="value"/> </test>
For better readability, use one or two character for your prefix. Plus avoid meaningless prefixes like "namespace". ;)
-- Gruß/Regards Thomas Schraitle
_________________________________________________________________ Mailing list for the lxml Python XML toolkit - http://lxml.de/ lxml@lxml.de https://mailman-mail5.webfaction.com/listinfo/lxml
-- Will McGugan http://www.willmcgugan.com
participants (2)
-
Thomas Schraitle
-
Will McGugan