2

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

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
venerik
  • 5,766
  • 2
  • 33
  • 43

2 Answers2

2

This XPath selects the desired result element:

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

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

1

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"

Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431