When you use XML for data storage, these sorts of problems are very common, so XQuery was created to simplify handling them. If you have an application that supports XQuery, you can write simple queries to perform all the tasks I mentioned in the previous paragraph and many, many more.
XQuery has been around for quite a while but has been slow to catch on. Part of this probably has to do with the fact that XML has yet to replace relational databases as the data storage medium of choice for modern applications, and it may never do so. However, XML has proven itself useful in just about every role outside of efficient data storage and retrieval, and could therefore benefit from an XML-specific query language. Enter XQuery.
For the latest information on XQuery, check out the W3C's XQuery page at
XQuery aims to provide XML developers with the data querying power that SQL (Structured Query Language) provides to database developers. Unlike relational databases, which typically have SQL querying features built into their database management systems, you have to use a special tool in order to execute XQuery queries on XML data. This is primarily because there is no concept of a management system for XML data because the data is simply text. Later in the tutorial you learn how to use a tool to perform XML queries using XQuery.
For a look at how to use SQL to perform queries on a relational database and generate XML results, check out Using XML with Databases.