여기서 XPath contains ()를 사용하는 방법은 무엇입니까?
xpath를 배우려고합니다. 이 주변의 다른 contains () 예제를 보았지만 AND 연산자를 사용하는 것은 없습니다. 나는 이것을 작동시킬 수 없다 :
//ul[@class='featureList' and contains(li, 'Model')]
의 위에:
...
<ul class="featureList">
<li><b>Type:</b> Clip Fan</li><li><b>Feature:</b> Air Moved: 65 ft.
Amps: 1.1
Clip: Grips any surface up to 1.63"
Plug: 3 prong grounded plug on heavy duty model
Usage: Garage, Workshop, Dorm, Work-out room, Deck, Office & more.</li><li><b>Speed Setting:</b> 2 speeds</li><li><b>Color:</b> Black</li><li><b>Power Consumption:</b> 62 W</li><li><b>Height:</b> 14.5"</li><li><b>Width:</b> Grill Diameter: 9.5"</li><li><b>Length:</b> 11.5"</li>
<li><b>Model #: </b>CR1-0081-06</li>
<li><b>Item #: </b>N82E16896817007</li>
<li><b>Return Policy: </b></li>
</ul>
...
텍스트를 포함 할 수있는 자식 요소를 li
찾는 대신 쿼리에서 첫 번째 li
자식 만보고 있습니다 'Model'
. 필요한 것은 다음과 같은 쿼리입니다.
//ul[@class='featureList' and ./li[contains(.,'Model')]]
이 쿼리는 텍스트를 포함하는 하나 이상의 자식 이있는 class
의 요소를 제공합니다 .featureList
li
'Model'
나는 이미 +1을 Jeff Yates의 솔루션에 주었다.
다음은 접근 방식이 작동하지 않는 이유에 대한 간단한 설명입니다. 이:
// ul [@ class = 'featureList'및 포함 (li, 'Model')]
contains()
함수 (또는 그 문제에 대한 XPath의 다른 문자열 함수) 의 제한이 발생합니다 .
첫 번째 인수는 문자열이어야합니다. 노드 목록에 " li
" 를 주면 노드 목록을 제공하면 문자열로 변환해야합니다. 그러나이 변환은 목록의 첫 번째 노드에 대해서만 수행됩니다.
귀하의 경우 목록의 첫 번째 노드는 <li><b>Type:</b> Clip Fan</li>
(문자열 : " Type: Clip Fan
" 로 변환 됨 ) 이는 다음을 의미합니다.
// ul [@ class = 'featureList'및 포함 (li, 'Type')]
실제로 노드를 선택합니다!
이것은 XPath에서 일반적인 오해 에 대한 오래된 질문에 대한 새로운 대답입니다 contains()
...
요약 : contains()
수단 문자열을 포함 , 하지 노드를 포함 .
상해
이 XPath는 종종 잘못 해석됩니다.
//ul[contains(li, 'Model')]
잘못된 해석 :ul
요소가 포함 된 li
요소를 선택하십시오 Model
.
이것은 잘못 되었기 때문에
contains(x,y)
x
문자열 이기를 기대 하고여러 요소를 문자열로 변환하는 XPath 규칙은 다음과 같습니다.
노드 세트는 문서 순서 에서 첫 번째 인 노드 세트에있는 노드 의 문자열 값 을 리턴하여 문자열 로 변환됩니다 . 노드 집합이 비어 있으면 빈 문자열이 반환됩니다.
올바른 해석 : 첫 번째 자식 에 하위 문자열 이 포함 된 문자열 값 이있는 ul
요소를 선택하십시오 . li
Model
예
XML
<r>
<ul id="one">
<li>Model A</li>
<li>Foo</li>
</ul>
<ul id="two">
<li>Foo</li>
<li>Model A</li>
</ul>
</r>
XPath
//ul[contains(li, 'Model')]
selects theone
ul
element.Note: The
two
ul
element is not selected because the string-value of the firstli
child of thetwo
ul
isFoo
, which does not contain theModel
substring.//ul[li[contains(.,'Model')]]
selects theone
andtwo
ul
elements.Note: Both
ul
elements are selected becausecontains()
is applied to eachli
individually. (Thus, the tricky multiple-element-to-string conversion rule is avoided.) Bothul
elements do have anli
child whose string value contains theModel
substring -- position of theli
element no longer matters.
See also
//ul[@class="featureList" and li//text()[contains(., "Model")]]
Paste my contains
example here:
//table[contains(@class, "EC_result")]/tbody
참고URL : https://stackoverflow.com/questions/1064968/how-to-use-xpath-contains-here
'programing tip' 카테고리의 다른 글
Go로 시작하는 기능이 없습니까? (0) | 2020.06.24 |
---|---|
c # /. net에서 예외를 문서화하는 방법 (0) | 2020.06.24 |
다른 포트로 아파치 리디렉션 (0) | 2020.06.24 |
bash 스크립트에서 다른 명령으로 매개 변수를 어떻게 전달합니까? (0) | 2020.06.24 |
스칼라에서 ==와 .equals의 차이점은 무엇입니까? (0) | 2020.06.24 |