2014-02-13 26 views
6

選擇替代的子路徑此作品:如何在XPath的

(//someparentpath/h3 | //someparentpath/ul/li/div)/somechildpath 

這並不:(爲什麼

//someparentpath/(h3 | ul/li/div)/somechildpath 

回答

0

好問題,我很感興趣的是 「爲什麼」 自己

儘管如此,你可以改寫這個

//someparentpath/(h3 | ul/li/div)/somechildpath 

,因爲這(不完全等同)

//someparentpath//*[self::h3 or self::ul/li/div]/somechildpath 

這是不是太糟糕得多。

但是,我一直對此感到沮喪。

+0

不幸的是,這給了我一樣// someparentpath/h3/somechildpath | // someparentpath/ul/somechildpath – Marina

+0

@Marina,我的錯誤,請參閱編輯。這並不完全等同於你所要求的,但可能已經足夠接近了。區別在於它可以包含不是'someparentpath'的直接子節點的'h3'(以及'ul/li/div')。爲此,恐怕你需要多餘的表達。 – harpo

3

我認爲你可以得到最接近的是以下幾點:

//someparentpath//*[self::h3 or self::div[parent::li/parent::ul]]/somechildpath 

然而,正如哈波已經提到的,//防止它們完全等同,我不認爲這是在周圍的一種方式一個單一的表達。請注意,如果你是在XSLT工作,您可以使用多個變量賦值達到你正在嘗試做的,避免了冗餘的某些部分:

<xsl:variable name="ppath" select="//someparentpath" /> 
<xsl:variable name="children" select="($ppath/h3 | $ppath/ul/div/li)/somechildpath" /> 

注意,假設表達你描述:

//someparentpath/(h3 | ul/li/div)/somechildpath 

允許在XPath 2.0中。這在XPath 1.0中是不允許的,因爲1.0不允許你在表達中途通過路徑。

6

XPath 1.0的語法不允許在軸步驟中進行更改,請參閱specifications for node sets或嘗試XPath 1.0 Grammar Test Page。您的第一個查詢是語法允許的,第二個查詢不是有效的XPath 1.0。

如果你有任何機會,切換到一個XPath 2.0實現它提供在查詢XML更可能性。這兩個查詢都是有效的XPath 2.0語句。

XPath 1。0,你將不得不採取:

  • 完全寫出來的路徑兩次,並使用這些的結合:

    //someparentpath/h3/somechildpath | //someparentpath/ul/li/div/somechildpath 
    

    或公共軸允許的查詢中的步驟,其是至少一點點少重複:

    (//someparentpath/h3 | //someparentpath/ul/li/div)/somechildpath 
    
  • 使用一些descending-or-self - 使用harpo和JLRishe提出的謂詞,但它們有共同之處,即可能會匹配比您想要的更多的元素。