PHP DOMXpath query

1 개념[ | ]

PHP DOMXPath의 query 방법

2 예시 1[ | ]

  • author가 "jmnote"인 book 찾기
  • library 앞의 //는 문서에서 library를 찾으라는 것이다.
  • book, author 앞의 /는 book이 library의 자식임을 알려준다.
  • [text() = "jmnote"]는 노드의 값을 "jmnote"와 비교한다.
$xml = ' 
<library> 
  <book isbn="isbn1111"> 
    <title>zetawiki</title> 
    <author>jmnote</author>  
    <chapter position="first"> 
      <chaptertitle>First Chapter</chaptertitle> 
      <text>Welcome to Zetawiki</text> 
    </chapter> 
  </book>
  <book isbn="isbn2222"> 
    <title>zetaq</title> 
    <author>john</author> 
    <chapter position="first"> 
      <chaptertitle>Chapter One</chaptertitle> 
      <text>Welcome to ZetaQ</text> 
    </chapter> 
  </book> 
</library>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
$result = $xpath->query('//library/book/author[text() = "jmnote"]/..'); 
var_dump($result);

3 예시 2[ | ]

  • isbn이 isbn1111인 책의 제목 찾기
  • [@isbn = 'isbn1111']는 isbn속성을 isbn1111 값과 비교한다.
$xml = ' 
<library>
  <book isbn="isbn1111">
    <title>zetawiki</title>
    <author>jmnote</author>
    <chapter position="first">
      <chaptitle>First Chapter</chaptitle>
      <text>Welcome to Zetawiki</text>
    </chapter>
  </book>
  <book isbn="isbn2222">
    <title>zetaq</title>
    <author>john</author>
    <chapter position="first">
      <chaptitle>Chapter One</chaptitle>
      <text>Welcome to ZetaQ</text>
    </chapter>
  </book>
</library>';
$dom = new DOMDocument;
$dom->loadXML($xml);
$xpath = new DOMXPath($dom);
$result = $xpath->query('//library/book[@isbn = "isbn1111"]');
var_dump($result->item(0)->getElementsByTagName("title")->item(0)->nodeValue);

4 예시 3[ | ]

  • 2개의 요소 (title, author)를 동시에 가져오기
  • [self::title or self::author]를 통해 title과 author tag를 동시에 가져온다.
$xml = '
<library>
  <book isbn="isbn1111">
    <title>zetawiki</title>
    <author>jmnote</author>
    <chapter position="first">
      <chaptitle>First Chapter</chaptitle>
      <text>Welcome to Zetawiki</text>
    </chapter>
  </book>
  <book isbn="isbn2222">
    <title>zetaq</title>
    <author>john</author>
    <chapter position="first">
      <chaptitle>Chapter One</chaptitle>
      <text>Welcome to ZetaQ</text>
    </chapter>
  </book>
</library>';

$dom = new DOMDocument;
$dom->loadXML($xml);

$query = "//book/*[self::title or self::author]";
$xpath = new DOMXPath($dom);
$result = $xpath->query($query);

for ($i = 0; $i < $result->length; $i++) {
    echo $result->item($i)->nodeValue.PHP_EOL;
}

5 예시 4[ | ]

  • 다른 level의 요소인 title과 chaptitle 찾기
  • "/library//*[self::title or self::chaptitle]"는 root 에서 library요소를 찾고 그 아래의 모든 요소 중 title, chaptitle을 찾는다.
$xml = '
<library>
  <book isbn="isbn1111">
    <title>zetawiki</title>
    <author>jmnote</author>
    <chapter position="first">
      <chaptitle>First Chapter</chaptitle>
      <text>Welcome to Zetawiki</text>
    </chapter>
  </book>
  <book isbn="isbn2222">
    <title>zetaq</title>
    <author>john</author>
    <chapter position="first">
      <chaptitle>Chapter One</chaptitle>
      <text>Welcome to ZetaQ</text>
    </chapter>
  </book>
</library>';

$dom = new DOMDocument;
$dom->loadXML($xml);

$query = "/library//*[self::title or self::chaptitle]";
$xpath = new DOMXPath($dom);
$result = $xpath->query($query);

for ($i = 0; $i < $result->length; $i++) {
    echo $result->item($i)->nodeValue . PHP_EOL;
}

6 Query 표현식 기호[ | ]

기호 설명 예시
/ root로 부터의 절대 경로 /a → xml tree의 root에서 a를 찾음
// xml 문서 전체에서 찾음 //a → xml tree의 모든 곳에서 a를 찾음
* 모든 요소 /a//* → root a 아래의 모든 요소

7 같이 보기[ | ]

8 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}