[xpath] XPath : 속성이없는 노드를 선택하는 방법은 무엇입니까?

XPath를 사용하여 속성이없는 노드를 선택하는 방법 (속성 개수 = 0)?

예를 들면 :

<nodes>
    <node attribute1="aaaa"></node>
    <node attribute1="bbbb"></node>
    <node></node> <- FIND THIS
</nodes>



답변

//node[not(@*)]

이것은 문서에서 속성없이 “node”라는 이름의 모든 노드를 선택하는 XPath입니다.


답변

//node[count(@*)=0]

속성이없는 모든 <node>를 선택합니다.


답변

Marek Czaplicki의 의견을 처리하고 답변을 확장하려면

//node[not(@*) or not(string-length(@*))]

…. 속성이 없거나 모두 비어있는 속성이있는 모든 노드 요소를 선택합니다. 모든 속성이 아니라 관심있는 특정 속성 인 경우 다음을 사용할 수 있습니다.

//node[not(@attribute1) or not(string-length(@attribute1))]

… 그리고 이것은 비어 attribute1있는 attribute1속성을 가진 OR 라는 속성이없는 모든 노드 요소를 선택합니다 .

즉, 다음 요소는 이러한 xpath 표현식 중 하나에 의해 선택됩니다.

<nodes>
    <node attribute1="aaaa"></node>
    <node attribute1=""></node> <!--This one -->
    <node attribute1="bbbb"></node>
    <node></node> <!--...and this one -->
</nodes>

여기에서 jsfiddle 예제를 참조 하십시오.


답변