Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Xpath Examples in XML

Xpath Examples in XML

XPath Examples in XML


Given this sample XML:

<library>  <book id="b1" category="fiction">    <title lang="en">The Great Gatsby</title>    <author>F. Scott Fitzgerald</author>    <year>1925</year>  </book>  <book id="b2" category="history">    <title lang="en">Sapiens</title>    <author>Yuval Noah Harari</author>    <year>2011</year>  </book>  <book id="b3" category="fiction">    <title lang="fr">Le Petit Prince</title>    <author>Antoine de Saint-Exupéry</author>    <year>1943</year>  </book></library>

XPath Query Examples and Results

XPath ExpressionMeaningResult Node(s)
/library/bookSelect all <book> elements under <library>All 3 <book> elements
/library/book[1]Select the first <book> element<book id="b1" ...>
//titleSelect all <title> elements anywhereAll 3 <title> elements
//book[@category='fiction']Select all <book> elements with category "fiction"Books with id b1 and b3
//book/title[@lang='en']Select <title> elements where lang="en"Titles: "The Great Gatsby", "Sapiens"
//book[author='Yuval Noah Harari']Select <book> where author is "Yuval Noah Harari"<book id="b2" ...>
//book[year>1950]Select <book> published after 1950<book id="b2" ...>
/library/book/title/text()Select text content of all <title>"The Great Gatsby", "Sapiens", "Le Petit Prince"

Using XPath Axes Examples

XPath ExpressionMeaningResult
/library/book/title/parent::bookSelect parent <book> of each <title>The <book> elements
/library/book[1]/following-sibling::bookSelect books after the first bookBooks with id b2 and b3
/library/book[2]/preceding-sibling::bookSelect books before the second bookBook with id b1
//title/ancestor::librarySelect the <library> ancestor of all <title>The <library> root node

If you'd like, I can help you construct XPath expressions tailored to your XML data!

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql