Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XML, Python, xpath: returning empty list

Posted on 2013-01-09
5
Medium Priority
?
1,202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
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.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

688 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