Solved

XML, Python, xpath: returning empty list

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jsp insert to database example 2 59
maven disable workspace resolution 1 37
Java basic valueOf question 1 32
Strange router problem - can't access hotmail.com 14 35
The purpose of this article is to demonstrate how we can use conditional statements using Python.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

713 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