SimpleXML SimpleXMLElement::xpath

(PHP 5, PHP 7, PHP 8)

SimpleXMLElement::xpath — XML 데이터에 대해 XPath 쿼리를 실행합니다.


설명

public SimpleXMLElement::xpath(string $expression): array|null|false

xpath 메서드는 SimpleXML 노드에서 XPath expression과 일치하는 자식을 검색합니다.


매개변수

expression
XPath 경로

반환 값

성공하면 SimpleXMLElement 객체의 배열을 반환합니다. 또는 오류의 경우 null 또는 false입니다.


Examples

예제 #1 Xpath

                  
<?php
$string = <<<XML
<a>
 <b>
  <c>text</c>
  <c>stuff</c>
 </b>
 <d>
  <c>code</c>
 </d>
</a>
XML;

$xml = new SimpleXMLElement($string);

/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');

foreach ($result as $node) {
    echo '/a/b/c: ',$node,"\n";
}

/* Relative paths also work... */
$result = $xml->xpath('b/c');

foreach ($result as $node) {
    echo 'b/c: ',$node,"\n";
}
?>
                  
                

위의 예는 다음을 출력합니다.

/a/b/c: text
/a/b/c: stuff
b/c: text
b/c: stuff
                

두 결과가 같습니다.


기타