2009-08-28 31 views
1

有沒有辦法使用當前上下文中的數據來過濾statsource中其他位置的節點。在XPATH中,使用當前上下文中的數據過濾其他數據

舉例來說,如果我有這樣的XML:

<root> 
    <group1> 
     <inst> 
      <type>Foo</type> 
      <value>First Foo</value> 
     </inst> 
     <inst> 
      <type>Bar</type> 
      <value>The Bar</value> 
     </inst> 
     <inst> 
      <type>Foo</type> 
      <value>Second Foo</value> 
     </inst> 
    </group1> 
    <group2> 
     <Filter> 
      <FilterType>Foo</FilterType> 
     </Filter> 
     <Filter> 
      <FilterType>Bar</FilterType> 
     </Filter> 
    </group2> 
</root> 

假設我的上下文是過濾器標籤中的一個,我想回到獲得1組指定類型的實例的數量。我想寫XPATH,看起來像這樣:

count(/root/group1/inst[type = **FilterType**]) 

有什麼我可以用來獲取原始上下文中的FilterType?

回答

1

這可以很容易了XPath 2.0來完成:

for $type in /*/*/Filter[1]/FilterType 
    return 
    count(/*/group1/*[type eq $type]) 

當此XPath表達式與提供的XML文檔進行評價,則返回正確的結果:

在XPath 1.0中,如果預先知道group1/inst元素的數量,並且$ vType代表所討論的FilterType,則可以共享nstruct以下XPath 1.0表達式:

($vType = /*/group1/inst[1]/type) 
+ 
    ($vType = /*/group1/inst[2]/type) 
+ 
    ($vType = /*/group1/inst[3]/type) 

這再次產生:

2.

最後,如果需要在XSLT和 「過濾器」 中的XPath 1.0表達式是當前節點,

然後下面的XPath表達式來計算的匹配的準確數目:

count(/*/group1/inst[type = curent()/FilterType]) 
0

我正在尋找相同問題的解決方案。目前,我使用一個變量來解決此路線......它會看起來像下面

<xsl:variable name='FilterType'><xsl:value-of select='FilterType'/></xsl:variable> 
<xsl:value-of select='count(/root/group1/inst[type = $FilterType])'/> 

但是,必須有一個更好的辦法。

相關問題