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>";
}
?>
$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>";
}
?>
»인스턴스를 실행
정의 및 사용
네임 스페이스 컨텍스트를 만들 수있는 다음 XPath 쿼리에 대한 registerXPathNamespace () 함수.
당신은 XML 문서의 네임 스페이스 접두사를 변경하는 경우,이 기능이 유용합니다. 특정 네임 스페이스 접두어를 만들 registerXPathNamespace () 함수는 해당 XML 노드는 여러 상황에서 응용 프로그램 코드를 변경하지 않고 액세스 될 수있다.
문법
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
기술 세부 사항
반환 값 : | 이 TRUE를 반환 성공하면, 실패 할 경우 FALSE를 반환합니다. |
---|---|
PHP 버전 : | 5.2 이상 |
PHP SimpleXML을 참조 설명서