fonction PHP registerXPathNamespace ()
Manuel PHP SimpleXML Référence
Exemples
Pour la prochaine requête XPath pour créer un contexte d'espace de noms:
<?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>";
}
?>
Exécution instance »
Définition et utilisation
fonction registerXPathNamespace () pour la prochaine requête XPath pour créer un contexte d'espace de noms.
Si vous modifiez le préfixe d'espace de noms dans le document XML, cette fonction est utile. registerXPathNamespace function () va créer un préfixe d'espace de noms spécifié, les nœuds XML concernés est accessible sans modifier le code d'application dans de nombreuses circonstances.
grammaire
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
détails techniques
Retours: | En cas de succès, il retourne TRUE, en cas d'échec retourne FALSE. |
---|---|
PHP version: | 5.2+ |
Manuel PHP SimpleXML Référence