{"id":44446,"date":"2017-04-11T11:00:13","date_gmt":"2017-04-11T08:00:13","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=44446"},"modified":"2017-04-09T11:44:48","modified_gmt":"2017-04-09T08:44:48","slug":"xpath-xslt-tutorial","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/","title":{"rendered":"XPath XSLT Tutorial"},"content":{"rendered":"<p>In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses &#8216;path like&#8217; syntax to identify and navigate nodes in an XML document.<\/p>\n<h2>1. Introduction<\/h2>\n<p>XPath can be used to navigate through elements and attributes in an XML document. XPath contains over 200 built-in functions.\u00a0There are functions for string values, numeric values, booleans, date and time comparison, node manipulation, sequence manipulation, and much more.\u00a0XPath expressions can also be used in JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages.<\/p>\n<h2>2. Terminologies<\/h2>\n<p>In this section we will learn about the various terminologies used in XPath.<\/p>\n<h3>2.1 Node<\/h3>\n<p>In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.\u00a0XML documents are treated as trees of nodes. The topmost element of the tree is called the root element. Look at the following XML document:<\/p>\n<p><em><span style=\"text-decoration: underline;\">persons.xml<\/span><\/em><\/p>\n<pre class=\"&quot;brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;persons&gt;\r\n  &lt;person&gt;\r\n    &lt;name lang=\"en\"&gt;\r\n      &lt;firstName&gt;Steve&lt;\/firstName&gt;\r\n      &lt;surname&gt;Jones&lt;\/surname&gt;\r\n    &lt;\/name&gt;\r\n    &lt;address&gt;\r\n      &lt;firstLine&gt;33 Churchill Road&lt;\/firstLine&gt;\r\n      &lt;secondLine&gt;Washington&lt;\/secondLine&gt;\r\n      &lt;city&gt;Washington DC&lt;\/city&gt;\r\n    &lt;\/address&gt;\r\n    &lt;age&gt;45&lt;age&gt;\r\n  &lt;\/person&gt;\r\n&lt;\/persons&gt;<\/pre>\n<p>In the above xml person, name, firstName etc are all nodes. &#8216;persons&#8217; is the root node. Each node has a parent node except the root node.\u00a0Element nodes may have zero, one or more children.\u00a0Nodes that have the same parent are called Siblings. An ancestor is a\u00a0node&#8217;s parent, parent&#8217;s parent, etc.<\/p>\n<h3>2.2 Attribute<\/h3>\n<p>Attribute is assigned to the node. In the above example &#8216;lang&#8217; in an attribute of &#8216;name&#8217; node.<\/p>\n<h3>2.3 XPath Expressions<\/h3>\n<p>In general, an XPath expression specifies a pattern that selects a set of XML nodes. XSLT templates then use those patterns when applying transformations. (XPointer, on the other hand, adds mechanisms for defining a point or a range so that XPath expressions can be used for addressing).\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>The root element of the XML data is modeled by an element node. The XPath root node contains the document&#8217;s root element as well as other information relating to the document.<\/p>\n<h3>2.4 XSLT\/XPath Data Model<\/h3>\n<p>Like the Document Object Model (DOM), the XSLT\/XPath data model consists of a tree containing a variety of nodes. Under any given element node, there are text nodes, attribute nodes, element nodes, comment nodes, and processing instruction nodes.<\/p>\n<p>In this abstract model, syntactic distinctions disappear, and you are left with a normalized view of the data. In a text node, for example, it makes no difference whether the text was defined in a CDATA section or whether it included entity references. The text node will consist of normalized data, as it exists after all parsing is complete. So the text will contain a &lt; character, whether or not an entity reference such as &lt; or a CDATA section was used to include it. (Similarly, the text will contain an &amp; character, whether it was delivered using &amp; or it was in a CDATA section).<\/p>\n<h2>3. XPath Nodes Selection<\/h2>\n<p>XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below:<\/p>\n<table style=\"height: 206px;\" width=\"1075\">\n<tbody>\n<tr>\n<td width=\"324\"><strong>Expression<\/strong><\/td>\n<td><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td><em>nodename<\/em><\/td>\n<td>Selects all nodes with the name &#8220;<em>nodename<\/em>&#8220;<\/td>\n<\/tr>\n<tr>\n<td>\/<\/td>\n<td>Selects from the root node<\/td>\n<\/tr>\n<tr>\n<td>\/\/<\/td>\n<td>Selects nodes in the document from the current node that match the selection no matter where they are<\/td>\n<\/tr>\n<tr>\n<td>.<\/td>\n<td>Selects the current node<\/td>\n<\/tr>\n<tr>\n<td>..<\/td>\n<td>Selects the parent of the current node<\/td>\n<\/tr>\n<tr>\n<td>@<\/td>\n<td>Selects attributes<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Below we show the result if we used these XPath expressions on out sample xml:<\/p>\n<table style=\"height: 275px;\" width=\"1192\">\n<tbody>\n<tr>\n<td width=\"324\"><strong>Path Expression<\/strong><\/td>\n<td><strong>Result<\/strong><\/td>\n<\/tr>\n<tr>\n<td>person<\/td>\n<td>Selects all nodes with the name &#8220;person&#8221;<\/td>\n<\/tr>\n<tr>\n<td>\/persons<\/td>\n<td>Selects the root element persons<\/p>\n<p><strong>Note:<\/strong>\u00a0If the path starts with a slash ( \/ ) it always represents an absolute path to an element!<\/td>\n<\/tr>\n<tr>\n<td>person\/name<\/td>\n<td>Selects all name elements that are children of person<\/td>\n<\/tr>\n<tr>\n<td>\/\/name<\/td>\n<td>Selects all name elements no matter where they are in the document<\/td>\n<\/tr>\n<tr>\n<td>person\/\/name<\/td>\n<td>Selects all name elements that are descendant of the person element, no matter where they are under the person element<\/td>\n<\/tr>\n<tr>\n<td>\/\/@lang<\/td>\n<td>Selects all attributes that are named lang<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>3.1 Predicates<\/h3>\n<p>Predicates are used to find a specific node or a node that contains a specific value.\u00a0Predicates are always embedded in square brackets.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><code>\/persons\/person[1]<\/code> =&gt; Selects the first person\u00a0element that is the child of the persons element.<\/p>\n<p><code>\/persons\/person[last()]<\/code> =&gt; Selects the last person element that is the child of the persons element.<\/p>\n<p><code>\/persons\/person[last()-1]<\/code> =&gt; Selects the last but one person element that is the child of the persons element.<\/p>\n<p><code>\/persons\/person[position()&lt;3]<\/code> =&gt; Selects the first two person elements that are children of the persons element.<\/p>\n<p><code>\/\/name[@lang]<\/code> =&gt; Selects all the name elements that have an attribute named lang.<\/p>\n<p><code>\/\/name[@lang='en']<\/code> =&gt; Selects all the name elements that have a &#8220;lang&#8221; attribute with a value of &#8220;en&#8221;.<\/p>\n<p><code>\/persons\/person[age&gt;40]<\/code> =&gt; Selects all the person elements of the persons element that have an age element with a value greater than 40.<\/p>\n<p><code>\/persons\/person[age&gt;40]\/name<\/code> =&gt; Selects all the name elements of the person elements of the persons element that have an age element with a value greater than 40.<\/p>\n<h3>3.2 Selecting Unknown nodes<\/h3>\n<p>XPath wildcards can be used to select unknown XML nodes.<\/p>\n<p><code>*<\/code> =&gt; Matches any element node<br \/>\n<code>@*<\/code> =&gt; Matches any attribute node<br \/>\n<code>node()<\/code> =&gt; Matches any node of any kind<\/p>\n<p>Below we will apply these on our sample xml<\/p>\n<p><code>\/persons\/*<\/code> =&gt; Selects all the child element nodes of the persons element<br \/>\n<code>\/\/*<\/code> =&gt; Selects all elements in the document<br \/>\n<code>\/\/name[@*]<\/code> =&gt; Selects all name elements which have at least one attribute of any kind<\/p>\n<p>By using the | operator in an XPath expression you can select several paths.<\/p>\n<h2>4. XSLT<\/h2>\n<p>XSLT stands for XSL (EXtensible Stylesheet Language) Transformations.\u00a0XSLT is a language for transforming XML documents.\u00a0XSLT is used to transform an XML document into another XML document, or another type of document that is recognized by a browser, like HTML and XHTML. Normally XSLT does this by transforming each XML element into an (X)HTML element.\u00a0With XSLT you can add\/remove elements and attributes to or from the output file. You can also rearrange and sort elements, perform tests and make decisions about which elements to hide and display, and a lot more.<\/p>\n<p><figure id=\"attachment_44470\" aria-describedby=\"caption-attachment-44470\" style=\"width: 673px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/XSLT.jpg\"><img decoding=\"async\" class=\"wp-image-44470 size-full\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/XSLT.jpg\" alt=\"\" width=\"673\" height=\"615\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/XSLT.jpg 673w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/XSLT-300x274.jpg 300w\" sizes=\"(max-width: 673px) 100vw, 673px\" \/><\/a><figcaption id=\"caption-attachment-44470\" class=\"wp-caption-text\">Figure 1. XSLT Transformation<\/figcaption><\/figure><\/p>\n<p>XSLT uses XPath to find information in an XML document. XPath is used to navigate through elements and attributes in XML documents.\u00a0In the transformation process, XSLT uses XPath to define parts of the source document that should match one or more predefined templates. When a match is found, XSLT will transform the matching part of the source document into the result document.<\/p>\n<p>The root element that declares the document to be an XSL style sheet is <code>&lt;xsl:stylesheet&gt;<\/code> or <code>&lt;xsl:transform&gt;<\/code>.\u00a0<code>&lt;xsl:stylesheet&gt;<\/code> and <code>&lt;xsl:transform&gt;<\/code> are completely synonymous and either can be used.\u00a0The correct way to declare an XSL style sheet according to the W3C XSLT Recommendation is:<\/p>\n<pre class=\"brush:xml\">&lt;xsl:stylesheet version=\"1.0\"\u00a0xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\"&gt;<\/pre>\n<p>or:<\/p>\n<pre class=\"brush:xml\">&lt;xsl:transform version=\"1.0\" xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\"&gt;<\/pre>\n<p>To get access to the XSLT elements, attributes and features we must declare the XSLT namespace at the top of the document.\u00a0The xmlns:xsl=&#8221;http:\/\/www.w3.org\/1999\/XSL\/Transform&#8221; points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version=&#8221;1.0&#8243;.<\/p>\n<h2>5. Convert XML to HTML<\/h2>\n<p>The output of an XSLT processing can be an HTML, XML (e.g. XHTML, SVG etc) or pure text. In this section we will see how we can convert an XML to an HTML using XSLT. We will use the persons.xml file for this. Create an XSL Style Sheet with a transformation template:<\/p>\n<p><em><span style=\"text-decoration: underline;\">persons.xsl<\/span><\/em><\/p>\n<pre class=\"brush:xml\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n\r\n&lt;xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http:\/\/www.w3.org\/1999\/XSL\/Transform\"&gt;\r\n\r\n  &lt;xsl:template match=\"\/\"&gt;\r\n    &lt;html&gt;\r\n      &lt;body&gt;\r\n        &lt;h2&gt;XSLT transformation example&lt;\/h2&gt;\r\n        &lt;table border=\"1\"&gt;\r\n          &lt;tr bgcolor=\"grey\"&gt;\r\n            &lt;th&gt;First Name&lt;\/th&gt;\r\n            &lt;th&gt;Surname&lt;\/th&gt;\r\n            &lt;th&gt;First line of Address&lt;\/th&gt;\r\n            &lt;th&gt;Second line of Address&lt;\/th&gt;\r\n            &lt;th&gt;City&lt;\/th&gt;\r\n            &lt;th&gt;Age&lt;\/th&gt;\r\n          &lt;\/tr&gt;\r\n          &lt;xsl:for-each select=\"persons\/person\"&gt;\r\n            &lt;tr&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"name\/firstName\"\/&gt;&lt;\/td&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"name\/surname\"\/&gt;&lt;\/td&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"address\/firstLine\"\/&gt;&lt;\/td&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"address\/secondLine\"\/&gt;&lt;\/td&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"address\/city\"\/&gt;&lt;\/td&gt;\r\n              &lt;td&gt;&lt;xsl:value-of select=\"age\"\/&gt;&lt;\/td&gt;\r\n            &lt;\/tr&gt;\r\n          &lt;\/xsl:for-each&gt;\r\n        &lt;\/table&gt;\r\n      &lt;\/body&gt;\r\n    &lt;\/html&gt;\r\n  &lt;\/xsl:template&gt;\r\n&lt;\/xsl:stylesheet&gt;\r\n<\/pre>\n<p>Add the XSL style sheet reference to your XML document.<\/p>\n<pre class=\"brush:xml\">&lt;?xml-stylesheet type=\"text\/xsl\" href=\"persons.xsl\"?&gt;<\/pre>\n<h3>5.1 XSL Template<\/h3>\n<p>An XSL style sheet consists of one or more set of rules that are called templates.\u00a0A template contains rules to apply when a specified node is matched. It\u00a0is a set of formatting instructions that apply to the nodes selected by an XPath expression. The <code>&lt;xsl:template&gt;<\/code> element is used to build templates.\u00a0The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match=&#8221;\/&#8221; defines the whole document).<\/p>\n<p>Since an XSL style sheet is an XML document, it always begins with the XML declaration: <code>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;<\/code>.\u00a0The next element, <code>&lt;xsl:stylesheet&gt;<\/code>, defines that this document is an XSLT style sheet document (along with the version number and XSLT namespace attributes).\u00a0The <code>&lt;xsl:template&gt;<\/code> element defines a template. The <code>match=\"\/\"<\/code> attribute associates the template with the root of the XML source document.\u00a0The content inside the <code>&lt;xsl:template&gt;<\/code> element defines some HTML to write to the output.\u00a0The last two lines define the end of the template and the end of the style sheet.<\/p>\n<p>The <code>&lt;xsl:value-of&gt;<\/code> element can be used to extract the value of an XML element and add it to the output stream of the transformation.<\/p>\n<h3>5.2 Transformation<\/h3>\n<p>In this section we will see how to do the transformation in Java. We will make use of two java packages:<\/p>\n<p><code>javax.xml.parsers<\/code> &#8211; It provides\u00a0classes allowing the processing of XML documents.\u00a0Two types of plugable parsers are supported: SAX (Simple API for XML) and\u00a0DOM (Document Object Model)<\/p>\n<p><code>javax.xml.transform<\/code> &#8211;\u00a0This package defines the generic APIs for processing transformation instructions, and performing a transformation from source to result.\u00a0These interfaces have no dependencies on SAX or the DOM standard, and try to make as few assumptions as possible about the details of the source and result of a transformation. It achieves this by defining <code>Source<\/code> and <code>Result<\/code> interfaces.\u00a0To define concrete classes for the user, the API defines specializations of the interfaces found at the root level. These interfaces are found in <code>javax.xml.transform.sax<\/code>, <code>javax.xml.transform.dom<\/code>, and <code>javax.xml.transform.stream<\/code>.\u00a0The API allows a concrete <code>TransformerFactory<\/code> object to be created from the static function <code>TransformerFactory.newInstance()<\/code>.<\/p>\n<p>First we will create the <code>DocumentBuilderFactory<\/code>:<\/p>\n<pre class=\"brush:java\">DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();<\/pre>\n<p>It defines a factory API that enables applications to obtain a parser that produces DOM object trees from XML documents. Then we will create a new <code>DocumentBuilder<\/code> using this factory:<\/p>\n<pre class=\"brush:java\">DocumentBuilder builder = factory.newDocumentBuilder();<\/pre>\n<p>This class defines the API to obtain DOM Document instances from an XML document.\u00a0Once an instance of this class is obtained, XML can be parsed from a variety of input sources.\u00a0These input sources are InputStreams, Files, URLs, and SAX InputSources.\u00a0Note that this class reuses several classes from the SAX API. This does not require that the implementor of the underlying DOM implementation use a SAX parser to parse XML document into a Document. It merely requires that the implementation communicate with the application using these existing APIs.<\/p>\n<p>Then we will parse the xml:<\/p>\n<pre class=\"brush:java\">document = builder.parse(xml);<\/pre>\n<p>This method parses the content of the given file as an XML document and return a new DOM Document object.<\/p>\n<p>Now we will create the transformer as below:<\/p>\n<pre class=\"brush:java\">TransformerFactory tFactory = TransformerFactory.newInstance();\r\nStreamSource stylesource = new StreamSource(xsl);\r\nTransformer transformer = tFactory.newTransformer(stylesource);\r\n<\/pre>\n<p>A <code>TransformerFactory<\/code> instance can be used to create <code>Transformer<\/code> and <code>Templates<\/code> objects.<\/p>\n<p>Now we can use this transformer instance to transform the xml source to the result. Below is the full class representation:<\/p>\n<p><em><span style=\"text-decoration: underline;\">XsltTransformation.java<\/span><\/em><\/p>\n<pre class=\"brush:java\">package com.javacodegeeks;\r\n\r\nimport org.w3c.dom.Document;\r\n\r\nimport javax.xml.parsers.DocumentBuilder;\r\nimport javax.xml.parsers.DocumentBuilderFactory;\r\nimport javax.xml.transform.Transformer;\r\nimport javax.xml.transform.TransformerFactory;\r\nimport javax.xml.transform.dom.DOMSource;\r\nimport javax.xml.transform.stream.StreamResult;\r\nimport javax.xml.transform.stream.StreamSource;\r\nimport java.io.File;\r\n\r\n\/**\r\n* Created by Meraj on 08\/04\/2017.\r\n*\/\r\npublic class XsltTrasfromation {\r\n\r\n  private static Document document;\r\n\r\n  public static void main(String[] args) throws Exception {\r\n    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();\r\n\r\n    File xml = new File(\"C:\\\\temp\\\\persons.xml\");\r\n    File xsl = new File(\"C:\\\\temp\\\\persons.xsl\");\r\n\r\n    DocumentBuilder builder = factory.newDocumentBuilder();\r\n    document = builder.parse(xml);\r\n\r\n    \/\/ Use a Transformer for output\r\n    TransformerFactory transformerFactory = TransformerFactory.newInstance();\r\n    StreamSource style = new StreamSource(xsl);\r\n    Transformer transformer = transformerFactory.newTransformer(style);\r\n\r\n    DOMSource source = new DOMSource(document);\r\n    StreamResult result = new StreamResult(System.out);\r\n    transformer.transform(source, result);\r\n  }\r\n}\r\n<\/pre>\n<p>If we run the above program the html will be outputted to the console. You can copy the html text in a file and save this file as *.html. If you open this file you will see something like:<\/p>\n<p><figure id=\"attachment_44472\" aria-describedby=\"caption-attachment-44472\" style=\"width: 617px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/output.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44472\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/output.jpg\" alt=\"\" width=\"617\" height=\"369\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/output.jpg 617w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/04\/output-300x179.jpg 300w\" sizes=\"(max-width: 617px) 100vw, 617px\" \/><\/a><figcaption id=\"caption-attachment-44472\" class=\"wp-caption-text\">Figure 2. Output<\/figcaption><\/figure><\/p>\n<h2>6. Conclusion<\/h2>\n<p>In this article\u00a0we learned about XPath and XSLT. We saw how XSLT works and how it uses XPath to do the processing. We also discussed various terminologies used in XPath and XSLT and what they corresponds to in an XML document. We also showed the example of how to convert a given XML to another format (or another XML) using XSLT. In the end we discussed how to do the transformation with Java. XSLT is a very useful feature in any project as it allows you, adapt to changes very quickly and efficiently.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses &#8216;path like&#8217; syntax to identify and navigate nodes in an XML document. 1. Introduction XPath can be used to navigate through elements and attributes in an XML document. XPath contains over 200 &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":[256,1656],"class_list":["post-44446","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xpath","tag-xpath-2","tag-xslt"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>XPath XSLT Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses &#039;path like&#039; syntax to\" \/>\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-xslt-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"XPath XSLT Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses &#039;path like&#039; syntax to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\" \/>\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=\"2017-04-11T08:00:13+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=\"13 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-xslt-tutorial\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\"},\"author\":{\"name\":\"Mohammad Meraj Zia\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed\"},\"headline\":\"XPath XSLT Tutorial\",\"datePublished\":\"2017-04-11T08:00:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\"},\"wordCount\":1947,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"xpath\",\"xslt\"],\"articleSection\":[\"XPath\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\",\"name\":\"XPath XSLT Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2017-04-11T08:00:13+00:00\",\"description\":\"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses 'path like' syntax to\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#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-xslt-tutorial\/#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 XSLT Tutorial\"}]},{\"@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 XSLT Tutorial - Java Code Geeks","description":"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses 'path like' syntax to","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-xslt-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"XPath XSLT Tutorial - Java Code Geeks","og_description":"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses 'path like' syntax to","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-04-11T08:00:13+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":"13 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/"},"author":{"name":"Mohammad Meraj Zia","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/442b4f9b8a4aa7e12376464fc354f8ed"},"headline":"XPath XSLT Tutorial","datePublished":"2017-04-11T08:00:13+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/"},"wordCount":1947,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["xpath","xslt"],"articleSection":["XPath"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/","name":"XPath XSLT Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2017-04-11T08:00:13+00:00","description":"In this article we will learn\u00a0how to use XPath in XSLT. XPath stands for XML Path Language. It is a W3C recommendation. It\u00a0uses 'path like' syntax to","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/xml\/xpath\/xpath-xslt-tutorial\/#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-xslt-tutorial\/#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 XSLT Tutorial"}]},{"@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\/44446","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=44446"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44446\/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=44446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}