PHP registerXPathNamespace() Functie
Voorbeeld
Maak een naamruimtecontext voor de volgende XPath-query:
<?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>";
}
?>
Definitie en gebruik
De functie registerXPathNamespace() maakt een naamruimtecontext voor de volgende XPath-query.
Deze functie is handig als een naamruimteprefix wordt gewijzigd in een XML-document. De functie registerXPathNamespace() maakt een prefix voor de opgegeven naamruimte, zodat de betrokken XML-knooppunten toegankelijk zijn zonder de toepassingscode te veel te wijzigen.
Syntaxis
SimpleXMLElement::registerXPathNamespace(prefix, ns)
Parameterwaarden
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
Winstwaarde: | WAAR op succes. ONWAAR bij fout |
---|---|
PHP-versie: | 5.1+ |
❮ PHP SimpleXML-referentie