Thursday, November 9, 2006

XPath: The unordered() function, Quite an Oddity

There is a function in XPath which takes one parameter and that XPath engines are free to implement by just retuning that parameter. It is the unordered() function. How useful can it be? The unordered() function takes a sequence of items as parameter and return a sequence that contain the same items, but not necessarily in the same order. The purpose of this function is not to shuffle items around, but to give an optimization hint to the XPath engine. Say you have: /company/department/employee[@salary > 80000] This expression returns all the employees with a salary higher than 80,000. In XPath, any expression using the path operator ("/") must return nodes in document order. The above expression falls in that category, so if Leticia appears before Bruce in the document, Leticia must be before Bruce in the sequence of nodes returned by the expression. If the document is stored in an XML database, you might have an index on "salary". The XPath engine might be able to use that index to quickly retrieve the sequence of employees with a salary higher than 80,000. But because it is created based on the index, this sequence is ordered by increasing salary. To returns nodes in document order, the XPath engine needs to reorder the nodes in the sequence. If you don't care about getting the nodes in document order, you can use the unordered() to tell the engine that this last reordering is not necessary: unordered(/company/department/employee[@salary > 80000]) Should you use unordered() in your XPath expressions? If you think that unordered() adds clarity to your expressions, then do use it. But most likely adding unordered() will do more to clutter your XPath expressions. So you might want to check first if your engine does anything special with unordered(). If you are using a stand-alone engine, most likely unordered() is a no-op. XML databases might handle unordered() in a special way, but they are not guaranteed to do so. For instance, using unordered() in the open source eXist XML database won't have any effect.

No comments:

Post a Comment