Solved

XML, Python, xpath: returning empty list

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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. 
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

734 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