Blame doc/tutorial/ar01s05.html

Packit 423ecb
<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">

Using XPath to Retrieve Element Content

In addition to walking the document tree to find an element,

Packit 423ecb
    Libxml2 includes support for
Packit 423ecb
      use of XPath expressions to retrieve sets of
Packit 423ecb
      nodes that match a specified criteria. Full documentation of the
Packit 423ecb
      XPath API is here.
Packit 423ecb
    

XPath allows searching through a document

Packit 423ecb
    for nodes that match specified criteria. In the example below we search
Packit 423ecb
      through a document for the contents of all <tt class="varname">keyword</tt>
Packit 423ecb
    elements.
Packit 423ecb
      

[Note]Note

A full discussion of XPath is beyond

Packit 423ecb
	  the scope of this document. For details on its use, see the XPath specification.

Packit 423ecb
      Full code for this example is at Appendix D, Code for XPath Example.
Packit 423ecb
    

Using XPath requires setting up an

Packit 423ecb
      xmlXPathContext and then supplying the XPath
Packit 423ecb
      expression and the context to the
Packit 423ecb
      <tt class="function">xmlXPathEvalExpression</tt> function. The function returns
Packit 423ecb
      an xmlXPathObjectPtr, which includes the set of nodes satisfying the
Packit 423ecb
      XPath expression.

Packit 423ecb
      

Packit 423ecb
	xmlXPathObjectPtr
Packit 423ecb
	getnodeset (xmlDocPtr doc, xmlChar *xpath){
Packit 423ecb
	
Packit 423ecb
	1xmlXPathContextPtr context;
Packit 423ecb
	xmlXPathObjectPtr result;
Packit 423ecb
Packit 423ecb
	2context = xmlXPathNewContext(doc);
Packit 423ecb
	3result = xmlXPathEvalExpression(xpath, context);
Packit 423ecb
	4if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
Packit 423ecb
		xmlXPathFreeObject(result);
Packit 423ecb
                printf("No result\n");
Packit 423ecb
		return NULL;
Packit 423ecb
      

Packit 423ecb
      

1

First we declare our variables.

2

Initialize the <tt class="varname">context</tt> variable.

3

Apply the XPath expression.

4

Check the result and free the memory allocated to

Packit 423ecb
	    <tt class="varname">result</tt> if no result is found.

Packit 423ecb
    

The xmlPathObjectPtr returned by the function contains a set of nodes

Packit 423ecb
    and other information needed to iterate through the set and act on the
Packit 423ecb
      results. For this example, our functions returns the
Packit 423ecb
    <tt class="varname">xmlXPathObjectPtr</tt>. We use it to print the contents of
Packit 423ecb
      <tt class="varname">keyword</tt> nodes in our document. The node set object
Packit 423ecb
      includes the number of elements in the set (<tt class="varname">nodeNr</tt>) and
Packit 423ecb
      an array of nodes (<tt class="varname">nodeTab</tt>):
Packit 423ecb
      

Packit 423ecb
	1for (i=0; i < nodeset->nodeNr; i++) {
Packit 423ecb
	2keyword = xmlNodeListGetString(doc, nodeset->nodeTab[i]->xmlChildrenNode, 1);
Packit 423ecb
		printf("keyword: %s\n", keyword);
Packit 423ecb
	        xmlFree(keyword);
Packit 423ecb
	}
Packit 423ecb
      

Packit 423ecb
      

1

The value of <tt class="varname">nodeset->Nr</tt> holds the number of

Packit 423ecb
	  elements in the node set. Here we use it to iterate through the array.

2

Here we print the contents of each of the nodes returned.

Packit 423ecb
	    

[Note]Note

Note that we are printing the child node of the node that is

Packit 423ecb
		returned, because the contents of the <tt class="varname">keyword</tt>
Packit 423ecb
		element are a child text node.

Packit 423ecb
	  

Packit 423ecb
    

</body></html>