2014-04-15 56 views
0

我有這樣的XPath(感謝伊恩·羅伯茨的答案SO Xpath2.0 selecting first and last occurance of string with iteration):的XPath替換FOR語句

Events/Properties[ 
    count(
    for $me in (Property[@Descriptor="200"]) return 
     (preceding-sibling::Properties[1][Property[@Descriptor="200"] = $me], 
     following-sibling::Properties[1][Property[@Descriptor="200"] = $me]) 
) != 2 
] 

的XPath是偉大的,但我使用的工具不接受for聲明。

如何重寫此XPath以避免此問題[沒有for聲明]?

第一批組:A組

//Properties[ 
    Property/@Descriptor = 200 
    and not(
    Property = preceding-sibling::*/Property[@Descriptor = 200] 
) 
] 

最後的

+0

那麼你使用的是什麼工具? – Tomalak

+0

專有的報告工具...沒有與Xpath相關的規範是誠實的。只需使用XPath作爲選擇XML文檔中節點的方式。 –

+1

所以我假設你想要一個XPath 1.0表達式,因爲如果你的工具在2.0上存在,那麼它可能不夠現代。 – Tomalak

回答

1

假設你輸入命令(!這是最重要的一點),你可以通過發出這些的XPath 1.0表達式做到這一點:

//Properties[ 
    Property/@Descriptor = 200 
    and not(
    Property = following-sibling::*/Property[@Descriptor = 200] 
) 
] 

你可以從那裏選擇Property[@Descriptor = 100]Property[@Descriptor = 200]獨立。


可以兩個表達式組合成一個:

//Properties[ 
    Property/@Descriptor = 200 and ( 
    not(
     Property = preceding-sibling::*/Property[@Descriptor = 200] 
    ) or not(
     Property = following-sibling::*/Property[@Descriptor = 200] 
    ) 
) 
] 

你會得到(在各自小組第一最後的和)的<Properties>雙名單,除了是一個元素長組。這意味着無法保證您不會返回偶數個節點。