Re: schema problem
<jholg <at> gmx.de>
2012-02-08 15:37:08 GMT
Hi,
> <test
> xmlns="TestAutomation"
> xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
> xs:schemaLocation="TestAutomation TestAutomation.xsd" >
>
Parses ok for me:
>>> root = etree.fromstring("""
... <test
... xmlns="TestAutomation"
... xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
... xs:schemaLocation="TestAutomation TestAutomation.xsd" >
... <x>foobar</x>
... </test>
... """)
>>> print root
<Element {TestAutomation}test at 26bbd0>
>>>
> This makes the tree unparseable (iterators don't work, xpath doesn't
> work), specifically, None key causes this problem - even if I pass
> root.nsmap to 'namespaces' in element.xpath() call, I get TypeError (if
> I pass a copy of dictionary without None key, xpath and iterators
> silently fail, that is, they return empty results).
Iterators work ok:
>>> for elt in root.iter(): print elt
...
<Element {TestAutomation}test at 26bbd0>
<Element {TestAutomation}x at 26ba50>
>>> for elt in root.iter('test'): print elt
...
>>> for elt in root.iter('{TestAutomation}test'): print elt
...
<Element {TestAutomation}test at 26bbd0>
>>> for elt in root.iter('{TestAutomation}*'): print elt
...
<Element {TestAutomation}test at 26bbd0>
<Element {TestAutomation}x at 26ba50>
>>>
XPath works ok:
>>> root.xpath("//nsprefix:*", namespaces={'nsprefix':'TestAutomation'})[<Element
{TestAutomation}test at 26bbd0>, <Element {TestAutomation}x at 26ba50>]
>>>
>>> root.xpath("//nsprefix:test", namespaces={'nsprefix':'TestAutomation'})
[<Element {TestAutomation}test at 26bbd0>]
>>>
I suppose you've run into this:
>>> root.xpath("//test", namespaces={None:'TestAutomation'})
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "lxml.etree.pyx", line 1314, in lxml.etree._Element.xpath (src/lxml/lxml.etree.c:37234)
File "xpath.pxi", line 244, in lxml.etree.XPathElementEvaluator.__init__ (src/lxml/lxml.etree.c:103583)
File "xpath.pxi", line 117, in lxml.etree._XPathEvaluatorBase.__init__ (src/lxml/lxml.etree.c:102283)
File "xpath.pxi", line 55, in lxml.etree._XPathContext.__init__ (src/lxml/lxml.etree.c:101630)
File "extensions.pxi", line 78, in lxml.etree._BaseContext.__init__ (src/lxml/lxml.etree.c:94046)
TypeError: empty namespace prefix is not supported in XPath
>>>
?
You should be able to use prefixed XPath expression instead.
Might be worth reading http://lxml.de/tutorial.html#namespaces for clarification.
Note that your elements live in namespace "TestAutomation" (which happens to be used unprefixed in the xml
file). Personally it helps me a lot to think about namespaces in Clarke notation (see link above).
Hth
Holger
--
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
_________________________________________________________________
Mailing list for the lxml Python XML toolkit - http://lxml.de/
lxml <at> lxml.de
https://mailman-mail5.webfaction.com/listinfo/lxml