Quantcast

How to use XPath and sorting in XML

Get the WebProNews Newsletter:


[ Business]

In the first article I have shown you how to get quick results with XML,XSL and ASP. I have also introduced a short paragraph about XPath. In this article i will show you how you can use this combination with a very easy and understandable code to sort data. As sorting is a very important task that needs to be implemented in nearly every form, simplifying its implementation will save you precious developement time in your projects. 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 developement cycle.

Using XPath:

XPath is one of the useful features when using XML elements. Its main purpose is to retrieve specific nodes from an XML document. Like already mentioned in my first article, it works like the SQL ‘select’ statement for databasees. Like 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:

-==-

This expression will identify the file xpath.txt. We build XPath expressions similar to this. In both cases, the root node is identified by a slash ( / ).We will use the following XML document to describe the various types of the XPath expression:

-==-

The following XPath expression will be used to select all names in the PersonList/Person element.

-==-

If we want to select a specific person we can use the following:

-==-

Except for the equal operator ( = ), we can also make use of the greater than ( > ) and less than ( < ) operators. The combinations <=, >= and != (not equal) also are acceptable. The following expression will retrieve all persons, which are older than 18 years old.

-==-

Strings are compared case sensitively. You can use “*” as a wildcard. The operators <, > can be used to compare strings alphabetically. By using the following expression, you will get the second person. 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.

-==-

You can use

-==-

to get the last node. This is usefull for iterations. There is no function named first().If you want to select several paths, you can make use of the or ( | ) operator. This will select all name elements and age elements.

-==-

These are some of the main XPath expressions which i use and which are important.

Sorting:

Sorting is one of the important features which are nearly always required in each project. There are different ways to sort the XML data, but I want to sort the data and make use of XPath at the same time. The best way to do this is to pass a variable to the XSL file which will do the sorting for us. In the following XSL file I have declared a variable called “sortBy” which holds the value “Name” as default. This variable will be used to get the sorting parameter from the ASP file. I have also declared another variable called “strXPath” which holds the default value “//Person“, this variable is used to get the XPath expression. Here your XSL file:

-==-

In my ASP code I am using a very simple way to load the XML and XSL files. The only thing to mention is the addParameter function, which is used to pass a parameter from the ASP file to the XSL file. Note that I am passing the parameter “Name” as a sort criteria, instead I could also pass “Age” or I could use a variable which will be changed dynamically, but I think this part can be done by any of you.

Here is the asp code:

-==-

Note: To run the examples you need to install PWS on Win98 and IIS on Win2K. See the html help files of your OS CD to get more info.

Codefinger was founded 2000 by Sonu Kapoor. Sonu has studied E-Commerce in India and currently lives in Germany. After graduation he worked for several companies in Germany. He currently works as a Network Consultant and Software Developer. He has written many applications in various programming languages like VC++, ASP, ASP.NET, XML or XSL. Besides his passion for developing applications, Sonu writes articles for several major websites like Developer.com. He also works as a freelancer for CodeGuru.

How to use XPath and sorting in XML
Comments Off
About Sonu Kapoor
Codefinger was founded 2000 by Sonu Kapoor. Sonu has studied E-Commerce in India and currently lives in Germany. After graduation he worked for several companies in Germany. He currently works as a Network Consultant and Software Developer. He has written many applications in various programming languages like VC++, ASP, ASP.NET, XML or XSL. Besides his passion for developing applications, Sonu writes articles for several major websites like Developer.com. He also works as a freelancer for CodeGuru. WebProNews Writer


Top Rated White Papers and Resources

Comments are closed.

  • Join for Access to Our Exclusive Web Tools
  • Sidebar Top
  • Sidebar Middle
  • Sign Up For The Free Newsletter
  • Sidebar Bottom