CINXE.COM
XPath Node Set
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="keywords" content="xpath node set, syntax, programming, how to, xml tutorial, extensible markup language, learn xml"> <meta name="Description" content="Learn about XPath node sets with this free XML tutorial."> <link rel="canonical" href="https://www.quackit.com/xml/tutorial/xpath_node_set.cfm"> <title>XPath Node Set</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <link rel="shortcut icon" href="/pix/favicon96.png"> <link rel="apple-touch-icon" href="/pix/apple-touch-icon.png"> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet"> <link href="/common/css/master.45.min.css" rel="stylesheet"> <script async src="https://cdn.fuseplatform.net/publift/tags/2/3499/fuse.js"></script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-Q3H025ZKLN"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q3H025ZKLN'); </script> </head> <body> <header class="site-header"> <div class="site-header-base"> <div class="site-logo"> <a title="Quackit Homepage" target="_top" href="/"><img src="/pix/quackit_logo_watermark.png" width="87" height="33" alt="Quackit Logo"></a> </div> <button id="site-nav-toggler" class="site-nav-toggler" aria-expanded="false" aria-controls="site-nav"> <span class="sr-only">Toggle navigation</span> ☰ </button> </div> <nav id="site-nav" class="site-nav"> <div class="site-links"> <ul> <li><a href="/"><i class="fa fa-home"></i> <span class="sr-only">Home</span></a></li> <li><a href="/html/">HTML</a></li> <li><a href="/css/">CSS</a></li> <li><a href="/scripting/">Scripting</a></li> <li><a href="/database/">Database</a></li> </ul> </div> <div class="site-search-top"> <form action="/search/" id="cse-search-box-bottom" class="site-search"> <div> <input type="hidden" name="cx" value="partner-pub-6331358926293806:98x0fk-bbgi"> <input type="hidden" name="cof" value="FORID:10"> <input type="hidden" name="ie" value="ISO-8859-1"> <input type="text" name="q" size="20" class="site-search-input"> <button type="submit" name="sa" class="site-search-button"><i class="fa fa-search"></i></button> </div> </form> </div> </nav> </header> <div class="main"> <article class="content"> <h1 class="page-title">XPath Node Set</h1> <div class="ad ad-top"> <!-- GAM 71161633/QCKIT_quackit/article_header --> <div data-fuse="23059883623"></div> </div> <ul class="pager"> <li><a href="/xml/tutorial/xpath_predicate.cfm"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> XPath Predicate</a></li> <li><a href="/xml/tutorial/xpath_comparison_operators.cfm">Comparison Operators <i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></li> </ul> <p class="lead">A <dfn>node set</dfn> is a set of nodes. When you write an XPath expression to return one or more nodes, you call these nodes a node set.</p> <div class="ad"> <!-- GAM 71161633/QCKIT_quackit/article_incontent_1 --> <div data-fuse="23059883629"></div> </div> <p>For example, if you use the following expression to return a node called <code>title</code>, you will have a set of nodes all called <code>title</code> (assuming there's more than one record). </p> <script src="/common/js/codemirror/lib/codemirror.js"></script> <script src="/common/js/codemirror/mode/htmlmixed/htmlmixed.js"></script> <script src="/common/js/codemirror/mode/css/css.js"></script> <script src="/common/js/codemirror/mode/javascript/javascript.js"></script> <script src="/common/js/codemirror/mode/xml/xml.js"></script> <div class="code-only"> <textarea id="example1" autocomplete="off" spellcheck="false">child::title</textarea> </div> <script> var exampleCode1 = CodeMirror.fromTextArea(document.getElementById("example1"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <h2>Node Set Functions</h2> <p>You can use the following functions when working with node sets:</p> <table class="tabular"> <tr><th>Function</th><th>Description</th></tr> <tr><td>last()</td><td>Returns the number of nodes in a node set.</td></tr> <tr><td>position()</td><td>Returns the position of the context node (current node). The starting value is 1. As you loop through each node, the position increments. </td></tr> <tr><td>count(<i>node1, node2, ...</i>)</td><td>Returns the total number of nodes in the node set as provided between the parentheses. If you leave the parentheses blank, it will use the context node.</td></tr> <tr><td>id((<i>string1, string2, ...</i>) <i>node</i>)</td><td>Returns the nodes whose ID matches the string/s passed to the function.</td></tr> <tr><td>local-name(<i>node_set</i>)</td><td>Returns the local name of the first node in the node set. The local name is the name without the namespace prefix. To use the context node, simply leave <i>node_set</i> blank.</td></tr> <tr><td>namespace-uri(<i>node_set</i>)</td><td>Returns the URI of the namespace of the first node in the node set. To use the context node, leave <i> node_set</i> blank.</td></tr> <tr><td>name(<i>node_set</i>)</td><td>Returns the full, qualified name of the first node in the node set. To use the context node, simply leave <i>node_set</i> blank.</td></tr> </table> <h2>Node Set Function Example</h2> <h3>The Source XML File</h3> <p>Imagine we have the following XML file:</p> <script src="/common/js/codemirror/mode/htmlmixed/htmlmixed.js"></script> <div class="code-only"> <textarea id="example2" autocomplete="off" spellcheck="false"><?xml version="1.0"?>
<food_list>
 <food_item type="vegetable">
 <name>Agar</name>
 <carbs_per_serving>81</carbs_per_serving>
 <fiber_per_serving>8</fiber_per_serving>
 <fat_per_serving>0.5</fat_per_serving>
 <kj_per_serving>1280</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Asparagus</name>
 <carbs_per_serving>1</carbs_per_serving>
 <fiber_per_serving>1</fiber_per_serving>
 <fat_per_serving>0</fat_per_serving>
 <kj_per_serving>40</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Cabbage</name>
 <carbs_per_serving>0</carbs_per_serving>
 <fiber_per_serving>1</fiber_per_serving>
 <fat_per_serving>0</fat_per_serving>
 <kj_per_serving>14</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Potato</name>
 <carbs_per_serving>21.5</carbs_per_serving>
 <fiber_per_serving>2</fiber_per_serving>
 <fat_per_serving>1</fat_per_serving>
 <kj_per_serving>460</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Pumpkin</name>
 <carbs_per_serving>6</carbs_per_serving>
 <fiber_per_serving>1</fiber_per_serving>
 <fat_per_serving>0.5</fat_per_serving>
 <kj_per_serving>150</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Yam</name>
 <carbs_per_serving>30.5</carbs_per_serving>
 <fiber_per_serving>2</fiber_per_serving>
 <fat_per_serving>0.5</fat_per_serving>
 <kj_per_serving>550</kj_per_serving>
 </food_item>
 <food_item type="vegetable">
 <name>Zucchini</name>
 <carbs_per_serving>1.5</carbs_per_serving>
 <fiber_per_serving>1.5</fiber_per_serving>
 <fat_per_serving>0.5</fat_per_serving>
 <kj_per_serving>55</kj_per_serving>
 </food_item>
 <food_item type="seafood">
 <name>Abalone</name>
 <carbs_per_serving>0</carbs_per_serving>
 <fiber_per_serving>0</fiber_per_serving>
 <fat_per_serving>1</fat_per_serving>
 <kj_per_serving>400</kj_per_serving>
 </food_item>
 <food_item type="seafood">
 <name>Barramundi</name>
 <carbs_per_serving>0</carbs_per_serving>
 <fiber_per_serving>0</fiber_per_serving>
 <fat_per_serving>2</fat_per_serving>
 <kj_per_serving>390</kj_per_serving>
 </food_item>
 <food_item type="fruit">
 <name>Apple</name>
 <carbs_per_serving>15</carbs_per_serving>
 <fiber_per_serving>2.5</fiber_per_serving>
 <fat_per_serving>0</fat_per_serving>
 <kj_per_serving>250</kj_per_serving>
 </food_item>
 <food_item type="fruit">
 <name>Kiwi Fruit</name>
 <carbs_per_serving>7.5</carbs_per_serving>
 <fiber_per_serving>2.5</fiber_per_serving>
 <fat_per_serving>0</fat_per_serving>
 <kj_per_serving>150</kj_per_serving>
 </food_item>
 <food_item type="grain">
 <name>Oatbran</name>
 <carbs_per_serving>62</carbs_per_serving>
 <fiber_per_serving>14</fiber_per_serving>
 <fat_per_serving>7</fat_per_serving>
 <kj_per_serving>1400</kj_per_serving>
 </food_item>
 <food_item type="grain">
 <name>Wheatgerm</name>
 <carbs_per_serving>1.5</carbs_per_serving>
 <fiber_per_serving>1</fiber_per_serving>
 <fat_per_serving>0.5</fat_per_serving>
 <kj_per_serving>70</kj_per_serving>
 </food_item>
