Tuesday, February 02, 2010

XPATH / XSLT sorting

As result of the CapeSOA 11g training, some nice solutions and ideas are created. Within this training there is a business requirement to sort particular data to get the cheapest value. There are many ways to determine the cheapest value from a XML payload. One solution is to use XLST off course.

But how about the idea to do this without XLST?

The trick here is to use XPath to select the best item (flight, car, hotel) from the list. Suppose your data looks something like this:
  • Results
    • Result
      • Price
      • ...
    • Result
      • Price
      • ...
    • ...
What you want is the Result, such that is has the lowest price element. You can rephrase this to: "the result such that there is no other result with a lower price". In XPath you can write this as follows: "/Results/Result[not(../Result/Price < ./Price)]". The bit between the square brackets is a filter on what comes directly before it. So "/Results/Result" selects all Result elements, and "[not(../Result/Price < ./Price)]" further refines this to only those results that have the lowest price.

Credits go to jan willem broek for this solution!

Post a Comment