[lxml-dev] Parsing this document
Hi everyone, Sorry to as a -users question on a -dev mailing-list, but I don't see a -users resource for lxml. If one exists, I'd be happy to head over there. I have an XML document from a network device that I want to pull into my python code. The example document I'm working with is attached as vlan.xml. I find the results of this to be utterly perplexing:
import lxml.etree as ET vtree = ET.parse("vlan.xml") vtree.xpath("vlan") [] vtree.xpath("//vlan") [] vtree.xpath("rpc-reply") [] vtree.xpath("/rpc-reply") [] vtree.xpath("*") [<Element {http://xml.juniper.net/junos/9.2R2/junos-esw}vlan-information at 95a8a04>] vtree.xpath("vlan-information") []
Based on the examples in the lxml XPath documentation, I'd expect the first to give me a list of Elements, one for each <vlan>...</vlan> in the file. Second should be the same based on XPath stuff I've read in Python and XML. My next two are shots in the dark: <rpc-reply> is the top-level element, but I can't retrieve it relatively or absolutely. Next is another shot in the dark, and somehow what I get the element one under the top-level. Huh? Also, I can't retrieve it by name. -- Ross Vandegrift ross@kallisti.us "If the fight gets hot, the songs get hotter. If the going gets tough, the songs get tougher." --Woody Guthrie
On Wed, 2008-11-19 at 15:19 -0500, Ross Vandegrift wrote:
vtree.xpath("*") [<Element {http://xml.juniper.net/junos/9.2R2/junos-esw}vlan-information at 95a8a04>]
From this result, it shows that at least your root element has a namespace. Take a look at this section of the documentation: http://codespeak.net/lxml/tutorial.html#namespaces
-- John Krukoff <jkrukoff@ltgc.com> Land Title Guarantee Company
On Wed, Nov 19, 2008 at 01:25:27PM -0700, John Krukoff wrote:
On Wed, 2008-11-19 at 15:19 -0500, Ross Vandegrift wrote:
vtree.xpath("*") [<Element {http://xml.juniper.net/junos/9.2R2/junos-esw}vlan-information at 95a8a04>]
From this result, it shows that at least your root element has a namespace. Take a look at this section of the documentation: http://codespeak.net/lxml/tutorial.html#namespaces
Ahhhhh, until I read that, I don't think I actually understood namespaces at all. It's funny how I don't think I saw anywhere in the books I've read that namespaces can be nested and an internal namespace overrides ones already defined. Suddenly, things become quite a bit more clear :) Thanks a bunch, Ross
-- John Krukoff <jkrukoff@ltgc.com> Land Title Guarantee Company
-- Ross Vandegrift ross@kallisti.us "If the fight gets hot, the songs get hotter. If the going gets tough, the songs get tougher." --Woody Guthrie
participants (2)
-
John Krukoff
-
Ross Vandegrift