função PHP registerXPathNamespace ()
Manual do PHP SimpleXML Referência
Exemplos
Para a próxima consulta XPath para criar um contexto de namespace:
<?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>";
}
?>
Correndo instância »
Definição e Uso
registerXPathNamespace () função para a próxima consulta XPath para criar um contexto de namespace.
Se você alterar o prefixo de namespace no documento XML, esta função é útil. função registerXPathNamespace () irá criar um prefixo de namespace especificado, os nós XML afetadas pode ser acessado sem alterar o código do aplicativo em muitas circunstâncias.
gramática
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
detalhes técnicos
retorna: | Se bem sucedida, retorna TRUE, em caso de falha retorna FALSE. |
---|---|
versão do PHP: | 5.2+ |
Manual do PHP SimpleXML Referência