<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=XPath_Primer</id>
	<title>XPath Primer - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=XPath_Primer"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=XPath_Primer&amp;action=history"/>
	<updated>2026-06-02T22:24:10Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=XPath_Primer&amp;diff=61540&amp;oldid=prev</id>
		<title>Ksolis at 02:32, 4 October 2017</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=XPath_Primer&amp;diff=61540&amp;oldid=prev"/>
		<updated>2017-10-04T02:32:14Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:course_code_xmlfund]]&lt;br /&gt;
[[Category:XML]]&lt;br /&gt;
[[Category:XPath]]&lt;br /&gt;
[[File:Nobleprog.svg]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;np_frontpage_footer&amp;quot;&amp;gt; &amp;lt;p&amp;gt;Copyright © 2015 NobleProg™. All Rights Reserved.  www.nobleprog.us&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{PB}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--__NOTOC__--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
{{PB}}&lt;br /&gt;
&lt;br /&gt;
=Introduction=&lt;br /&gt;
*XPath is used to navigate through elements and attributes in an XML document&lt;br /&gt;
*HTML is roughly a subset of XML&lt;br /&gt;
*XPath can select elements or element sets in an HTML document&lt;br /&gt;
::The expressions are similar to specifying a path in a file system&lt;br /&gt;
::Generally the purpose of XPath is to allow the user to select a single element or a set of elements&lt;br /&gt;
&lt;br /&gt;
=Terminology=&lt;br /&gt;
*See reference [http://www.w3schools.com/xsl/xpath_nodes.asp XPath Terminology]&lt;br /&gt;
::Nodes - nodes are in parent child relationships (a tree of nodes)&lt;br /&gt;
::Elements - &amp;lt;a href=&amp;quot;xxx&amp;quot;&amp;gt;Click this Link&amp;lt;/a&amp;gt; is an element node&lt;br /&gt;
::Attributes - value=&amp;quot;Bike&amp;quot; and href=&amp;quot;xxx&amp;quot; are attribute nodes&lt;br /&gt;
::Text&lt;br /&gt;
::Namespace&lt;br /&gt;
::Processing-Instruction&lt;br /&gt;
::Comment &amp;lt;!-- this is a comment --&amp;gt;&lt;br /&gt;
::Document&lt;br /&gt;
&lt;br /&gt;
=Node Selection=&lt;br /&gt;
*Useful path expressions&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
nodename - selects all nodes with the name &amp;quot;nodename&amp;quot;&lt;br /&gt;
/        - selects from the root node&lt;br /&gt;
//       - selects nodes from the current node that match the selection&lt;br /&gt;
.        - selects the current node&lt;br /&gt;
..       - selects the parent of the current node&lt;br /&gt;
@        - selects attributes&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*XPath contains a set of Node Tests which when satisfied chooses the node&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
comment()	          - Selects nodes that are comments.&lt;br /&gt;
node()                    - Selects nodes of any type.&lt;br /&gt;
processing-instruction()  - Selects nodes that are processing instructions. You can specify &lt;br /&gt;
                            which processing instruction to select by providing it&amp;#039;s name in&lt;br /&gt;
                            the parentheses.  These are generally in xml documents&lt;br /&gt;
text()                    - Selects a text node.&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=Predicates=&lt;br /&gt;
:::Used to find a specific node once a path expression is specified&lt;br /&gt;
:::Predicates are placed within braces []&lt;br /&gt;
:::Positional Predicates have numbers, last() or position() within the braces []&lt;br /&gt;
&lt;br /&gt;
:::Examples of node selection with predicates&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
//input[@value=&amp;#039;Bike&amp;#039;]                           - get the input element with an attribute of Bike&lt;br /&gt;
//a[@href=&amp;quot;www.google.com&amp;quot;]                      - the &amp;quot;a&amp;quot; element (the link) with an href attribute of www.google.com&lt;br /&gt;
//img[@src=&amp;quot;images/carved bowl.jpg&amp;quot;]             - get the img element with a src attribute of images/carved bowl.jpg&lt;br /&gt;
//div[@style=&amp;quot;color:red&amp;quot;]/text()                 - get the text within the div tag that has attribute style=&amp;quot;color:red&amp;quot;&lt;br /&gt;
//div[@style=&amp;quot;color:red&amp;quot; and @height=&amp;quot;80&amp;quot;]/text()- get the text within the div tag for the two attributes specified&lt;br /&gt;
//img[1]                                         - get the first img tag on the page&lt;br /&gt;
//img[last()]                                    - get the last img tag on the page&lt;br /&gt;
//img[position()&amp;lt;3]                              - get the first two img tags on the page&lt;br /&gt;
//ul[@class=&amp;quot;headline-list__list js-split-list js-add-p1&amp;quot;]/li[@class=&amp;quot;headline-list__item&amp;quot;][2]/a&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XPath Paths=&lt;br /&gt;
*Path elements are separated by a slash /&lt;br /&gt;
&lt;br /&gt;
://body/div[@style=&amp;#039;color:red&amp;#039;] - from the root find body with a div where style=&amp;#039;color:red&amp;#039;&lt;br /&gt;
://body/input - find all input elements that are children of the body element&lt;br /&gt;
&lt;br /&gt;
=Wildcards=&lt;br /&gt;
*Wildcards allows several elements to be chosen at the same time&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
*       - matches and element node&lt;br /&gt;
@*      - matches any attribute node&lt;br /&gt;
node()  - matches any node of any kind&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
:Examples of wildcard use&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
//*[@height=&amp;quot;80&amp;quot;]                   - all nodes that have a height attribute set to 80&lt;br /&gt;
//*[@src=&amp;quot;images/carved bowl.jpg&amp;quot;]  - any node that has a src attribute of images/carved bowl.jpg&lt;br /&gt;
//div[@*]                           - any div tag that has an attribute of any kind&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=Selecting Several Paths=&lt;br /&gt;
*To select several nodes with varying paths&lt;br /&gt;
::Use the | operator between the path expressions&lt;br /&gt;
:::For example /book/title | //book/price&lt;br /&gt;
&lt;br /&gt;
=XPath Axes=&lt;br /&gt;
&lt;br /&gt;
*An axes defines a set of nodes with respect to the present node&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
ancestor            - selects all ancestors of the current node&lt;br /&gt;
ancestor-or-self    - selects ancestors and the current node itself&lt;br /&gt;
attribute           - selects all attributes of the current node&lt;br /&gt;
child               - selects all child nodes of the current node&lt;br /&gt;
descendant          - selects all descendants of the current node children, grandchildren, etc.&lt;br /&gt;
descendant-or-self  - selects all descendants and the node itself&lt;br /&gt;
following           - selects everything in the document after the closing tag of the current node&lt;br /&gt;
following-sibling   - selects all following siblings of the current node&lt;br /&gt;
namespace           - selects all namespace nodes of the current node&lt;br /&gt;
parent              - selects the parent of the current node&lt;br /&gt;
preceding           - selects the nodes in the document before the current node, excluding ancestors, &lt;br /&gt;
                      attributes, and namespaces&lt;br /&gt;
preceding-sibling   - selects all siblings before the current node&lt;br /&gt;
self                - selects the current node&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
*Examples of Axes Use&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
//div[@style=&amp;quot;color:red&amp;quot; and @height=&amp;quot;80&amp;quot;]/attribute::style - select the style attribute of the div tag&lt;br /&gt;
//img/attribute::src  - select the src attribute of all the image tags&lt;br /&gt;
//img[last()]/following::a - find the &amp;quot;a&amp;quot; tag (the link) that follows the last image tag in the page&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XPath Functions=&lt;br /&gt;
*XPath has a significant number of functions that are available.&lt;br /&gt;
::See reference [http://www.w3schools.com/xpath/xpath_functions.asp XPath Functions]&lt;br /&gt;
&lt;br /&gt;
*Examples of Functions use&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:lightgrey&amp;quot;&amp;gt;&lt;br /&gt;
//div[contains(text(),&amp;#039;Some&amp;#039;)]     - find a div tag where the text starts with &amp;#039;Some&amp;#039;&lt;br /&gt;
//input[starts-with(@id, &amp;#039;text-&amp;#039;)] - find an input tag where the id starts with &amp;#039;text-&amp;#039;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=XPath Hands on Exercise=&lt;br /&gt;
:&amp;lt;big&amp;gt;&amp;lt;span style=&amp;quot;color:#FF0000&amp;quot;&amp;gt;Hands on Exercise&amp;lt;/span&amp;gt;&amp;lt;/big&amp;gt;&lt;br /&gt;
::In this exercise use the XPath tester at [http://www.freeformatter.com/xpath-tester.html XPath Tester]&lt;br /&gt;
::Go to the XPath Tester and put the following HTML code into the tester&lt;br /&gt;
:::Note that his code has been corrected slightly so that it is valid XML&lt;br /&gt;
:::For example the meta tag on line 3 has had a slash added before the final &amp;gt;&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color:blue;background-color:khaki&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
&amp;lt;head&amp;gt;&lt;br /&gt;
&amp;lt;meta http-equiv=&amp;quot;Content-Type&amp;quot; content=&amp;quot;text/html; charset=ISO-8859-1&amp;quot;/&amp;gt;&lt;br /&gt;
&amp;lt;title&amp;gt;Text Image and Video Demo&amp;lt;/title&amp;gt;&lt;br /&gt;
&amp;lt;/head&amp;gt;&lt;br /&gt;
&amp;lt;body&amp;gt;&lt;br /&gt;
&amp;lt;div style=&amp;quot;color:red&amp;quot;&amp;gt; this is it&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;div&amp;gt;This is the divs tag&amp;#039;s contents&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;a href=&amp;quot;seleniumTest.jsp&amp;quot;&amp;gt;back to main page&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
North America is a continent wholly within the Northern Hemisphere and almost wholly within &lt;br /&gt;
the Western Hemisphere. It can also be considered a northern subcontinent of the Americas.[1] &lt;br /&gt;
It is bordered to the north by the Arctic Ocean, to the east by the Atlantic Ocean, to the west &lt;br /&gt;
and south by the Pacific Ocean, and to the southeast by South America and the Caribbean Sea.&lt;br /&gt;
&lt;br /&gt;
North America covers an area of about 24,709,000 square kilometers (9,540,000 square miles), &lt;br /&gt;
about 4.8% of the planet&amp;#039;s surface or about 16.5% of its land area. As of 2013, its population &lt;br /&gt;
was estimated at nearly 565 million people across 23 independent states, representing about 7.5% &lt;br /&gt;
of the human population. Most of the continent&amp;#039;s land area is dominated by Canada, the United States, &lt;br /&gt;
Greenland, and Mexico, while smaller states exist in the Central American and Caribbean regions. &lt;br /&gt;
North America is the third largest continent by area, following Asia and Africa,[2] and the fourth &lt;br /&gt;
by population after Asia, Africa, and Europe.[3]&lt;br /&gt;
&lt;br /&gt;
The first people to live in North America were Paleoindians who began to arrive during the last &lt;br /&gt;
glacial period by crossing the Bering land bridge. They differentiated into a number of diverse &lt;br /&gt;
cultures and communities across the continent. The largest and most advanced Pre-Columbian &lt;br /&gt;
civilizations in North America were the Aztecs in what is now Mexico and the Maya in Central &lt;br /&gt;
America. European colonists began to arrive starting in the 16th and 17th centuries, wiping out &lt;br /&gt;
large numbers of the native populations and beginning an era of European dominance.&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;iframe width=&amp;quot;640&amp;quot; height=&amp;quot;360&amp;quot; src=&amp;quot;https://www.youtube.com/embed/PEtz0twft2U?feature=player_detailpage&amp;quot; &lt;br /&gt;
frameborder=&amp;quot;0&amp;quot; allowfullscreen=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;iframe width=&amp;quot;640&amp;quot; height=&amp;quot;360&amp;quot; src=&amp;quot;https://www.youtube.com/embed/PEtz0twft2U?feature=player_detailpage&amp;quot; &lt;br /&gt;
frameborder=&amp;quot;0&amp;quot; allowfullscreen=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/iframe&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;iframe width=&amp;quot;640&amp;quot; height=&amp;quot;360&amp;quot; src=&amp;quot;https://www.youtube.com/embed/OnzZye5tGdY?feature=player_detailpage&amp;quot; &lt;br /&gt;
frameborder=&amp;quot;0&amp;quot; allowfullscreen=&amp;quot;true&amp;quot;&amp;gt;&amp;lt;/iframe&amp;gt;&lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
 &amp;lt;img src=&amp;quot;images/bowl.jpg&amp;quot; alt=&amp;quot;Bowl with filigree&amp;quot; height=&amp;quot;80&amp;quot; width=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;img src=&amp;quot;images/bluebowl.jpg&amp;quot; alt=&amp;quot;Bowl with filigree&amp;quot; height=&amp;quot;80&amp;quot; width=&amp;quot;100&amp;quot;/&amp;gt; &lt;br /&gt;
 &amp;lt;img src=&amp;quot;images/bowl with glass.jpg&amp;quot; alt=&amp;quot;Bowl with filigree&amp;quot; height=&amp;quot;80&amp;quot; width=&amp;quot;100&amp;quot;/&amp;gt;&lt;br /&gt;
 &amp;lt;img src=&amp;quot;images/carved bowl.jpg&amp;quot; alt=&amp;quot;Bowl with filigree&amp;quot; height=&amp;quot;80&amp;quot; width=&amp;quot;100&amp;quot;/&amp;gt; &lt;br /&gt;
&amp;lt;br/&amp;gt;&amp;lt;br/&amp;gt;&lt;br /&gt;
&amp;lt;a href=&amp;quot;seleniumTest.jsp&amp;quot;&amp;gt;back to main page&amp;lt;/a&amp;gt;&lt;br /&gt;
&amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
::Find the XPath expressions that will find the following elements&lt;br /&gt;
::*All iframes on the page&lt;br /&gt;
::*The image with a source file of images/bluebowl.jpg&lt;br /&gt;
::*Any div tags on the page&lt;br /&gt;
::*All image tags on the page&lt;br /&gt;
::*The text contents of the div tag with an attribute&lt;br /&gt;
&lt;br /&gt;
:&amp;lt;big&amp;gt;References:&amp;lt;/big&amp;gt;&lt;br /&gt;
::: [http://www.w3schools.com/xsl/xpath_intro.asp XPath Tutorial]&lt;br /&gt;
::: [http://www.quackit.com/xml/tutorial/xpath_introduction.cfm Another XPath Tutorial]&lt;br /&gt;
::: [http://www.tutorialspoint.com/xpath/ A third XPath Tutorial]&lt;br /&gt;
::: [http://www.freeformatter.com/xpath-tester.html XPath Tester]&lt;br /&gt;
::: [http://www.xpathtester.com/xpath XPath Tester (Another one, previous one seems better)]&lt;/div&gt;</summary>
		<author><name>Ksolis</name></author>
	</entry>
</feed>