Java XPath 2.0中的函数Reverse():如何使用它计算表达式?

Java XPath 2.0中的函数Reverse():如何使用它计算表达式?,java,xml,xpath,xpath-2.0,Java,Xml,Xpath,Xpath 2.0,请放轻松,这是我第一次使用XPath2.0 给定以下代码: 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 M

请放轻松,这是我第一次使用XPath2.0

给定以下代码:

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);
    }

}
我要反转查询:

String query = "inventory/book/chapter[3]/preceding-sibling::chapter//title";
Object myQuery= xpath.evaluate(query,doc,XPathConstants.NODESET);
对于我的XML文件(如果您愿意,我可以附加XML文件,因此如果确实需要,请这样说!

根据我的理解,如果我这样做:

MyReverse rev = new MyReverse();
然后,如果我尝试像这样使用
evaluateString
方法:
rev.evaluateString(arg0)
然后
arg0
应该是
XPathContext
对象

如何使用上述方法进行查询

问候

编辑1:

<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>
在尊敬的@Dimitre Novatchev先生所写的示例中,需要的是从node
X
到文档上方的所有节点

但是,如果某个
X的
兄弟姐妹中有一个有孩子,那么我需要先介绍兄弟姐妹,然后再介绍他的(兄弟姐妹的)孩子(然后再介绍下一个兄弟姐妹,同样的情况再次出现),而不是兄弟姐妹的孩子&只有在那时——兄弟姐妹

很抱歉,我之前没有提及和解释这一点


再次感谢:)

只需计算这个XPath 2.0表达式:

reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
<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>
这里是一个基于XSLT的验证

reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
<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>

只需计算此XPath 2.0表达式即可

reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
<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>
这里是一个基于XSLT的验证

reverse(/inventory/book/chapter[3]/preceding-sibling::chapter//title)
<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>


您正在摆弄内部Saxon实现类,这些类本不需要使用,但您显然不了解。您应该在XPath表达式中完成所需的工作(颠倒一组节点的顺序),而不是在调用Java代码中。

您正在摆弄内部Saxon实现类,这些类本不需要使用,但您显然不了解。您应该在XPath表达式中完成所需的工作(颠倒一组节点的顺序),而不是在调用Java代码中。

XPath 2.0有一个
reverse
函数,因此您可以在XPath表达式级别上使用
reverse(inventory/book/chapter[3]/previous sibling::chapter//title)
@MartinHonnen:我知道,但我想在功能层面上这样做,有可能吗?@ron:对不起,我不明白你在编辑中说了什么。*Reverse*的意思是:根据定义,
Reverse(n1,n2n,nM)=(nM,n(M-1),…n2,n1
。您希望所选节点按相反的文档顺序排列--这是
Reverse()时的结果
函数应用于选定的节点集。如果您现在想要一些不同的东西,请提出一个新问题。@ron:我想在您的新问题中对此进行解释。请不要使用“反向”一词,因为它的意思与您想要的不同。晚安。好的,我想您想要的是:
反向(/inventory/book/chapter[3]/previous sibling::chapter)//title
XPath 2.0有一个
reverse
函数,因此您可以在XPath表达式级别上使用
reverse(inventory/book/chapter[3]/previous sibling::chapter//title)来解决这个问题
@MartinHonnen:我知道,但我想在函数级别上这样做,可能吗?@ron:对不起,我不明白你在编辑中说的话。*Reverse*的意思是:根据定义
Reverse(n1,n2n,nM)=(nM,n(M-1),…n2,n1
。您希望所选节点按相反的文档顺序排列--这是对所选节点集应用
reverse()
函数时的结果。如果您现在想要不同的内容,请提出一个新问题。@ron:我希望在您的新问题中对此进行解释。请不要使用“reverse”一词晚安,我想你想要的是:
reverse(/inventory/book/chapter[3]/previous sibling::chapter)//title
链接已断开,您是否有其他选择?10x!@ron:我可以访问该链接。显然,您存在互联网连接问题。我起初没有注意到,但“反向”功能无法完成所需的操作。请查看您的输出,并将其与请求的输出进行比较(这是请求的):“*来了一只蜘蛛-第B章*来了一只蜘蛛-第B章-第1节*亚历克斯·克罗斯回来了-第A章”…正如你所看到的,对于前两个条目,第一行应该是第二行(第二行应该是第一行)…@ron:在你的问题中,你没有明确说明“请求”是什么“。您只需说:“我想反转查询”。现在,这对我来说意味着您想获得所选元素的序列,但顺序相反。
reverse()
函数正是这样做的。如果您想要另一个顺序,那么您必须编辑问题,删除单词“reverse”并解释到底需要什么样的新顺序。或者更好的是,开始一个新问题,这次要更加注意你说的话。@ron:我用XPath表达式更新了这个答案,根据你的新要求选择元素。链接断了,你可能还有其他选择吗?10x!@ron:我可以访问链接,好的。显然,你存在internet连接问题。起初我没有注意到,但“反转”功能无法完成所需的操作。请查看您的输出,并将其与请求的输出进行比较(这是请求的):“*来了一只蜘蛛-第B章*来了一只蜘蛛-第B章-第1节*亚历克斯·克罗斯回来了-第A章”…正如你所看到的,对于前两个条目,第一行应该是第二行(第二行应该是第一行)…@ron:在你的问题中,你没有明确说明“请求”是什么“。您只需说:“我想反转查询”。现在,这对我来说意味着您想获得所选元素的序列,但顺序相反。而
reverse()
函数正好做到了这一点