XPath is a language that queries the DOM (XML Path Language). It was developed to find your XML path and has been continuing since 1999.

Small revisions are published. As always, W3C is behind it. You can follow W3 XPATH for detailed examination.

XPath is designed using XPOINTER, XSLT, and other XML technologies.

I recommend reviewing Introduction to XML to learn concepts such as root element, child element, etc. when writing XPath.

We'll continue with the library.xml we used before. To recall:

<books>
	<book isbn="9759954949">
		<title>Yunus Emre Divani</title>
		<author role="Editor">Selim Yagmur</author>
		<language>Turkish</language>
		<edition>8</edition>
		<date>
			<year>2014</year>
			<month>04</month>
			<day>01</day>
		</date>
	</book>
	<book isbn="9753386203">
		<title>Risaletun-Nushiyye Yunus Emre</title>
		<author role="Translator">Prof. Dr. Umay Turkes Gunay</author>
		<language>Turkish</language>
		<edition>3</edition>
		<date>
			<year>2009</year>
			<month>01</month>
			<day>01</day>
		</date>
	</book>
</books>

Now, starting from the XML, let's transfer the book elements to a variable and list only the titles from there.

<?php
$xml = simplexml_load_file("library.xml"); // we load the library into a variable
$results = $xml->xpath('book'); // we assigned all elements with the book element to the results variable.
foreach ($results as $key => $value) {
echo $value->title; // we print the value of the title element under the book elements to the screen
}
?>
// Screen Output
Yunus Emre Divani
Risaletun-Nushiyye Yunus Emre

So when we use xpath('element'), it brings those elements. Now in our other example, when we use xpath('/element'), it's called full path, meaning you need to write in order from the root to the destination.

<?php
$xml = simplexml_load_file("library.xml"); // we load the library into a variable
$results = $xml->xpath('/books/book'); // we assigned all elements with the book element under the books root to the results variable.
foreach ($results as $key => $value) {
echo $value->title; // we print the value of the title element under the book elements to the screen
}
?>
// Screen Output
Yunus Emre Divani
Risaletun-Nushiyye Yunus Emre

We got the same result as you've seen above.