PHP - Funktion registerXPathNamespace()
Beispiel
Erstellen Sie einen Namespace-Kontext für die nächste XPath-Abfrage:
<?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>";
}
?>
Definition und Verwendung
Die Funktion registerXPathNamespace() erstellt einen Namespace-Kontext für die nächste XPath-Abfrage.
Diese Funktion ist nützlich, wenn ein Namespace-Präfix in einem XML-Dokument geändert wird. Die Funktion registerXPathNamespace() erstellt ein Präfix für den angegebenen Namespace, sodass auf die betroffenen XML-Knoten zugegriffen werden kann, ohne den Anwendungscode zu sehr zu ändern.
Syntax
SimpleXMLElement::registerXPathNamespace(prefix, ns)
Parameterwerte
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 |
Technische Details
Rückgabewert: | TRUE auf Erfolg. FALSE bei Fehler |
---|---|
PHP-Version: | 5.1+ |
❮ PHP-SimpleXML-Referenz