PHP registerXPathNamespace () Funktion
PHP SimpleXML Reference Manual
Beispiele
Für die nächste XPath-Abfrage, um einen Namespace Kontext zu erstellen:
<?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>";
}
?>
Ausführung einer Instanz »
Definition und Verwendung
registerXPathNamespace () Funktion für den nächsten XPath-Abfrage einen Namespace-Kontext zu erstellen.
Wenn Sie das Namespacepräfix im XML-Dokument ändern, ist diese Funktion nützlich. registerXPathNamespace () Funktion wird ein Namespacepräfix angegeben, werden die betroffenen XML-Knoten erstellen kann ohne Änderung der Anwendungscode unter vielen Umständen zugegriffen werden.
Grammatik
registerXPathNamespace( prefix , ns );
参数 | 描述 |
---|---|
prefix | 必需。规定在 ns 指定的命名空间的 XPath 查询中使用的的命名空间前缀。 |
ns | 必需。规定用于 XPath 查询的命名空间。 |
Technische Details
Rückkehr: | Wenn dies gelingt es TRUE zurück, bei einem Fehler FALSCH. |
---|---|
PHP-Version: | 5.2+ |
PHP SimpleXML Reference Manual