Hi Michael

Thank you very much.

Thanks
Srikanth

On Tue, Jun 17, 2008 at 11:42 PM, Michael Kay <mike@saxonica.com> wrote:
I have now fixed this in the Subversion source code (9.0 branch).
 
I found you also need to use "string(.)" as the sort key - there is no automatic atomization of the node to an atomic value.
 
Michael Kay
http://www.saxonica.com/


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


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


-------------------------------------------------------------------------
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



--
Thanks
Srikanth