次の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>
次のクエリを入力すると:
Object myQuery= xpath.evaluate("stock/book/chapter[3]/preceding-sibling::chapter//title",doc,XPathConstants.NODESET);
私は次のように出力を取得します:
<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が先行する兄弟軸を使用する場合です。つまり、ノードを逆の順序で返したいのです。
* <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>
つまり、基本的に、ご覧のとおり、JavaはXMLファイル内のノードの順序で、混合された順序でノードを返しますが、XPathが先行する兄弟軸を使用する場合、必要なのは、ノードを逆に返すことです。注文 。
これは、この種の「動作」が発生している(混合ノードを返す)唯一のコマンドではありません。
私の目的はこれを修正することです、私は変更しようとしましたがXPath's
evalute
、それでも機能しません。
誰かがこれを行う方法についていくつかの指示/ヒントを与えることができますか?
PS
このコードを使用する:
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("*******");
}
上記の結果が得られました(混合ノード)