Solved

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

Posted on 2011-03-18
7
607 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 500 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
Industry Leaders: 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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
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…

749 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