función de PHP registerXPathNamespace ()
SimpleXML PHP Manual de Referencia
Ejemplos
Para la próxima consulta XPath para crear un contexto de espacio de nombres:
<?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>";
}
?>
Instancia en ejecución »
Definición y Uso
registerXPathNamespace () la función para la próxima consulta XPath para crear un contexto de espacio de nombres.
Si cambia el prefijo de espacio de nombres en el documento XML, esta función es útil. registerXPathNamespace función () se creará un prefijo de espacio de nombres especificado, los nodos XML afectado puede ser accedido sin cambiar el código de la aplicación en muchas circunstancias.
gramática
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
detalles técnicos
Devuelve: | Si tiene éxito, devuelve TRUE, en caso de error devuelve FALSO. |
---|---|
versión de PHP: | 5.2+ |
SimpleXML PHP Manual de Referencia