{"id":22615,"date":"2015-04-16T15:00:08","date_gmt":"2015-04-16T12:00:08","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=22615"},"modified":"2015-04-15T01:18:29","modified_gmt":"2015-04-14T22:18:29","slug":"xpath-ancestor-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/","title":{"rendered":"XPath ancestor example"},"content":{"rendered":"<p>In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and how to use it, then we will see the working model of the desired\u00a0functionality.<\/p>\n<p>Tools and technologies used in this example are Java 1.6, Maven, Intellij.(Please note you don&#8217;t need Maven to run this example. It can be run as standalone java application. You can use any IDE or even not use any if you want to.)<\/p>\n<h2>1. Overview<\/h2>\n<p>An <strong>XPath<\/strong> expression specifies a pattern that selects a set of XML nodes. XSLT templates then use those patterns when applying transformations.\u00a0The nodes in an XPath expression refer to more than just elements. They also refer to text and attributes, among other things. In fact, the XPath specification defines an abstract document model that defines seven kinds of nodes:<\/p>\n<ul>\n<li>Root<\/li>\n<li>Element<\/li>\n<li>Text<\/li>\n<li>Attribute<\/li>\n<li>Comment<\/li>\n<li>Processing instruction<\/li>\n<li>Namespace<\/li>\n<\/ul>\n<p>XPath also provides rules for converting a node in an XML document object model (DOM) tree to a boolean, double, or string value.\u00a0XPath is a W3C-defined language and an official W3C recommendation; the W3C hosts the XML Path Language (XPath) Version 1.0 specification.<\/p>\n<h2>2. XPath Expressions<\/h2>\n<p>An XPath\u00a0<em>expression<\/em>\u00a0is composed of a\u00a0<em>location path<\/em>\u00a0and one or more optional\u00a0<em>predicates<\/em>. Expressions may also include XPath variables.<\/p>\n<p>The following is an example of a simple XPath expression:<\/p>\n<p><code>\/Country\/City<\/code><\/p>\n<p>This example would select the\u00a0<code>&lt;City&gt;<\/code>\u00a0element in an XML document.\u00a0A location path can be absolute or relative.\u00a0An absolute location path starts with a slash ( \/ ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:<\/p>\n<p>An axis defines a node-set relative to the current node.<\/p>\n<table class=\"reference notranslate\">\n<tbody>\n<tr>\n<th>AxisName<\/th>\n<th>Result<\/th>\n<\/tr>\n<tr>\n<td>ancestor<\/td>\n<td>Selects all ancestors (parent, grandparent, etc.) of the current node<\/td>\n<\/tr>\n<tr>\n<td>ancestor-or-self<\/td>\n<td>Selects all ancestors (parent, grandparent, etc.) of the current node and the current node itself<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<h2>3. Project Structure<\/h2>\n<p>Below is the project structure used in this example<\/p>\n<p><figure id=\"attachment_22616\" aria-describedby=\"caption-attachment-22616\" style=\"width: 352px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure.jpg\"><img decoding=\"async\" class=\"size-full wp-image-22616\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure.jpg\" alt=\"Figure 1. Project Structure\" width=\"352\" height=\"350\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure.jpg 352w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure-150x150.jpg 150w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure-300x298.jpg 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/Project-Structure-70x70.jpg 70w\" sizes=\"(max-width: 352px) 100vw, 352px\" \/><\/a><figcaption id=\"caption-attachment-22616\" class=\"wp-caption-text\">Figure 1. Project Structure<\/figcaption><\/figure><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2>4. XLM file<\/h2>\n<p>Below is the xml file representing Users. Each user has got a unique UserId, name, address, sex, Date Of Birth and email.<\/p>\n<p><span style=\"text-decoration: underline\"><em>user.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;Users&gt;\r\n    &lt;User&gt;\r\n        &lt;UserId&gt;12345&lt;\/UserId&gt;\r\n        &lt;Name&gt;\r\n            &lt;FirstName&gt;Maden&lt;\/FirstName&gt;\r\n            &lt;MiddleName&gt;Over&lt;\/MiddleName&gt;\r\n            &lt;Surname&gt;Gulliver&lt;\/Surname&gt;\r\n        &lt;\/Name&gt;\r\n        &lt;Address&gt;\r\n            &lt;FirstLineOfAddress&gt;Oxford Street&lt;\/FirstLineOfAddress&gt;\r\n            &lt;SecondLineOfAddress&gt;&lt;\/SecondLineOfAddress&gt;\r\n            &lt;City&gt;London&lt;\/City&gt;\r\n            &lt;County&gt;Middlesex&lt;\/County&gt;\r\n            &lt;Country&gt;United Kingdom&lt;\/Country&gt;\r\n        &lt;\/Address&gt;\r\n        &lt;Sex&gt;Male&lt;\/Sex&gt;\r\n        &lt;DateOfBirth&gt;01\/02\/1967&lt;\/DateOfBirth&gt;\r\n        &lt;Email&gt;&lt;\/Email&gt;\r\n    &lt;\/User&gt;\r\n    &lt;User&gt;\r\n        &lt;UserId&gt;56789&lt;\/UserId&gt;\r\n        &lt;Name&gt;\r\n            &lt;FirstName&gt;Tom&lt;\/FirstName&gt;\r\n            &lt;MiddleName&gt;Cruise&lt;\/MiddleName&gt;\r\n            &lt;Surname&gt;Kidman&lt;\/Surname&gt;\r\n        &lt;\/Name&gt;\r\n        &lt;Address&gt;\r\n            &lt;FirstLineOfAddress&gt;Reagent Street&lt;\/FirstLineOfAddress&gt;\r\n            &lt;SecondLineOfAddress&gt;&lt;\/SecondLineOfAddress&gt;\r\n            &lt;City&gt;New York&lt;\/City&gt;\r\n            &lt;County&gt;&lt;\/County&gt;\r\n            &lt;Country&gt;United States&lt;\/Country&gt;\r\n        &lt;\/Address&gt;\r\n        &lt;Sex&gt;Female&lt;\/Sex&gt;\r\n        &lt;DateOfBirth&gt;02\/03\/1978&lt;\/DateOfBirth&gt;\r\n        &lt;Email&gt;&lt;\/Email&gt;\r\n    &lt;\/User&gt;\r\n    &lt;User&gt;\r\n        &lt;UserId&gt;72638&lt;\/UserId&gt;\r\n        &lt;Name&gt;\r\n            &lt;FirstName&gt;Amitabh&lt;\/FirstName&gt;\r\n            &lt;MiddleName&gt;&lt;\/MiddleName&gt;\r\n            &lt;Surname&gt;Bachchan&lt;\/Surname&gt;\r\n        &lt;\/Name&gt;\r\n        &lt;Address&gt;\r\n            &lt;FirstLineOfAddress&gt;Panama Street&lt;\/FirstLineOfAddress&gt;\r\n            &lt;SecondLineOfAddress&gt;&lt;\/SecondLineOfAddress&gt;\r\n            &lt;City&gt;Mumbai&lt;\/City&gt;\r\n            &lt;County&gt;&lt;\/County&gt;\r\n            &lt;Country&gt;India&lt;\/Country&gt;\r\n        &lt;\/Address&gt;\r\n        &lt;Sex&gt;Male&lt;\/Sex&gt;\r\n        &lt;DateOfBirth&gt;05\/04\/1999&lt;\/DateOfBirth&gt;\r\n        &lt;Email&gt;amitabh.bachchan@asv.com&lt;\/Email&gt;\r\n    &lt;\/User&gt;\r\n&lt;\/Users&gt;<\/pre>\n<h2>5. Java file<\/h2>\n<p>Below is the java file which will use XPath to extract information from the <code>User.xml<\/code>.<\/p>\n<p><span style=\"text-decoration: underline\"><em>ExampleXPathAncestor.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\r\n\r\nimport org.w3c.dom.Document;\r\nimport org.w3c.dom.NodeList;\r\nimport org.xml.sax.SAXException;\r\n\r\nimport javax.xml.parsers.DocumentBuilder;\r\nimport javax.xml.parsers.DocumentBuilderFactory;\r\nimport javax.xml.parsers.ParserConfigurationException;\r\nimport javax.xml.xpath.XPath;\r\nimport javax.xml.xpath.XPathConstants;\r\nimport javax.xml.xpath.XPathExpressionException;\r\nimport javax.xml.xpath.XPathFactory;\r\nimport java.io.IOException;\r\n\r\n\/**\r\n * Example code for XPath ancestor selection\r\n * @author Java Code Geeks\r\n *\/\r\npublic class ExampleXPathAncestor {\r\n\r\n    private static DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();\r\n    private static DocumentBuilder builder = null;\r\n    private static XPath xPath = null;\r\n    private static Document document = null;\r\n\r\n    public static void main(String q[]) {\r\n        try {\r\n            builder = builderFactory.newDocumentBuilder();\r\n            document = builder.parse(ExampleXPathAncestor.class.getResourceAsStream(\"\/user.xml\"));\r\n        } catch (ParserConfigurationException e) {\r\n            e.printStackTrace();\r\n        } catch (SAXException e) {\r\n            e.printStackTrace();\r\n        } catch (IOException e) {\r\n            e.printStackTrace();\r\n        }\r\n        xPath =  XPathFactory.newInstance().newXPath();\r\n        String expression = \"\/\/SecondLineOfAddress\/ancestor::Address\";\r\n        try {\r\n            NodeList nl = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);\r\n            System.out.println(nl.getLength());\r\n            System.out.println(nl.item(0).getTextContent());\r\n            System.out.println(nl.item(1).getTextContent());\r\n            System.out.println(nl.item(3).getTextContent());\r\n        } catch (XPathExpressionException e) {\r\n            e.printStackTrace();\r\n        }\r\n\r\n    }\r\n\r\n}<\/pre>\n<p>The expression\u00a0<code>\/\/SecondLineOfAddress\/ancestor::Address<\/code> will select the ancestor <em>Address<\/em> of\u00a0<em>SecondLineOfAddress<\/em>.<\/p>\n<h2>6. Output<\/h2>\n<p>Run the\u00a0<em>ExampleXPathAncestor.java<\/em> file. Below is the generated output.<\/p>\n<pre class=\"brush:bash\">3\r\n\r\n    Oxford Street\r\n\r\n    London\r\n    Middlesex\r\n    United Kingdom\r\n\r\n    Reagent Street\r\n\r\n    New York\r\n\r\n    United States<\/pre>\n<h2>7. Download the source file<\/h2>\n<p>This was an example showing how to use XPath ansector.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here :\u00a0<a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/04\/XPath-ancestor.zip\">XPath-ancestor<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and how to use it, then we will see the working model of the desired\u00a0functionality. Tools and technologies used in this example are Java 1.6, &hellip;<\/p>\n","protected":false},"author":34,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-22615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xpath"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>XPath ancestor example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"XPath ancestor example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-04-16T12:00:08+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Mohammad Meraj Zia\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Mohammad Meraj Zia\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\"},\"author\":{\"name\":\"Mohammad Meraj Zia\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed\"},\"headline\":\"XPath ancestor example\",\"datePublished\":\"2015-04-16T12:00:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\"},\"wordCount\":442,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"articleSection\":[\"XPath\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\",\"name\":\"XPath ancestor example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2015-04-16T12:00:08+00:00\",\"description\":\"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"Bipartite Graph\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"xml\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"XPath\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/xpath\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"XPath ancestor example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed\",\"name\":\"Mohammad Meraj Zia\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg\",\"caption\":\"Mohammad Meraj Zia\"},\"description\":\"Senior Java Developer\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/mohammad-zia\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"XPath ancestor example - Java Code Geeks","description":"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and","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:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/","og_locale":"en_US","og_type":"article","og_title":"XPath ancestor example - Java Code Geeks","og_description":"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-04-16T12:00:08+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Mohammad Meraj Zia","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Mohammad Meraj Zia","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/"},"author":{"name":"Mohammad Meraj Zia","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed"},"headline":"XPath ancestor example","datePublished":"2015-04-16T12:00:08+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/"},"wordCount":442,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","articleSection":["XPath"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/","name":"XPath ancestor example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2015-04-16T12:00:08+00:00","description":"In this example we will learn how to get the ancestor details of a given node using XPath. We will start with the introduction of XPath \u2013 what it is and","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-ancestor-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"xml","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/"},{"@type":"ListItem","position":5,"name":"XPath","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/xml\/xpath\/"},{"@type":"ListItem","position":6,"name":"XPath ancestor example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed","name":"Mohammad Meraj Zia","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2020\/06\/IMG-20200324-WA0003-96x96.jpg","caption":"Mohammad Meraj Zia"},"description":"Senior Java Developer","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/mohammad-zia\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/22615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=22615"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/22615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=22615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=22615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=22615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}