PHP registerXPathNamespace() 함수
예시
다음 XPath 쿼리에 대한 네임스페이스 컨텍스트를 만듭니다.
<?php
$xml=<<<XML
<book xmlns:chap="http://example.org/chapter-title">
<title>My Book</title>
<chapter id="1">
<chap:title>Chapter 1</chap:title>
<para>Donec velit. Nullam eget tellus...</para>
</chapter>
<chapter id="2">
<chap:title>Chapter 2</chap:title>
<para>Lorem ipsum dolor sit amet....</para>
</chapter>
</book>
XML;
$sxe=new SimpleXMLElement($xml);
$sxe->registerXPathNamespace('c','http://example.org/chapter-title');
$result=$sxe->xpath('//c:title');
foreach ($result as $title)
{
echo $title . "<br>";
}
?>
정의 및 사용
registerXPathNamespace() 함수는 다음 XPath 쿼리에 대한 네임스페이스 컨텍스트를 만듭니다.
이 함수는 XML 문서에서 네임스페이스 접두사가 변경된 경우에 유용합니다. registerXPathNamespace() 함수는 애플리케이션 코드를 너무 많이 변경하지 않고도 영향을 받는 XML 노드에 액세스할 수 있도록 지정된 네임스페이스에 대한 접두사를 생성합니다.
통사론
SimpleXMLElement::registerXPathNamespace(prefix, ns)
매개변수 값
Parameter | Description |
---|---|
prefix | Required. Specifies the namespace prefix to use in the XPath query for the namespace given in ns |
ns | Required. Specifies the namespace to use for the XPath query |
기술적 세부 사항
반환 값: | 성공 시 TRUE입니다. 실패시 FALSE |
---|---|
PHP 버전: | 5.1+ |
❮ PHP SimpleXML 참조