fyodorfranz
fyodorfranz

Reputation: 486

Select all nodes with a child that has a certain attribute

I'm trying to select only the a elements that have a child with a c attribute. So in this case it would be only the first two a elements.

<alpha>
  <a id="1">
    <b c="1"/>
  </a>
  <a id="2">
    <b c="1"/>
  </a>
  <a id="3">
    <b />
  </a>
</alpha>

Any thoughts on how to do this? I've tried the following with no luck:

/alpha/a/b/*[@c]
//a[b/*[@c]]

Thanks in advance for any help!

edit: It was suggested that I consult the solutions posed to this question, however, the problem addressed in that thread is different than mine (involving child elements rather than attributes of child elements) and I haven't been able to extrapolate a solution to my problem from this.

Upvotes: 2

Views: 255

Answers (2)

Kachna
Kachna

Reputation: 2961

try the following experssion (abbreviated syntax):

//a[*/@c]

Or you can use unabbreviated syntax:

/descendant::a[child::*/attribute::c]

Upvotes: 1

staszko032
staszko032

Reputation: 1

This should works as expected: //a/*[@c]

Select any node "//a" with any child "/*" with attribute c "[@c]".

Upvotes: 0

Related Questions