2017-12-27 273 views
2

從這個Stack Overflow answer任何屬性,我可以看到如何匹配與特定字符串開頭的任何屬性值。例如,當發現有任何屬性用一個值開頭字母H元素的列表:XPath查詢匹配與名稱以X

//*[@*[starts-with(., 'h')]]

我的問題是如何搭配屬性與一個特定的字符串開始?

據我所知,@*表示匹配任何元素的更廣泛的//*集合中的任何屬性。 starts-with函數需要兩個參數,haystack和needle,並且乾草堆的.表示屬性值。我覺得我是90%的路,但我無法弄清楚我需要在屬性名稱上匹配什麼,而不是價值

例子匹配:

<example> 
    <first match-me="123">MATCH!</first> 
    <second do-not-match-me="456">NO MATCH!</second> 
    <third match-me-yes-please="789">MATCH!</third> 
    <fourth> 
     <fifth match-me:oh-yes>MATCH!</fifth> 
    </fourth> 
</example> 

回答

3

「我覺得我90%的有我的路」 - 是的,最後一個階段是指定屬性名稱的方法:

//*[@*[starts-with(name(), 'match-me')]] 

注意,依靠在簡單屬性名稱匹配時,處理像match-me:oh-yes(內部冒號)等屬性名稱時可能會遇到問題。在這種情況下,您可能會收到錯誤The prefix "match-me" for attribute "match-me:oh-yes" associated with an element type "fifth" is not boundAttribute name "match-me:oh-yes" associated with an element type "fifth" must be followed by the ' = ' character.(在「無價值」屬性的情況下)