

Sorting is one of the important features that are nearly always required in each project. These are some of the main XPath expressions that I use and are important. PersonList/Person/Name | /PersonList/Person/Age This will select all name elements and age elements. If you want to select several paths, you can make use of the or ( | ) operator. This expression is very useful if you want to iterate through the XML document, just replace the number with a variable and it will work. The operators can be used to compare strings alphabetically.īy using the following expression, you will get the second person. The following expression will retrieve all persons who are older than 18 years old. If we want to select a specific person, we can use the following: /PersonList/Person/Įxcept for the equal operator ( = ), we can also make use of the greater than ( > ) and less than ( = and != (not equal) also are acceptable. The following XPath expression will be used to select all names in the PersonList/Person element. We will use the following XML document to describe the various types of the XPath expression: In both cases, the root node is identified by a slash ( / ). We build XPath expressions similar to this. This expression will identify the file xpath.txt. As the XPath name already suggests, it uses path expressions to identify nodes in an XML document, which is similar to the path expressions of your file system: /Articles/XML/XPath/xpath.txt As was already mentioned in my first article, it works like the SQL “select” statement for databases.

Its main purpose is to retrieve specific nodes from an XML document. XPath is one of the useful features when using XML elements. As opposed to the hand-crafted solutions that one needed to use in the past, XPath enables you to write robust, self-documented, and easy to understand sorting code with a very short development cycle. As sorting is a very important task that needs to be implemented in nearly every form, simplifying its implementation will save you precious development time in your projects.

In this article, I will show you how you can use this combination with a very easy and understandable code to sort data. I also introduced a short paragraph about XPath. In the first article, I showed you how to get quick results with XML, XSL, and ASP.
