Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7936211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:08:41+00:00 2026-06-03T22:08:41+00:00

Please go easy on me , this is my first time using XPath 2.0

  • 0

Please go easy on me , this is my first time using XPath 2.0 🙂

Given the following code :

import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.StaticContext;
import net.sf.saxon.functions.CompileTimeFunction;
import net.sf.saxon.sort.Reverser;
import net.sf.saxon.trans.XPathException;

public class MyReverse extends CompileTimeFunction {

    public Expression simplify(StaticContext env) throws XPathException {
        Reverser a = new Reverser(argument[0]);
        return a.simplify(env);
    }

}

I want to reverse the query :

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

for my XML file (I can attach the XML file if you wish ,so please say so if it’s indeed required!)

Now from my understanding if I do this :

MyReverse rev = new MyReverse();

then if I try to use the method evaluateAsString like this : rev.evaluateAsString(arg0)
then arg0 should be a XPathContext object .

How can I use my query with the above method ?

Regards

EDIT 1:

In the example that dear Mr. @Dimitre Novatchev wrote , what is needed is all the nodes from node X , to the upper side of the document.

However , if one of X's siblings , has children , then I need to present the sibling and only then his (the sibling’s) children (and after that move on to the next sibling , and the same all over again) , and not the child of the sibling & only then – the sibling .

I’m sorry for not mentioning & explaining this earlier

thanks again 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:08:44+00:00Added an answer on June 3, 2026 at 10:08 pm

    Just evaluate this XPath 2.0 expression:

    reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
    

    Here is an XSLT -based verification:

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
         <xsl:sequence select=
         "reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
         "/>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the following XML document (taken from your previous question):

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

    the above XPath expression is evaluated and the resulting sequence of nodes is copied to the output:

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

    As we can see, the sequence contains the wanted elements and in reverse-document order — exactly as requested.

    As for how to evaluate an XPath expression with Saxon 9.x, read the appropriate documentation: http://www.saxonica.com/documentation/xpath-api/s9api-xpath.xml which has a pointer to a fully working code example.

    UPDATE:

    In a comment the OP has indicated that he needs the chapter elements in reverse document order, but their title elements in document order.

    To avhieve this, use:

    reverse(/inventory/book/chapter[3]/preceding-sibling::chapter)//title
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This my first time asking a question so please go easy one me :-p
This is my first time using recursion in actionscript so I'm sure that there
First time posting a question on StackOverflow, so please go easy on me :)
This is my first question so please go easy :) I am new to
First time poster so please take it a bit easy on me if I
Hi guys this is my first question ever on SO so please go easy
please be easy - first post! Looking to modify the following script: http://jonraasch.com/blog/a-simple-jquery-slideshow Love
Hi there , it my first time here - please go easy on me!
I'm fairly new to this so please go easy on me if this is
Please your opinion on the following code. I need to calculate the diff in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.