Ronene 0 Newbie Poster

Hello ,

I'm trying to solve a "known problem" in XPath 1.0 .
From msdn : "Although the ordering of returned collections is not specified in the XML XPath Language 1.0 Recommendation, this extension method returns nodes in document order.
Note that nodes are returned in document order even when you use a reverse axis, such as preceding-sibling or ancestor-or-self
."
I've been given the pleasure to fix it :)

So basically ,as you can see ,Java returns the nodes in a mixed order , by the order of the nodes in the XML file , but what I need is , when XPath uses the preceding-sibling axis ,then return the nodes in a reverse order .

Below you can see an exmaple ,however ,this is not the one and only query where this kind of "behavior" is happening (returning mixed nodes) .

My objective is to fix this , I've tried to change XPath's evalute , but it's still doesn't work .

Can anyone give some directions / hints how to do this ?

A little example might help ,take for example the following XML file :

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stock SYSTEM "new.dtd">
<stock>
    <book num="myBook1">
        <title>Lindsy Boxer</title>
        <author>James Patterson</author>
        <publisher>LittleBig</publisher>
        <price>18.21</price>
        <chapter>
            <title>Alex Cross Is Back - Chapter A</title>
            <paragraph>
                This is the <emph>first</emph> paragraph.
                <image file="alexCrossImage.gif"/>
                afetr image...
            </paragraph>
            <paragraph>
                This is the <emph>second</emph> paragraph.
                <image file="alexCrossImageAnother.gif"/>
                afetr image...
            </paragraph>
        </chapter>
        <chapter>
            <title>Along Came A Spider - Chapter B</title>
            <section>
                <title>Along Came A Spider - Chapter B - section 1</title>
                <paragraph>
                    This is the <emph>first</emph>paragraph for chapter TWO section ONE.
                    <image file="Chapter_B_firstParagraphImage.gif"/>
                    afetr image...
                </paragraph>
                <paragraph>
                    This is the <emph>second</emph> paragraph for chapter TWO section ONE.
                    <image file="Chapter_B_secondParagraphImage.gif"/>
                    afetr image...
                </paragraph>
            </section>
        </chapter>
        <chapter>
            <title>Chapter C</title>
            <paragraph>
                This chapter has no images and only one paragraph
            </paragraph>
        </chapter>
    </book>
    <book num="myBook2">
        <title>Jack Reacher Series</title>
        <author>Lee Child</author>
        <author>Jonny White</author>
        <publisher>Pocket</publisher>
        <price>5.99</price>
        <chapter>
            <title>Jack Reacher - Chapter ONE</title>
        </chapter>
        <chapter>
            <title>Jack Reacher - Chapter TWO</title>
            <paragraph>
                This is the <emph>second</emph> paragraph of SECOND book chapter TWO.
                <image file="Jack_Reacher_Picture_Top_Sniper_US_Army.gif"/>
                afetr image...
            </paragraph>
        </chapter>
    </book>
    <book num="myBook3">
        <title>Alex Cross - Double Cross</title>
        <author>James Patterson</author>
        <publisher>Spectra</publisher>
        <price>17.30</price>
        <chapter>
            <title>Alex Cross - Double Cross - Chapter A</title>
        </chapter>
    </book>
</stock>

Now ,If I type the query :

Object myQuery= xpath.evaluate("stock/book/chapter[3]/preceding-sibling::chapter//title",doc,XPathConstants.NODESET);

I'd get :

<title>Alex Cross Is Back - Chapter A</title>
<title>Along Came A Spider - Chapter B</title>
<title>Along Came A Spider - Chapter B - section 1</title>

But what I want is :

<title>Along Came A Spider - Chapter B</title>
<title>Along Came A Spider - Chapter B - section 1</title>
<title>Alex Cross Is Back - Chapter A</title>

I thought about trying to "cover" all the possible reversed queries that out there, but I think that it's pretty much impossible , since
there is always a chance that I missed one .

What I'm trying to find is a generic solution for the reversed-axis queries .
I've been trying to solve this for the last 3 days however found nothing concrete , so I'd appreciate any help .

Thanks ,
Ron