Solved

XML, Python, xpath: returning empty list

Posted on 2013-01-09
5
1,038 Views
Last Modified: 2013-01-10
Hi,

I am trying to read the Length of all the Field nodes of my xml file using xpath, but my xpath query is returning an empty list.  I need help in my xpath query. My python code and axml.xml are posted below.

========================= code =======================
tree = etree.parse("c:\\axml.xml")
rxpath = tree.xpath("//soap/soap/res/jx/fx/FA/Field/Length/text()")
print (rxpath)
   
output:
==========
 [ ]



============== axml.xml file ============
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <soap:Body>
            <res xmlns="http://www.abcxp.com">
                  <jx xmlns="" xsi:type="gxen:output">
                        <fx xsi:type="name:Fields">
                              <FA xsi:type="name:ArrayOfField">
                                    <Field xsi:type="name:Field">
                                          <Name>machine1</Name>
                                          <Type>xp</Type>
                                          <Length>4</Length>
                                    </Field>
                                    <Field xsi:type="name:Field">
                                          <Name>IDFNDFIELD</Name>
                                          <Type>win7</Type>
                                          <Length>10</Length>
                                    </Field>
                              </FA>
                        </fx>
                  </jx>
            </res>
      </soap:Body>
</soap:Envelope>



Thanks,
Gaurav
0
Comment
Question by:zeinth
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 38761281
you have namespaces in your document,
so you need to bind the namespace to a prefix and prefix the lot

see
http://lxml.de/xpathxslt.html

try

rxpath = tree.xpath("//soap:Envelope/soap:Body/abc:res/jx/fx/FA/Field/Length/text()", namespaces={'soap':  'http://schemas.xmlsoap.org/soap/envelope/', 'abc': 'http://www.abcxp.com'})
0
 

Author Comment

by:zeinth
ID: 38764089
awesome, it works in first attempt, thanks Gertone! for the xpath expression...

I have one last question here, from where this namespace key 'abc' is coming in the xpath
expression

namespace = {'abc': 'http://www.abcxp.com'}

in xml file this namespace looks like, in the xpath expression I tried to use "xmlns" instead of 'abc' but it didn't work.

 <res xmlns="http://www.abcxp.com">
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38764202
ah, well xmlns="..." indicates the default namespace. It is teh namespace all elements have (below the declaration) that don't have a prefix
xmlns:abc="..." binds a prefix to a specific namespace
you can't tell etree that in teh xpath unprefixed elements are in a default namespace, so you need to bind the default namespace to a prefix

prefixes are a local pointer to a namespace. The namespace itself is important. So you can choose any prefix at any time, as long as it refers to the correct namespace

instead of
namespace = {'abc': 'http://www.abcxp.com'}
you could do
namespace = {'banana': 'http://www.abcxp.com'}
as long as you do then in the XPath
...("//soap:Envelope/soap:Body/banana:res ...
0
 

Author Comment

by:zeinth
ID: 38764292
thanks for explaining "prefixes are a local pointer to a namespace"
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 38764434
welcome
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now