PHP registerXPathNamespace()Funktionen
PHP SimpleXML Referenzhandbuch
Beispiele
Erstelle einen Namespace-Kontext für die nächste XPath-Abfrage:
<?php
$xml=<<<XML
<book xmlns:chap="http://example.org/chapter-title">
<title>Mein Buch</title>
<chapter id="1">
<chap:title>Kapitel 1</chap:title>
<para>Donec velit. Nullam eget tellus...</para>
</chapter>
<chapter id="2">
<chap:title>Kapitel 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>Mein Buch</title>
<chapter id="1">
<chap:title>Kapitel 1</chap:title>
<para>Donec velit. Nullam eget tellus...</para>
</chapter>
<chapter id="2">
<chap:title>Kapitel 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>";
}
?>
Beispiel ausführen »
Definition und Verwendung
Die Funktion registerXPathNamespace() erstellt einen Namespace-Kontext für die nächste XPath-Abfrage.
Diese Funktion ist nützlich, wenn das Namespace-Präfix in einem XML-Dokument geändert wird. Die Funktion registerXPathNamespace() erstellt ein angegebenes Namespace-Präfix, wodurch auf betroffene XML-Knoten zugegriffen werden kann, ohne den Anwendungscode allzu stark zu ändern.
Syntax
registerXPathNamespace(prefix,ns);
| Parameters | Beschreibung |
|---|---|
| Präfix | Erforderlich. Gibt dasnsNamespace-Präfix an, das in der XPath-Abfrage für den angegebenen Namespace verwendet wird. |
| ns | Erforderlich. Gibt den Namespace an, der für die XPath-Abfrage verwendet wird. |
Technische Details
| Rückgabewert: | Gibt bei Erfolg TRUE zurück, bei Fehlschlag FALSE. |
|---|---|
| PHP-Version: | 5.2+ |
PHP SimpleXML Referenzhandbuch Andere Erweiterungen