2017-05-23 39 views
0

以下はXMLの例です。XML xpath、ルート要素の子を取得

 <?xml version="1.0" encoding="utf-8"?> 
     <store d:mi="22"> 
      <book price="12.99" d:price="Number" d:mi="4"> 
        <title d:constr="String" d:mi="1">Sword of Honour</title> 
        <category d:constr="String" d:mi="2">fiction</category> 
        <author d:constr="String" d:mi="3">Evelyn Waugh</author> 
      </book> 
      <book price="8.99" d:price="Number" d:mi="9"> 
       <sublist> 
        <title d:constr="String" d:mi="5">Moby Dick</title> 
        <category d:constr="String" d:mi="6">fiction</category> 
        <author d:constr="String" d:mi="7">Herman Melville</author> 
        <isbn d:constr="String" d:mi="8">0-553-21311-3</isbn> 
       </sublist> 
      </book> 
      <Note price="8.95" d:price="Number" d:mi="13"> 
        <title d:constr="String" d:mi="10">50</title> 
        <category d:constr="String" d:mi="11">reference</category> 
        <author d:constr="String" d:mi="12">Nigel Rees</author> 
      </Note> 
      <Note price="22.99" d:price="Number" d:mi="18"> 
        <title d:constr="String" d:mi="14">The Lord of the Rings</title> 
        <category d:constr="String" d:mi="15">fiction</category> 
        <author d:constr="String" d:mi="16">J. R. R. Tolkien</author> 
        <isbn d:constr="String" d:mi="17">0-395-19395-8</isbn> 
      </Note> 
     </store> 

以下のXpathを使用して、store要素を取得できます。

しかし、私たちはストアの子である本(私たちが知らないものもあります)を入手する必要があります(定数ワード)。それは可能ですか?

答えて

0

てみ必要title無視ノード名の親と一致する表現の下に使用する:

String name = "String"; 
String xpath = "//title[@d:constr='" + name + "']/parent::*"; 

はまたstoretitleの親ではないことに注意してください、あなたは、単にあなたの場合は"//title/ancestor::store"代わりの"//title/parent::store"

を使用する必要があります子供を得たいstore

//store/child::* 

ファーストチャイルド:

//store/child::*[1] 
関連する問題