From 56778878f60814271db1cfa9a61638c48ab52ae8 Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Sat, 22 Feb 2025 04:25:46 +0100 Subject: [PATCH] Add XSLTProcessor::registerPHPFunctionNS and DOMXPath::registerPhpFunctionNS (#4129) Co-authored-by: Gina Peter Banyard --- .../dom/domxpath/registerphpfunctionns.xml | 158 ++++++++++++++++++ reference/dom/versions.xml | 1 + reference/xsl/versions.xml | 1 + .../xsltprocessor/registerphpfunctionns.xml | 144 ++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 reference/dom/domxpath/registerphpfunctionns.xml create mode 100644 reference/xsl/xsltprocessor/registerphpfunctionns.xml diff --git a/reference/dom/domxpath/registerphpfunctionns.xml b/reference/dom/domxpath/registerphpfunctionns.xml new file mode 100644 index 000000000000..238a8e4333a2 --- /dev/null +++ b/reference/dom/domxpath/registerphpfunctionns.xml @@ -0,0 +1,158 @@ + + + + + DOMXPath::registerPhpFunctionNS + Register a PHP functions as namespaced XPath function + + + + &reftitle.description; + + public voidDOMXPath::registerPhpFunctionNS + stringnamespaceURI + stringname + callablecallable + + + This method enables the ability to use a PHP function as a namespaced XPath function + inside XPath expressions. + + + + + + &reftitle.parameters; + + + 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. + + + + + + + + &reftitle.returnvalues; + + &return.void; + + + + + &reftitle.examples; + + Register a namespaced XPath function and call it from the XPath expression + + + + PHP Basics + Jim Smith + Jane Smith + + + PHP Secrets + Jenny Smythe + + + XML basics + Joe Black + + +EOB; + +$doc = new DOMDocument; +$doc->loadXML($xml); + +$xpath = new DOMXPath($doc); + +// Register the my: namespace (required) +$xpath->registerNamespace("my", "urn:my.ns"); + +// Register PHP functions (no restrictions) +$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"; +} + +?> +]]> + + &example.outputs.similar; + + + + + + + + &reftitle.seealso; + + DOMXPath::registerNamespace + DOMXPath::query + DOMXPath::evaluate + XSLTProcessor::registerPHPFunctions + XSLTProcessor::registerPHPFunctionNS + + + + + diff --git a/reference/dom/versions.xml b/reference/dom/versions.xml index 579cebf14e6b..f7c108cff5ee 100644 --- a/reference/dom/versions.xml +++ b/reference/dom/versions.xml @@ -197,6 +197,7 @@ + diff --git a/reference/xsl/versions.xml b/reference/xsl/versions.xml index a563302730aa..b35a11ff455a 100644 --- a/reference/xsl/versions.xml +++ b/reference/xsl/versions.xml @@ -11,6 +11,7 @@ + diff --git a/reference/xsl/xsltprocessor/registerphpfunctionns.xml b/reference/xsl/xsltprocessor/registerphpfunctionns.xml new file mode 100644 index 000000000000..89f9334f1cc3 --- /dev/null +++ b/reference/xsl/xsltprocessor/registerphpfunctionns.xml @@ -0,0 +1,144 @@ + + + + + XSLTProcessor::registerPHPFunctionNS + Register a PHP function as namespaced XSLT function + + + &reftitle.description; + + public voidXSLTProcessor::registerPHPFunctionNS + stringnamespaceURI + stringname + callablecallable + + + This method enables the ability to use a PHP function as a namespaced XSLT functions within XSL stylesheets. + + + + + &reftitle.parameters; + + + namespaceURI + + + The URI of the namespace. + + + + + name + + + The local function name inside the namespace. + + + + + callable + + + The PHP function to call when the XSL function gets called within the stylesheet. + When a node list is passed as parameter to the callback, + the argument becomes an array containing the matched dom nodes. + + + + + + + + &reftitle.returnvalues; + + &return.void; + + + + + &reftitle.examples; + + Simple PHP Function call from a stylesheet + + + + bob + + + joe + + +EOB; +$xsl = << + + + + +

users

+ + + + + + +
+ +
+ +
+
+EOB; +$xmldoc = new DOMDocument(); +$xmldoc->loadXML($xml); +$xsldoc = new DOMDocument(); +$xsldoc->loadXML($xsl); + +$proc = new XSLTProcessor(); +$proc->registerPHPFunctionNS('urn:my.ns', 'uppercase', strtoupper(...)); +$proc->registerPHPFunctionNS('urn:my.ns', 'count', fn (array $arg1) => count($arg1)); +$proc->importStyleSheet($xsldoc); +echo $proc->transformToXML($xmldoc); +?> +]]> +
+
+
+ + + &reftitle.seealso; + + DOMXPath::registerPhpFunctionNS + DOMXPath::registerPhpFunctions + XSLTProcessor::registerPhpFunctions + + + +
+