java - XPath 2.0 中的函数 Reverse() : how can use it to evaluate an expression?

标签 java xml xpath xpath-2.0

请放轻松,这是我第一次使用XPath 2.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();

那么如果我尝试使用方法evaluateAsString像这样:rev.evaluateAsString(arg0)然后 arg0应该是 XPathContext目的 。

如何通过上述方法使用我的查询?

问候

编辑 1:

在亲爱的@Dimitre Novatchev 先生写的例子中,需要的是来自节点 X 的所有节点。 , 到文档的上侧。

但是,如果 X's 之一 sibling ,有 child ,然后我需要展示 sibling ,然后是他( sibling 的) child (然后转到下一个 sibling ,然后再次相同),和 不是 sibling 的 child ,只有那时 - sibling 。

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

再次感谢 :)

最佳答案

只需评估这个 XPath 2.0 表达式 :

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

这是一个基于 XSLT 的验证 :
<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>

当此转换应用于以下 XML 文档时 (取自您之前的问题):
<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>

对上面的 XPath 表达式求值,并将生成的节点序列复制到输出:
<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>

正如我们所见,该序列包含所需的元素,并且按照逆文档顺序——完全符合要求。

至于如何使用 Saxon 9.x 评估 XPath 表达式,请阅读相应的文档:http://www.saxonica.com/documentation/xpath-api/s9api-xpath.xml它有一个指向完整工作代码示例的指针。

更新 :

在评论中,OP 表示他需要 chapter元素以相反的文档顺序排列,但它们的标题元素以文档顺序排列。

要实现这一点,请使用:
reverse(/inventory/book/chapter[3]/preceding-sibling::chapter)//title

关于java - XPath 2.0 中的函数 Reverse() : how can use it to evaluate an expression?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10565040/

相关文章:

java - 从 Java 中的 XML 中删除 XML header

c# - 如何在 C# 中的 XML 文件中添加 XML 属性

python - 属性错误 : 'float' object has no attribute 'get'

xml - IoC/依赖注入(inject) - 请解释代码与 XML

java - Android PDF ScrollView 单个连续页面

java - 捕获Spring数据异常

java - 使用 mule-domain-maven-plugin 发布时双重上传

java - 在 selenium 中定位 WebElements 的子节点

c# - HTML XPath 按类名搜索

c# - XPath 在浏览器和 HtmlAgilityPack 中给出不同的结果