开发者

How to select an attribute that contains an @-sign (at-sign)

开发者 https://www.devze.com 2023-01-26 05:22 出处:网络
Given this xml: <results> <result version=\"@2.15\" url=\"some url\"/> <result version=\"2.14\" url=\"some url\"/>

Given this xml:

<results>
    <result version="@2.15" url="some url"/>
    <result version="2.14" url="some url"/>
</results>

How do I select the element containing version="@2.15"开发者_运维百科? I have trouble figuring out how to put the @-sign in the XPath.

Thanks in advance, Erik


This XPath selects the desired result element:

/results/result[@version='@2.15']

Note: There is no need to "escape" a literal @.


How do I select the element containing version="@2.15"? I have trouble figuring out how to put the @-sign in the XPath.

In XPath this is straightforward: any string literal must be surrounded by a pair of quotes or apostrophes.

Thus:

@x 

specifies an attribute named x

but:

'@x'

is just a string literal, containing the characters '@' and 'x'

Solution:

Use:

/*/*[@version='@2.15']

This selects every element that is a child of the top element of the document and that has a version attribute with value the string "@2.15"

0

精彩评论

暂无评论...
验证码 换一张
取 消