{"id":23183,"date":"2013-05-13T17:01:19","date_gmt":"2013-05-13T11:31:19","guid":{"rendered":"http:\/\/vskills.in\/certification\/tutorial\/?p=23183"},"modified":"2024-04-12T14:23:50","modified_gmt":"2024-04-12T08:53:50","slug":"xpath-for-accessing-and-selecting-nodes","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/","title":{"rendered":"XPath for accessing and selecting nodes"},"content":{"rendered":"<h4>XPath<\/h4>\n<p>XPath is a non-XML language used to identify particular parts of XML documents. XPath lets you write expressions that refer to the document&#8217;s first<code>person<\/code>\u00a0element, the seventh child element of the third\u00a0<code>person<\/code>\u00a0element, the\u00a0<code>ID<\/code>\u00a0attribute of the first\u00a0<code>person<\/code>\u00a0element whose contents are the string &#8220;Fred Jones,&#8221; all\u00a0<code>xml-stylesheet<\/code>\u00a0processing instructions in the document&#8217;s prolog, and so forth. XPath indicates nodes by position, relative position, type, content, and several other criteria. XSLT uses XPath expressions to match and select particular elements in the input document for copying into the output document or further processing. XPointer uses XPath expressions to identify the particular point in or part of an XML document that an XLink links to.<\/p>\n<p>XPath expressions can also represent numbers, strings, or Booleans, so XSLT stylesheets carry out simple arithmetic for numbering and cross-referencing figures, tables, and equations. String manipulation in XPath lets XSLT perform tasks like making the title of a chapter uppercase in a headline, but mixed case in a reference in the body text.<\/p>\n<h2>XPath Functions<\/h2>\n<p>XPath provides many functions you may find useful in predicates or in raw expressions. All of these functions are discussed in\u00a0<em>Chapter 19,\u00a0<\/em><cite>XPath Reference<\/cite>. For example, the\u00a0<code>position( )<\/code>\u00a0function returns the current node&#8217;s position in the context node list as a number. This XSLT template uses the\u00a0<code>position( )<\/code>\u00a0function to calculate the number of the\u00a0<code>person<\/code>\u00a0being processed, relative to other nodes in the context node list:<\/p>\n<pre><code>&lt;xsl:template match=\"person\"&gt;<\/code> <code>  Person &lt;xsl:value-of select=\"position(  )\"\/&gt;,<\/code> <code>  &lt;xsl:value-of select=\"name\"\/&gt;<\/code> <code>&lt;\/xsl:template&gt;<\/code><\/pre>\n<p>Each XPath function returns one of these four types:<\/p>\n<ul>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">boolean<\/span><\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">number<\/span><\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">node set<\/span><\/li>\n<li><span style=\"line-height: 1.714285714; font-size: 1rem;\">string<\/span><\/li>\n<\/ul>\n<h1>XML Nodes<\/h1>\n<p>To find nodes in an XML file you can use\u00a0<strong>XPath<\/strong>\u00a0expressions. Method\u00a0<strong>XmlNode.Selec\u00adtNodes<\/strong>returns a\u00a0<strong>list of nodes<\/strong>\u00a0selected by the XPath string. Method\u00a0<strong>XmlNode.Selec\u00adtSingleNode<\/strong>\u00a0finds<strong>the first node<\/strong>\u00a0that matches the XPath string.<\/p>\n<p>Suppose we have this XML file.<\/p>\n[XML]\n<pre>&lt;Names&gt;\n    <b>&lt;Name&gt;<\/b>\n        &lt;FirstName&gt;John&lt;\/FirstName&gt;\n        &lt;LastName&gt;Smith&lt;\/LastName&gt;\n    <b>&lt;\/Name&gt;<\/b>\n    <b>&lt;Name&gt;<\/b>\n        &lt;FirstName&gt;James&lt;\/FirstName&gt;\n        &lt;LastName&gt;White&lt;\/LastName&gt;\n    <b>&lt;\/Name&gt;<\/b>\n&lt;\/Names&gt;<\/pre>\n<p>To get all &lt;Name&gt; nodes use\u00a0<strong>XPath expression<\/strong>\u00a0<code>\/Names\/Name<\/code>. The first slash means that the &lt;Names&gt; node must be a root node. SelectNodes method returns collection\u00a0XmlNodeListwhich will contain the &lt;Name&gt; nodes. To\u00a0<strong>get value<\/strong>\u00a0of sub node &lt;FirstName&gt; you can simply index\u00a0XmlNode\u00a0with the node name:\u00a0<code>xmlNode[\"FirstName\"].InnerText<\/code>. See the example\u00a0below.<\/p>\n[C#]\n<pre>XmlDocument xml = new XmlDocument();\nxml.LoadXml(myXmlString); \/\/ suppose that myXmlString contains \"&lt;Names&gt;...&lt;\/Names&gt;\"\n\nXmlNodeList xnList = xml.<strong>SelectNodes<\/strong>(\"\/Names\/Name\");\nforeach (XmlNode xn in xnList)\n{\n  string firstName = xn[\"FirstName\"].InnerText;\n  string lastName = xn[\"LastName\"].InnerText;\n  Console.WriteLine(\"Name: {0} {1}\", firstName, lastName);\n}<\/pre>\n<p>The output is:<\/p>\n<pre>Name: John Smith\nName: James White<\/pre>\n<div class=\"apply\">\n<h5>Go to- <a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/xml-developer\/\" target=\"_blank\" rel=\"noopener noreferrer\"><strong>Certified XML Developer Tutorial<\/strong><\/a><\/h5>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>XPath XPath is a non-XML language used to identify particular parts of XML documents. XPath lets you write expressions that refer to the document&#8217;s firstperson\u00a0element, the seventh child element of the third\u00a0person\u00a0element, the\u00a0ID\u00a0attribute of the first\u00a0person\u00a0element whose contents are the string &#8220;Fred Jones,&#8221; all\u00a0xml-stylesheet\u00a0processing instructions in the document&#8217;s prolog, and so forth. XPath indicates nodes&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[3353],"tags":[4553,4554,4555],"class_list":["post-23183","page","type-page","status-publish","hentry","category-xml","tag-xpath","tag-xpath-accessing","tag-xpath-selecting-nodes"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>XPath for accessing and selecting nodes - Tutorial<\/title>\n<meta name=\"description\" content=\"XPath for accessing and selecting nodes\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"XPath for accessing and selecting nodes - Tutorial\" \/>\n<meta property=\"og:description\" content=\"XPath for accessing and selecting nodes\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:53:50+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/\",\"name\":\"XPath for accessing and selecting nodes - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2013-05-13T11:31:19+00:00\",\"dateModified\":\"2024-04-12T08:53:50+00:00\",\"description\":\"XPath for accessing and selecting nodes\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"XPath for accessing and selecting nodes\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"XPath for accessing and selecting nodes - Tutorial","description":"XPath for accessing and selecting nodes","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/","og_locale":"en_US","og_type":"article","og_title":"XPath for accessing and selecting nodes - Tutorial","og_description":"XPath for accessing and selecting nodes","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:53:50+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/","name":"XPath for accessing and selecting nodes - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2013-05-13T11:31:19+00:00","dateModified":"2024-04-12T08:53:50+00:00","description":"XPath for accessing and selecting nodes","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/xpath-for-accessing-and-selecting-nodes\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"XPath for accessing and selecting nodes"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/23183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=23183"}],"version-history":[{"count":5,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/23183\/revisions"}],"predecessor-version":[{"id":134017,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/23183\/revisions\/134017"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=23183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=23183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=23183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}