Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当XPath使用前面的同级轴时,我想以相反的顺序返回节点?怎样_Java_Xml_Xpath_Xml Parsing - Fatal编程技术网

Java 当XPath使用前面的同级轴时,我想以相反的顺序返回节点?怎样

Java 当XPath使用前面的同级轴时,我想以相反的顺序返回节点?怎样,java,xml,xpath,xml-parsing,Java,Xml,Xpath,Xml Parsing,给定以下XML文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE stock SYSTEM "new.dtd"> <stock> <book num="myBook1"> <title>Lindsy Boxer</title> <author>James Patterson</author>

给定以下XML文件:

<?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>
我得到的输出是:

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

我得到了上面的结果(混合节点)

事实上,这不是“混合”顺序。这只是XPath执行的一个结果。正如我所见,你需要一个。DFS结果将与普通搜索结果类似-文档中首先出现的内容,将在搜索结果中出现较早的内容


您可以尝试使用流读取器自己实现它。这不是DFS,而是可以提供所需结果的普通XML读取器。

事实上,这不是“混合”顺序。这只是XPath执行的一个结果。正如我所见,你需要一个。DFS结果将与普通搜索结果类似-文档中首先出现的内容,将在搜索结果中出现较早的内容


您可以尝试使用流读取器自己实现它。这不是DFS,而是可以提供所需结果的普通XML读取器。

您希望所选节点的返回顺序不是其文档顺序

这在今天的XPath 1.0引擎中是不可能的——它们都以文档顺序返回所选节点。XPath 1.0没有指定任何特殊顺序的方法

因此,如果您使用的是XPath 1.0(似乎是这样),那么您将不得不接受这种情况——例如,索引结果时不是增加索引,而是减少索引

在XPath 2.0中有序列,您可以编写XPath表达式并指定序列中的每个项

或者(同样在XPath 2.0中),您可以只拥有:

reverse(yourExpression)

这将生成项目的相反顺序-
yourExpression

生成的项目顺序,您希望所选节点以非其文档顺序的顺序返回

这在今天的XPath 1.0引擎中是不可能的——它们都以文档顺序返回所选节点。XPath 1.0没有指定任何特殊顺序的方法

因此,如果您使用的是XPath 1.0(似乎是这样),那么您将不得不接受这种情况——例如,索引结果时不是增加索引,而是减少索引

在XPath 2.0中有序列,您可以编写XPath表达式并指定序列中的每个项

或者(同样在XPath 2.0中),您可以只拥有:

reverse(yourExpression)


这将产生项的相反顺序-
yourExpression

产生的项的顺序-,为什么你希望按这个顺序得到节点呢,这个q与计算XPath表达式时选择的节点非常相似,这些节点总是按文档顺序返回——您希望XPath对某些人(您的)的首选顺序了解多少?XPath 1.0中没有用于指定不同顺序的工具。在XPath 2.0中,可以构造一个可以是任何所需顺序的序列。从您的问题中不清楚所需顺序是什么——最好编辑并提供解释。人们无法猜出你认为什么是“好”的顺序,也不能猜出XPath。我想知道的是,当XPath使用前面的同级轴时,然后以相反的顺序返回节点。为什么您希望以这种顺序获取节点?此外,这个q与计算XPath表达式时选择的节点非常相似,它们总是以文档顺序返回——您希望XPath了解(您的)someon的哪些信息优先顺序?XPath 1.0中没有用于指定不同顺序的工具。在XPath 2.0中,可以构造一个可以是任何所需顺序的序列。从您的问题中不清楚所需顺序是什么——最好编辑并提供解释。人们无法猜出你认为什么是“好”的顺序,也不能猜出XPath。我想知道的是,当XPath使用前面的同级轴时,然后以相反的顺序返回节点。问题是我需要修复它,但我不知道如何在XPath 1.0下完成,而不是(!!)XPath 2.0。你能解释一下我该怎么做吗?重写“evaluate”或任何其他函数都没有问题,但是这个主题(XPath)对我来说是非常新的。非常感谢。@ron:正如我在这个答案中开始的那样,这不能仅用XPath 1.0来完成——您必须以相反的顺序处理返回的节点列表——这不是从
0
count(yourExpr)-1
的递增索引,而是从
count(yourExpr)-1
0
,索引递减。@ron:那么你对XPath的理解就错了——永远不可能以相同的方式处理所有XPath表达式的结果——这是毫无意义的。您需要阅读XPath中有关“反向轴”的更多内容,并了解在计算包含反向轴的表达式时,
position()
函数的语义是不同的。在XPath 2.0中,这样的一般处理变得更加不可能——有太多可能的求值结果类型…@ron:我最确信的是,我没有见过或听说过任何XPath 1.0实现以文档顺序以外的方式返回选定节点。也许你有足够的空闲时间来寻找不可抗拒的实现,但我的建议是把这段时间花在更有效率的事情上:)@ron:我认为输入(XML)不需要更改,而且这通常是不允许的。即使这是可能的,另一个应用程序也可能需要使用reverse axis选择修改文档的某些节点。。。我们只需通过XPath表达式处理作为选定节点的XPath引擎表示形式的任何节点列表,因为我们知道选定节点总是按顺序排列的
        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        domFactory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = domFactory.newDocumentBuilder();
        Document doc = builder.parse("new.xml");
        //create an XPath factory
        XPathFactory factory = XPathFactory.newInstance();
        //create an XPath Object
        XPath xpath = factory.newXPath();
        //***********************************************************//
        Object myQuery= xpath.evaluate("stock/book/chapter[3]/preceding-sibling::chapter//title",doc,XPathConstants.NODESET);
        System.out.println("The Results are:");
        NodeList nodes = (NodeList) myQuery;
        //print the output
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println("i: " + i);
            System.out.println("*******");
            System.out.println(nodeToString(nodes.item(i)));
            System.out.println("*******");
        }
reverse(yourExpression)