</food_list></textarea> </div> <script> var exampleCode2 = CodeMirror.fromTextArea(document.getElementById("example2"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <h3>The Requirement</h3> <p>Now, imagine we want to display the contents of the file in a table, and that we want to have a <code>counter</code> column that provides the count of each row. Something like this:</p> <img src="/pix/xml/tutorial/xpath_node_set_1.gif" class="screenshot" alt="XPath node set example" /> <h3>The Solution</h3> <p>We could achieve the above requirement using the XPath <code>position()</code> function, as follows:</p> <div class="code-only"> <textarea id="example3" autocomplete="off" spellcheck="false"><?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
 <xsl:apply-templates/>
</xsl:template>

<xsl:template match="food_list">
 <table border="1" style="background-color:#cccc00">
 <tr>
 <th>Node Position</th>
 <th>Food Item</th>
 <th>Carbs (g)</th>
 <th>Fiber (g)</th>
 <th>Fat (g)</th>
 <th>Energy (kj)</th>
 </tr>
 <xsl:for-each select="food_item">
 <tr>
 <td><b><xsl:value-of select="position()"/></b></td>
 <td><xsl:value-of select="name"/></td>
 <td><xsl:value-of select="carbs_per_serving"/></td>
 <td><xsl:value-of select="fiber_per_serving"/></td>
 <td><xsl:value-of select="fat_per_serving"/></td>
 <td><xsl:value-of select="kj_per_serving"/></td>
 </tr>
 </xsl:for-each>
 </table>
</xsl:template>

</xsl:stylesheet></textarea> </div> <script> var exampleCode3 = CodeMirror.fromTextArea(document.getElementById("example3"), { mode: "htmlmixed", tabMode: "indent", styleActiveLine: false, lineNumbers: false, lineWrapping: true, theme: "q-dark" }); </script> <ul class="pager"> <li><a href="/xml/tutorial/xpath_predicate.cfm"><i class="fa fa-long-arrow-left" aria-hidden="true"></i> XPath Predicate</a></li> <li><a href="/xml/tutorial/xpath_comparison_operators.cfm">Comparison Operators <i class="fa fa-long-arrow-right" aria-hidden="true"></i></a></li> </ul> </article> <div class="sidebar"> <nav> <ul> <li> <h3><a href="/xml/tutorial/" title="">XML Tutorial</a></h3> <ul> <li><a href="/xml/tutorial/about_xml.cfm" title="">About XML</a></li> <li><a href="/xml/tutorial/xml_viewers.cfm" title="">XML Viewers</a></li> <li><a href="/xml/tutorial/xml_editors.cfm" title="">XML Editors</a></li> <li><a href="/xml/tutorial/xml_documents.cfm" title="">XML Documents</a></li> <li><a href="/xml/tutorial/xml_syntax.cfm" title="">XML Syntax</a></li> <li><a href="/xml/tutorial/xml_elements.cfm" title="">XML Elements</a></li> <li><a href="/xml/tutorial/xml_attributes.cfm" title="">XML Attributes</a></li> <li><a href="/xml/tutorial/xml_css.cfm" title="">XML with CSS</a></li> <li><a href="/xml/tutorial/xml_xsl.cfm" title="">XML with XSL</a></li> <li><a href="/xml/tutorial/xml_namespace.cfm" title="">XML Namespace</a></li> <li><a href="/xml/tutorial/xml_local_namespace.cfm" title="">XML Local Namespace</a></li> <li><a href="/xml/tutorial/xml_default_namespace.cfm" title="">XML Default Namespace</a></li> <li><a href="/xml/tutorial/xml_entities.cfm" title="">XML Entities</a></li> <li><a href="/xml/tutorial/xml_creating_entities.cfm" title="">XML Creating Entities</a></li> <li><a href="/xml/tutorial/xml_cdata.cfm" title="">XML CDATA</a></li> </ul> </li> <li> <h3 class="heading-only">DTD</h3> <ul> <li><a href="/xml/tutorial/dtd_introduction.cfm" title="">DTD Introduction</a></li> <li><a href="/xml/tutorial/dtd_doctype.cfm" title="">DTD DOCTYPE</a></li> <li><a href="/xml/tutorial/dtd_internal_dtd.cfm" title="">DTD Internal</a></li> <li><a href="/xml/tutorial/dtd_external_dtd.cfm" title="">DTD External</a></li> <li><a href="/xml/tutorial/dtd_combined_dtd.cfm" title="">DTD Combined</a></li> <li><a href="/xml/tutorial/dtd_fpi.cfm" title="">DTD FPI</a></li> <li><a href="/xml/tutorial/dtd_elements.cfm" title="">DTD Elements</a></li> <li><a href="/xml/tutorial/dtd_element_operators.cfm" title="">DTD Element Operators</a></li> <li><a href="/xml/tutorial/dtd_attributes.cfm" title="">DTD Attributes</a></li> <li><a href="/xml/tutorial/dtd_attribute_default_values.cfm" title="">DTD Attribute Default Values</a></li> <li><a href="/xml/tutorial/dtd_attribute_types.cfm" title="">DTD Attribute Types</a></li> <li><a href="/xml/tutorial/dtd_general_entities.cfm" title="">DTD General Entities</a></li> <li><a href="/xml/tutorial/dtd_parameter_entities.cfm" title="">DTD Parameter Entities</a></li> <li><a href="/xml/tutorial/dtd_embedded_images.cfm" title="">DTD Embedded Images</a></li> </ul> </li> <li> <h3 class="heading-only">XSLT</h3> <ul> <li><a href="/xml/tutorial/xslt_introduction.cfm" title="">XSLT Introduction</a></li> <li><a href="/xml/tutorial/xslt_example.cfm" title="">XSLT Example</a></li> <li><a href="/xml/tutorial/xslt_syntax.cfm" title="">XSLT Syntax</a></li> <li><a href="/xml/tutorial/xslt_template.cfm" title="">XSLT <template></a></li> <li><a href="/xml/tutorial/xslt_apply-templates.cfm" title="">XSLT <apply-templates></a></li> <li><a href="/xml/tutorial/xslt_value-of.cfm" title="">XSLT <value-of></a></li> <li><a href="/xml/tutorial/xslt_for-each.cfm" title="">XSLT <for-each></a></li> <li><a href="/xml/tutorial/xslt_sort.cfm" title="">XSLT <sort></a></li> <li><a href="/xml/tutorial/xslt_if.cfm" title="">XSLT <if></a></li> <li><a href="/xml/tutorial/xslt_choose.cfm" title="">XSLT <choose></a></li> </ul> </li> <li> <h3 class="heading-only">XPath</h3> <ul> <li><a href="/xml/tutorial/xpath_introduction.cfm" title="">XPath Introduction</a></li> <li><a href="/xml/tutorial/xpath_location_path.cfm" title="">XPath Location Path</a></li> <li><a href="/xml/tutorial/xpath_location_path_absolute.cfm" title="">XPath Location Path - Absolute</a></li> <li><a href="/xml/tutorial/xpath_location_path_relative.cfm" title="">XPath Location Path - Relative</a></li> <li><a href="/xml/tutorial/xpath_attributes.cfm" title="">XPath Attributes</a></li> <li><a href="/xml/tutorial/xpath_axis.cfm" title="">XPath Axis</a></li> <li><a href="/xml/tutorial/xpath_node_test.cfm" title="">XPath Node Test</a></li> <li><a href="/xml/tutorial/xpath_predicate.cfm" title="">XPath Predicate</a></li> <li><a href="/xml/tutorial/xpath_node_set.cfm" title="">XPath Node Set</a></li> <li><a href="/xml/tutorial/xpath_comparison_operators.cfm" title="">XPath Comparison Operators</a></li> <li><a href="/xml/tutorial/xpath_boolean_operators.cfm" title="">XPath Boolean Operators</a></li> <li><a href="/xml/tutorial/xpath_number_operators.cfm" title="">XPath Number Operators</a></li> <li><a href="/xml/tutorial/xpath_string_functions.cfm" title="">XPath String Functions</a></li> <li><a href="/xml/tutorial/xml_tutorial_summary.cfm" title="">Tutorial Summary</a></li> </ul> </li> </ul> </nav> <div class="ad ad-left"> <!-- GAM 71161633/QCKIT_quackit/article_vrec_2 --> <div data-fuse="23059511712"></div> </div> </div> <div class="ads"> <div class="ad ad-right"> <!-- GAM 71161633/QCKIT_quackit/article_vrec_1 --> <div data-fuse="23059883626"></div> </div> </div> </div> <div class="searchbox-bottom"> <form action="/search/" id="cse-search-box-bottom" class="site-search"> <div> <input type="hidden" name="cx" value="partner-pub-6331358926293806:npmuvy-i8kk"> <input type="hidden" name="cof" value="FORID:10"> <input type="hidden" name="ie" value="ISO-8859-1"> <input type="text" name="q" size="30" class="site-search-input"> <button type="submit" name="sa" class="site-search-button"><i class="fa fa-search"></i></button> </div> </form> <script src="//cse.google.com/cse/brand?form=cse-search-box-bottom&lang=en"></script> </div> <footer> <p class="about"><a href="/"><i class="fa fa-home"></i> Home</a> | <a href="/about.cfm" rel="nofollow">About</a> | <a href="/contact.cfm" rel="nofollow">Contact</a> | <a href="/terms_of_use.cfm" rel="nofollow">Terms of Use</a> | <a href="/privacy_policy.cfm" rel="nofollow">Privacy Policy</a></p> <p>© Copyright 2000 - 2025 Quackit.com </p> </footer> <script src="/common/js/spectrum/spectrum.js"></script> <script src="/common/js/lightbox2-master/dist/js/lightbox.min.js" charset="utf-8"></script> <script> $(document).ready(function(){ $( "#site-nav-toggler" ).click(function() { $( "#site-nav" ).toggle( "slow" ); }); }); </script> <script> $(function(){var a=window.location.href;$(".sidebar nav a").each(function(){a==this.href&&$(this).closest("li").addClass("selected")})}); </script> </body> </html>