Home | History | Annotate | Download | only in tutorial
      1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Using XPath to Retrieve Element Content</title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s04.html" title="Retrieving Element Content"><link rel="next" href="ar01s06.html" title="Writing element content"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Using XPath to Retrieve Element Content</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="ar01s06.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xmltutorialxpath"></a>Using XPath to Retrieve Element Content</h2></div></div><div></div></div><p>In addition to walking the document tree to find an element,
      2     <span class="application">Libxml2</span> includes support for
      3       use of <span class="application">XPath</span> expressions to retrieve sets of
      4       nodes that match a specified criteria. Full documentation of the
      5       <span class="application">XPath</span> <span class="acronym">API</span> is <a href="http://xmlsoft.org/html/libxml-xpath.html" target="_top">here</a>.
      6     </p><p><span class="application">XPath</span> allows searching through a document
      7     for nodes that match specified criteria. In the example below we search
      8       through a document for the contents of all <tt class="varname">keyword</tt>
      9     elements.
     10       </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>A full discussion of <span class="application">XPath</span> is beyond
     11 	  the scope of this document. For details on its use, see the <a href="http://www.w3.org/TR/xpath" target="_top">XPath specification</a>.</p></td></tr></table></div><p>
     12       Full code for this example is at <a href="apd.html" title="D.Code for XPath Example">AppendixD, <i>Code for XPath Example</i></a>.
     13     </p><p>Using <span class="application">XPath</span> requires setting up an
     14       xmlXPathContext and then supplying the <span class="application">XPath</span>
     15       expression and the context to the
     16       <tt class="function">xmlXPathEvalExpression</tt> function. The function returns
     17       an xmlXPathObjectPtr, which includes the set of nodes satisfying the
     18       <span class="application">XPath</span> expression.</p><p>
     19       </p><pre class="programlisting">
     20 	xmlXPathObjectPtr
     21 	getnodeset (xmlDocPtr doc, xmlChar *xpath){
     22 	
     23 	<a name="cocontext"></a><img src="images/callouts/1.png" alt="1" border="0">xmlXPathContextPtr context;
     24 	xmlXPathObjectPtr result;
     25 
     26 	<a name="cocreatecontext"></a><img src="images/callouts/2.png" alt="2" border="0">context = xmlXPathNewContext(doc);
     27 	<a name="corunxpath"></a><img src="images/callouts/3.png" alt="3" border="0">result = xmlXPathEvalExpression(xpath, context);
     28 	<a name="cocheckxpathresult"></a><img src="images/callouts/4.png" alt="4" border="0">if(xmlXPathNodeSetIsEmpty(result-&gt;nodesetval)){
     29 		xmlXPathFreeObject(result);
     30                 printf("No result\n");
     31 		return NULL;
     32       </pre><p>
     33       </p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#cocontext"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>First we declare our variables.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#cocreatecontext"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Initialize the <tt class="varname">context</tt> variable.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#corunxpath"><img src="images/callouts/3.png" alt="3" border="0"></a> </td><td valign="top" align="left"><p>Apply the <span class="application">XPath</span> expression.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#cocheckxpathresult"><img src="images/callouts/4.png" alt="4" border="0"></a> </td><td valign="top" align="left"><p>Check the result and free the memory allocated to
     34 	    <tt class="varname">result</tt> if no result is found.</p></td></tr></table></div><p>
     35     </p><p>The xmlPathObjectPtr returned by the function contains a set of nodes
     36     and other information needed to iterate through the set and act on the
     37       results. For this example, our functions returns the
     38     <tt class="varname">xmlXPathObjectPtr</tt>. We use it to print the contents of
     39       <tt class="varname">keyword</tt> nodes in our document. The node set object
     40       includes the number of elements in the set (<tt class="varname">nodeNr</tt>) and
     41       an array of nodes (<tt class="varname">nodeTab</tt>):
     42       </p><pre class="programlisting">
     43 	<a name="conodesetcounter"></a><img src="images/callouts/1.png" alt="1" border="0">for (i=0; i &lt; nodeset-&gt;nodeNr; i++) {
     44 	<a name="coprintkeywords"></a><img src="images/callouts/2.png" alt="2" border="0">keyword = xmlNodeListGetString(doc, nodeset-&gt;nodeTab[i]-&gt;xmlChildrenNode, 1);
     45 		printf("keyword: %s\n", keyword);
     46 	        xmlFree(keyword);
     47 	}
     48       </pre><p>
     49       </p><div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#conodesetcounter"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>The value of <tt class="varname">nodeset-&gt;Nr</tt> holds the number of
     50 	  elements in the node set. Here we use it to iterate through the array.</p></td></tr><tr><td width="5%" valign="top" align="left"><a href="#coprintkeywords"><img src="images/callouts/2.png" alt="2" border="0"></a> </td><td valign="top" align="left"><p>Here we print the contents of each of the nodes returned.
     51 	    </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="images/note.png"></td><th align="left">Note</th></tr><tr><td colspan="2" align="left" valign="top"><p>Note that we are printing the child node of the node that is
     52 		returned, because the contents of the <tt class="varname">keyword</tt>
     53 		element are a child text node.</p></td></tr></table></div><p>
     54 	  </p></td></tr></table></div><p>
     55     </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s04.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ar01s06.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Retrieving Element Content</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Writing element content</td></tr></table></div></body></html>
     56