Função PHP registerXPathNamespace()
Exemplo
Crie um contexto de namespace para a próxima consulta 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>";
}
?>
Definição e uso
A função registerXPathNamespace() cria um contexto de namespace para a próxima consulta XPath.
Essa função é útil se um prefixo de namespace for alterado em um documento XML. A função registerXPathNamespace() criará um prefixo para o namespace especificado, para que os nós XML afetados possam ser acessados sem alterar muito o código do aplicativo.
Sintaxe
SimpleXMLElement::registerXPathNamespace(prefix, ns)
Valores de parâmetro
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 |
Detalhes técnicos
Valor de retorno: | VERDADEIRO no sucesso. FALSO em caso de falha |
---|---|
Versão do PHP: | 5.1+ |
❮ Referência PHP SimpleXML