blob: 1c40925275f4fb9771d4d48b4adce218be74d87e [file] [log] [blame]
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +00001<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,
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +00002 <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.
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000010 </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>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000012 Full code for this example is at <a href="apd.html" title="D. Code for XPath Example">Appendix D, <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>
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000019 </p><pre class="programlisting">
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000020 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;
MDT 2002 John Fleck598f6eb2002-06-04 15:10:36 +000025
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000026 <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)){
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000029 xmlXPathFreeObject(result);
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000030 printf("No result\n");
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000031 return NULL;
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000032 </pre><p>
MDT 2004 John Fleck4c3bb7d2004-08-25 02:51:27 +000033 </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>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000035 </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>):
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000042 </p><pre class="programlisting">
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000043 <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);
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000046 xmlFree(keyword);
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000047 }
MST 2003 John Fleck731967e2003-01-27 00:39:50 +000048 </pre><p>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000049 </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.
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000051 </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
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000052 returned, because the contents of the <tt class="varname">keyword</tt>
MST 2004 John Fleckd14bccc2004-02-15 01:57:42 +000053 element are a child text node.</p></td></tr></table></div><p>
MDT 2003 John Fleck63f3a472003-07-24 21:48:30 +000054 </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>