2017-12-27 274 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.(在“无价值”属性的情况下)