?
Solved

How to perform Xpath queries on Children Nodes  ( using libxml2 )

Posted on 2011-03-18
7
Medium Priority
?
609 Views
Last Modified: 2012-05-11
I want to perform Xpath queries in the context of a chld node and not the document. Below I have
code similar to what I am doing. When i try:

      evaluator.setNewContext(node)
      typeA = evaluator.xpathEval("//node/contentNodeTypeA")

Open in new window


the xpathquery: //node/contentNodeTypeA  is executed on the entire document.

Below you'll find the code that illustrates what I am trying to do.

<rootNode>
     <nodes>
               <node>    <contentNodeTypeA/><contentNodeTypeB/>   </node>
               <node>     <contentNodeTypeA/><contentNodeTypeB/>   </node>
               <node>     <contentNodeTypeA/><contentNodeTypeB/>    </node>
      </nodes>
</rootNode>

Open in new window


Using libxml2 i do the following
import libxml2

doc = libxml2.parseFile ("doc.xml")
evaluator = doc.xpathNewContext()

nodeList  = evaluator.xpathEval("//rootNode/nodes/node")

for node in nodeList:
      evaluator.setNewContext(node)
      typeA = evaluator.xpathEval("//node/contentNodeTypeA")
      typeB = evaluator.xpathEval("//node/contentNodeTypeA")
      

Open in new window

0
Comment
Question by:geldfeld
[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
  • 2
7 Comments
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 35164557
'//foo' always starts from the document node
try
'.//foo' instead, '.' being the context node
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35164567
note that you will not find ".//node/contentNodeTypeA" since you are allready in the context of "node"
you will need ".//contentNodeTypeA" instead
0
 
LVL 29

Expert Comment

by:pepr
ID: 35164721
Have a look at Chapter 12 in "Dive Into Python 3" by Mark Pilgrim (http://diveintopython3.org/xml.html) namely the 12.6. Going Further With lxml (http://diveintopython3.org/xml.html#xml-lxml) for some details.

Try to do the XPath expressions in more steps to see what do.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:geldfeld
ID: 35167046
@pepr  the question was specific to libxml2

0
 

Author Comment

by:geldfeld
ID: 35167056
@gertone - thanks that worked. It was silly of me. My Xpath was wrong.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35167060
welcome
0
 
LVL 29

Expert Comment

by:pepr
ID: 35171837
The lxml module IS built around the libxml2 library and libxslt -- see http://lxml.de/
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

752 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