Solved

XML, Python, xpath: returning empty list

Posted on 2013-01-09
5
1,171 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 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

631 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