Solved

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

Posted on 2011-03-18
7
602 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 60

Accepted Solution

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

Expert Comment

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

Expert Comment

by:pepr
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:geldfeld
Comment Utility
@pepr  the question was specific to libxml2

0
 

Author Comment

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

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0
 
LVL 28

Expert Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now