Looking at the source code, I discovered that the sort key is used only if you evaluate the expression using the rawIterator() method.
 
Basically, setSortKey() and rawIterator() are relics of an earlier XPath API in Saxon that predates the JAXP interface. The setSortKey() method is marked as deprecated, and I don't expect it will be supported for ever. I would recommend that if you need sorting functionality, you should use XQuery rather than XPath - this avoids relying on features that are local to Saxon.
 
Michael Kay
Saxonica


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Srikanth Vittal
Sent: 17 June 2008 18:01
To: Mailing list for the SAXON XSLT and XQuery processor
Subject: Re: [saxon] XPath and setSortKey - Does not work for me

Hi Michael

Like you have suggested, I tried the expression ".", and the result is still not sorted!

Here is the code:
    public static void main(String[] args) {
        Document DecisionTreeDOM;

        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder parser = factory.newDocumentBuilder();
            DecisionTreeDOM = parser.parse("/home/srikanth/code/xml/decision.xml");
           
            NodeList nl = getXPathNodeListValue(DecisionTreeDOM);
           
            for (int j=0; j<=nl.getLength(); j++) {
                System.out.println( nl.item(j).getTextContent() );
            }
           
        }
        catch (Exception e) { System.out.println(e.getMessage()); }
    }
   
    public static NodeList getXPathNodeListValue(Node node)
    {
        XPath xpath;

        try
        {
            XPathFactory xf = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI);
            xpath = xf.newXPath();

            XPathExpressionImpl xei = (XPathExpressionImpl) xpath.compile("//BufferPosition");
           
            XPathExpressionImpl sortKey = (XPathExpressionImpl) xpath.compile(".");
           
            xei.setSortKey(sortKey);
           
            NodeList xpath_value = (NodeList) xei.evaluate(node, XPathConstants.NODESET);
            return xpath_value;
        }
        catch (XPathExpressionException e) { System.out.println(e.getMessage()); return null; }
        catch (XPathFactoryConfigurationException e) {System.out.println(e.getMessage()); return null; }
    }

Here is the XML:
<Decisions>
<Decision>
    <BufferPosition>3</BufferPosition>   
</Decision>
<Decision>
    <BufferPosition>1</BufferPosition>   
</Decision>
<Decision>
    <BufferPosition>4</BufferPosition>   
</Decision>
</Decisions>

Please let me know how I should fix this.

Thanks
Srikanth

On Tue, Jun 17, 2008 at 9:05 PM, Michael Kay <mike@saxonica.com> wrote:
The sort key should be an expression that returns a different result for each node in the node list to be sorted. Because your expression starts with "//", its value is the same for all nodes. Try the expression "." instead.
 
Also please note, Saxon's XPath implementation is much more efficient when you apply it to a native Saxon tree rather than a DOM.
 
Michael Kay
http://www.saxonica.com/


From: saxon-help-bounces@lists.sourceforge.net [mailto:saxon-help-bounces@lists.sourceforge.net] On Behalf Of Srikanth Vittal
Sent: 17 June 2008 16:10
To: saxon-help@lists.sourceforge.net
Subject: [saxon] XPath and setSortKey - Does not work for me

Hi Guyz

I am trying to use setSortKey to sort the xpath result. But, the output is not sorted!!! This is what I tried:

    // BufferPosition is a tag, and I want the output to be sorted based on the text content of this tag

    Document node = parser.parse("xml/test.xml");

    XPathExpressionImpl xei = (XPathExpressionImpl) xpath.compile("//BufferPosition");
           
    XPathExpressionImpl sortKey = (XPathExpressionImpl) xpath.compile("//BufferPosition/text()");
           
    xei.setSortKey(sortKey);
           
    NodeList xpath_value = (NodeList) xei.evaluate(node, XPathConstants.NODESET);

Any idea? Please help.

Thanks
Srikanth

-------------------------------------------------------------------------
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services for
just about anything Open Source.
http://sourceforge.net/services/buy/index.php
_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net

https://lists.sourceforge.net/lists/listinfo/saxon-help