1
<?xml>
<env>
<body>
<folderxml>
<folder id='1'>
 <folder id='2' name ='document'>
   <folder id='3' name ='Music'>
     <folder id= '4' name= 'album' xlink='true'>
     </folder>
   </folder>
</folder>
</folder>
</folderxml>
</body>
</env>

I need the path if the attribute 'link' is true. Something like xpath.evaluate(path if(link=true)). The result should be something like "/documents/music/album"

1 Answers1

0

After applying the following XPath expression (based on XPath tutorial):

xmlXPathEval("//*[@xlink='true']", doc)

you obtain a link to the node list. In the example it is a single node folder with id 4. Then you need to write a function that will travell all the parents and concatenate their name attributes.

If you needed only the name of the matching folder, than you could go with

xmlXPathEval("string(//*[@xlink='true']/@name)", doc)

and read the stringval member of the result. But that matches only the first node.

Edit: As for the function to iterate over parents I thought of direct access through xmlNodePtr->parent member and reading attributes with xmlGetProp. But as I see your comment (and also the title of the question) I think that you could make use of relative xpath queries, see What's the most efficient way to do recursive XPath queries using libxml2?.

Community
  • 1
  • 1
Jarekczek
  • 7,456
  • 3
  • 46
  • 66
  • xpath.evaluate("//*[@link='true']/@name", xlink); then i do recursive to get the parent by adding /../ till the selected name. xpath.evaluate("//*[@link='true']/../@name", link); and then i append the values – user1374855 Oct 24 '12 at 09:53
  • Good suggestion, I needed to correct the second part of the answer. I added also another notice. – Jarekczek Oct 24 '12 at 11:33