Blame doc/tutorial/ar01s05.html

Packit Service a31ea6
<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 Service a31ea6
    Libxml2 includes support for
Packit Service a31ea6
      use of XPath expressions to retrieve sets of
Packit Service a31ea6
      nodes that match a specified criteria. Full documentation of the
Packit Service a31ea6
      XPath API is here.
Packit Service a31ea6
    

XPath allows searching through a document

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

[Note]Note

A full discussion of XPath is beyond

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

Packit Service a31ea6
      Full code for this example is at Appendix D, Code for XPath Example.
Packit Service a31ea6
    

Using XPath requires setting up an

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

Packit Service a31ea6
      

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

Packit Service a31ea6
      

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 Service a31ea6
	    <tt class="varname">result</tt> if no result is found.

Packit Service a31ea6
    

The xmlPathObjectPtr returned by the function contains a set of nodes

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

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

Packit Service a31ea6
      

1

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

Packit Service a31ea6
	  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 Service a31ea6
	    

[Note]Note

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

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

Packit Service a31ea6
	  

Packit Service a31ea6
    

</body></html>