문제

그것은 사용하는 것이 가능하 XPath 를 선택하는 노드가 있는 특정한 아이는 요소는?예를 들어,이로부터 XML 나만을 원하는 요소 애완 동물에서는 아이가 있는'바'.그래서 그 결과 데이터 집합을 포함하는 것 lizardpig 소에서 이 예제:

<pets>
  <cat>
    <foo>don't care about this</foo>
  </cat>
  <dog>
   <foo>not this one either</foo>
  </dog>
  <lizard>
   <bar>lizard should be returned, because it has a child of bar</bar>
  </lizard>
  <pig>
   <bar>return pig, too</bar>
  </pig>
</pets>

이 Xpath 나에게 모든 애완 동물: "/pets/*", 지만,나만 애완 동물을위 노드의 이름을 'bar'.

도움이 되었습니까?

해결책

여기에 그것은,모든 영광에

/pets/*[bar]

영어:나에게 모든 어린이들의 pets 는 아 bar

다른 팁

/pets/child::*[child::bar]

내 용서하지 않았다,주석을 참조하십시오 이전에 답변했다.

그러나 이 경우에는 오히려보를 사용하는 descendant:: 축 포함하는 모든 요소로서 지정:

/pets[descendant::bar]

다만 경우에 당신이 원하는 것을 더 특정에 대한 어린이용-사용할 수도 있습니다 선택기들입니다.

예제:

<pets>
    <cat>
        <foo>don't care about this</foo>
    </cat>
    <dog>
        <foo>not this one either</foo>
    </dog>
    <lizard>
        <bar att="baz">lizard should be returned, because it has a child of bar</bar>
    </lizard>
    <pig>
        <bar>don't return pig - it has no att=bar </bar>
    </pig>
</pets>

지금,당신은 단지에 대한 모든 관리 pets 어떤 아 bar 는 특성 attbaz.다음을 사용할 수 있습니다 xpath expression:

//pets/*[descendant::bar[@att='baz']]

결과

<lizard>
    <bar att="baz">lizard should be returned, because it has a child of bar</bar>
</lizard>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top