# DOMXPath::registerPhpFunctionNS

(PHP >= 8.4.0)

DOMXPath::registerPhpFunctionNS — Register a PHP functions as namespaced XPath function

### Beschreibung

public DOMXPath::registerPhpFunctionNS(string `$namespaceURI`, string `$name`, callable `$callable`): void

This method enables the ability to use a PHP function as a namespaced XPath function inside XPath expressions.

### Parameter-Liste

`namespaceURI`
  The URI of the namespace.
`name`
  The local function name inside the namespace.
`callable`
  The PHP function to call when the XPath function gets called within the XPath expression. When a node list is passed as parameter to the callback, they are arrays containing the matched DOM nodes.

### Fehler/Exceptions

- Throws a ValueError if a callback name is not valid.
- Wenn `options` eine ungültige Option enthält, wird ein ValueError ausgelöst.
- Wenn `overrideEncoding` eine unbekannte Kodierung ist, wird ein ValueError ausgelöst.
- Throws a TypeError if a given callback is not callable.

### Rückgabewerte

Es wird kein Wert zurückgegeben.

### Beispiele

Beispiel #1 Register a namespaced XPath function and call it from the XPath expression

`<?php
$xml = <<<EOB
<books>
<book>
<title>PHP Basics</title>
<author>Jim Smith</author>
<author>Jane Smith</author>
</book>
<book>
<title>PHP Secrets</title>
<author>Jenny Smythe</author>
</book>
<book>
<title>XML basics</title>
<author>Joe Black</author>
</book>
</books>
EOB;
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
// Register the my: namespace (required)
$xpath->registerNamespace("my", "urn:my.ns");
// Register PHP function
$xpath->registerPHPFunctionNS(
'urn:my.ns',
'substring',
fn (array $arg1, int $start, int $length) => substr($arg1[0]->textContent, $start, $length)
);
// Call substr function on the book title
$nodes = $xpath->query('//book[my:substring(title, 0, 3) = "PHP"]');
echo "Found {$nodes->length} books starting with 'PHP':\n";
foreach ($nodes as $node) {
$title = $node->getElementsByTagName("title")->item(0)->nodeValue;
$author = $node->getElementsByTagName("author")->item(0)->nodeValue;
echo "$title by $author\n";
}
?>`

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

    Found 2 books starting with 'PHP':
    PHP Basics by Jim Smith
    PHP Secrets by Jenny Smythe

### Siehe auch

- DOMXPath::registerNamespace() - Registers the namespace with the DOMXPath object
- DOMXPath::query() - Evaluates the given XPath expression
- DOMXPath::evaluate() - Evaluates the given XPath expression and returns a typed result if possible
- XSLTProcessor::registerPHPFunctions() - Aktiviert die Nutzbarkeit von PHP-Funktionen als XSLT-Funktionen
- XSLTProcessor::registerPHPFunctionNS() - Register a PHP function as namespaced XSLT function

`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open