Skip to content

Commit 5677887

Browse files
Add XSLTProcessor::registerPHPFunctionNS and DOMXPath::registerPhpFunctionNS (#4129)
Co-authored-by: Gina Peter Banyard <[email protected]>
1 parent 1d4f5d1 commit 5677887

File tree

4 files changed

+304
-0
lines changed

4 files changed

+304
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<refentry xml:id="domxpath.registerphpfunctionns" xmlns="http://docbook.org/ns/docbook">
4+
<refnamediv>
5+
<refname>DOMXPath::registerPhpFunctionNS</refname>
6+
<refpurpose>Register a PHP functions as namespaced XPath function</refpurpose>
7+
</refnamediv>
8+
9+
<refsect1 role="description">
10+
&reftitle.description;
11+
<methodsynopsis role="DOMXPath">
12+
<modifier>public</modifier> <type>void</type><methodname>DOMXPath::registerPhpFunctionNS</methodname>
13+
<methodparam><type>string</type><parameter>namespaceURI</parameter></methodparam>
14+
<methodparam><type>string</type><parameter>name</parameter></methodparam>
15+
<methodparam><type>callable</type><parameter>callable</parameter></methodparam>
16+
</methodsynopsis>
17+
<simpara>
18+
This method enables the ability to use a PHP function as a namespaced XPath function
19+
inside XPath expressions.
20+
</simpara>
21+
22+
</refsect1>
23+
24+
<refsect1 role="parameters">
25+
&reftitle.parameters;
26+
<variablelist>
27+
<varlistentry>
28+
<term><parameter>namespaceURI</parameter></term>
29+
<listitem>
30+
<simpara>
31+
The URI of the namespace.
32+
</simpara>
33+
</listitem>
34+
</varlistentry>
35+
<varlistentry>
36+
<term><parameter>name</parameter></term>
37+
<listitem>
38+
<simpara>
39+
The local function name inside the namespace.
40+
</simpara>
41+
</listitem>
42+
</varlistentry>
43+
<varlistentry>
44+
<term><parameter>callable</parameter></term>
45+
<listitem>
46+
<simpara>
47+
The PHP function to call when the XPath function gets called within the XPath expression.
48+
When a node list is passed as parameter to the callback,
49+
they are arrays containing the matched DOM nodes.
50+
</simpara>
51+
</listitem>
52+
</varlistentry>
53+
</variablelist>
54+
</refsect1>
55+
56+
<refsect1 role="returnvalues">
57+
&reftitle.returnvalues;
58+
<simpara>
59+
&return.void;
60+
</simpara>
61+
</refsect1>
62+
63+
<refsect1 role="examples">
64+
&reftitle.examples;
65+
<example>
66+
<title>Register a namespaced XPath function and call it from the XPath expression</title>
67+
<programlisting role="php">
68+
<![CDATA[
69+
<?php
70+
$xml = <<<EOB
71+
<books>
72+
<book>
73+
<title>PHP Basics</title>
74+
<author>Jim Smith</author>
75+
<author>Jane Smith</author>
76+
</book>
77+
<book>
78+
<title>PHP Secrets</title>
79+
<author>Jenny Smythe</author>
80+
</book>
81+
<book>
82+
<title>XML basics</title>
83+
<author>Joe Black</author>
84+
</book>
85+
</books>
86+
EOB;
87+
88+
$doc = new DOMDocument;
89+
$doc->loadXML($xml);
90+
91+
$xpath = new DOMXPath($doc);
92+
93+
// Register the my: namespace (required)
94+
$xpath->registerNamespace("my", "urn:my.ns");
95+
96+
// Register PHP functions (no restrictions)
97+
$xpath->registerPHPFunctionNS(
98+
'urn:my.ns',
99+
'substring',
100+
fn (array $arg1, int $start, int $length) => substr($arg1[0]->textContent, $start, $length)
101+
);
102+
103+
// Call substr function on the book title
104+
$nodes = $xpath->query('//book[my:substring(title, 0, 3) = "PHP"]');
105+
106+
echo "Found {$nodes->length} books starting with 'PHP':\n";
107+
foreach ($nodes as $node) {
108+
$title = $node->getElementsByTagName("title")->item(0)->nodeValue;
109+
$author = $node->getElementsByTagName("author")->item(0)->nodeValue;
110+
echo "$title by $author\n";
111+
}
112+
113+
?>
114+
]]>
115+
</programlisting>
116+
&example.outputs.similar;
117+
<screen>
118+
<![CDATA[
119+
Found 2 books starting with 'PHP':
120+
PHP Basics by Jim Smith
121+
PHP Secrets by Jenny Smythe
122+
]]>
123+
</screen>
124+
</example>
125+
</refsect1>
126+
127+
<refsect1 role="seealso">
128+
&reftitle.seealso;
129+
<simplelist>
130+
<member><methodname>DOMXPath::registerNamespace</methodname></member>
131+
<member><methodname>DOMXPath::query</methodname></member>
132+
<member><methodname>DOMXPath::evaluate</methodname></member>
133+
<member><methodname>XSLTProcessor::registerPHPFunctions</methodname></member>
134+
<member><methodname>XSLTProcessor::registerPHPFunctionNS</methodname></member>
135+
</simplelist>
136+
</refsect1>
137+
138+
</refentry>
139+
<!-- Keep this comment at the end of the file
140+
Local variables:
141+
mode: sgml
142+
sgml-omittag:t
143+
sgml-shorttag:t
144+
sgml-minimize-attributes:nil
145+
sgml-always-quote-attributes:t
146+
sgml-indent-step:1
147+
sgml-indent-data:t
148+
indent-tabs-mode:nil
149+
sgml-parent-document:nil
150+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
151+
sgml-exposed-tags:nil
152+
sgml-local-catalogs:nil
153+
sgml-local-ecat-files:nil
154+
End:
155+
vim600: syn=xml fen fdm=syntax fdl=2 si
156+
vim: et tw=78 syn=sgml
157+
vi: ts=1 sw=1
158+
-->

reference/dom/versions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@
197197
<function name="domxpath::query" from="PHP 5, PHP 7, PHP 8"/>
198198
<function name="domxpath::registernamespace" from="PHP 5, PHP 7, PHP 8"/>
199199
<function name="domxpath::registerphpfunctions" from="PHP 5 &gt;= 5.3.0, PHP 7, PHP 8"/>
200+
<function name="domxpath::registerphpfunctionns" from="PHP &gt;= 8.4.0"/>
200201
<function name="domxpath::quote" from="PHP 8 &gt;= 8.4.0"/>
201202

202203
<function name="dom_import_simplexml" from="PHP 5, PHP 7, PHP 8"/>

reference/xsl/versions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<function name="XSLTProcessor::hasExsltSupport" from="PHP 5 &gt;= 5.0.4, PHP 7, PHP 8"/>
1212
<function name="XSLTProcessor::importStylesheet" from="PHP 5, PHP 7, PHP 8"/>
1313
<function name="XSLTProcessor::registerPhpFunctions" from="PHP 5 &gt;= 5.0.4, PHP 7, PHP 8"/>
14+
<function name="XSLTProcessor::registerPhpFunctionNS" from="PHP &gt;= 8.4.0"/>
1415
<function name="XSLTProcessor::removeParameter" from="PHP 5, PHP 7, PHP 8"/>
1516
<function name="XSLTProcessor::setParameter" from="PHP 5, PHP 7, PHP 8"/>
1617
<function name="XSLTProcessor::setProfiling" from="PHP &gt;= 5.3.0, PHP 7, PHP 8"/>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- $Revision$ -->
3+
<refentry xml:id="xsltprocessor.registerphpfunctionns" xmlns="http://docbook.org/ns/docbook">
4+
<refnamediv>
5+
<refname>XSLTProcessor::registerPHPFunctionNS</refname>
6+
<refpurpose>Register a PHP function as namespaced XSLT function</refpurpose>
7+
</refnamediv>
8+
<refsect1 role="description">
9+
&reftitle.description;
10+
<methodsynopsis role="XSLTProcessor">
11+
<modifier>public</modifier> <type>void</type><methodname>XSLTProcessor::registerPHPFunctionNS</methodname>
12+
<methodparam><type>string</type><parameter>namespaceURI</parameter></methodparam>
13+
<methodparam><type>string</type><parameter>name</parameter></methodparam>
14+
<methodparam><type>callable</type><parameter>callable</parameter></methodparam>
15+
</methodsynopsis>
16+
<simpara>
17+
This method enables the ability to use a PHP function as a namespaced XSLT functions within XSL stylesheets.
18+
</simpara>
19+
</refsect1>
20+
21+
<refsect1 role="parameters">
22+
&reftitle.parameters;
23+
<variablelist>
24+
<varlistentry>
25+
<term><parameter>namespaceURI</parameter></term>
26+
<listitem>
27+
<simpara>
28+
The URI of the namespace.
29+
</simpara>
30+
</listitem>
31+
</varlistentry>
32+
<varlistentry>
33+
<term><parameter>name</parameter></term>
34+
<listitem>
35+
<simpara>
36+
The local function name inside the namespace.
37+
</simpara>
38+
</listitem>
39+
</varlistentry>
40+
<varlistentry>
41+
<term><parameter>callable</parameter></term>
42+
<listitem>
43+
<simpara>
44+
The PHP function to call when the XSL function gets called within the stylesheet.
45+
When a node list is passed as parameter to the callback,
46+
the argument becomes an array containing the matched dom nodes.
47+
</simpara>
48+
</listitem>
49+
</varlistentry>
50+
</variablelist>
51+
</refsect1>
52+
53+
<refsect1 role="returnvalues">
54+
&reftitle.returnvalues;
55+
<simpara>
56+
&return.void;
57+
</simpara>
58+
</refsect1>
59+
60+
<refsect1 role="examples">
61+
&reftitle.examples;
62+
<example>
63+
<title>Simple PHP Function call from a stylesheet</title>
64+
<programlisting role="php">
65+
<![CDATA[
66+
<?php
67+
$xml = <<<EOB
68+
<allusers>
69+
<user>
70+
<uid>bob</uid>
71+
</user>
72+
<user>
73+
<uid>joe</uid>
74+
</user>
75+
</allusers>
76+
EOB;
77+
$xsl = <<<EOB
78+
<?xml version="1.0" encoding="UTF-8"?>
79+
<xsl:stylesheet version="1.0"
80+
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
81+
xmlns:my="urn:my.ns">
82+
<xsl:output method="html" encoding="utf-8" indent="yes"/>
83+
<xsl:template match="allusers">
84+
<html><body>
85+
<h2><xsl:value-of select="my:count(user/uid)" /> users</h2>
86+
<table>
87+
<xsl:for-each select="user">
88+
<tr>
89+
<td>
90+
<xsl:value-of select="my:uppercase(string(uid))"/>
91+
</td>
92+
</tr>
93+
</xsl:for-each>
94+
</table>
95+
</body></html>
96+
</xsl:template>
97+
</xsl:stylesheet>
98+
EOB;
99+
$xmldoc = new DOMDocument();
100+
$xmldoc->loadXML($xml);
101+
$xsldoc = new DOMDocument();
102+
$xsldoc->loadXML($xsl);
103+
104+
$proc = new XSLTProcessor();
105+
$proc->registerPHPFunctionNS('urn:my.ns', 'uppercase', strtoupper(...));
106+
$proc->registerPHPFunctionNS('urn:my.ns', 'count', fn (array $arg1) => count($arg1));
107+
$proc->importStyleSheet($xsldoc);
108+
echo $proc->transformToXML($xmldoc);
109+
?>
110+
]]>
111+
</programlisting>
112+
</example>
113+
</refsect1>
114+
115+
<refsect1 role="seealso">
116+
&reftitle.seealso;
117+
<simplelist>
118+
<member><methodname>DOMXPath::registerPhpFunctionNS</methodname></member>
119+
<member><methodname>DOMXPath::registerPhpFunctions</methodname></member>
120+
<member><methodname>XSLTProcessor::registerPhpFunctions</methodname></member>
121+
</simplelist>
122+
</refsect1>
123+
124+
</refentry>
125+
<!-- Keep this comment at the end of the file
126+
Local variables:
127+
mode: sgml
128+
sgml-omittag:t
129+
sgml-shorttag:t
130+
sgml-minimize-attributes:nil
131+
sgml-always-quote-attributes:t
132+
sgml-indent-step:1
133+
sgml-indent-data:t
134+
indent-tabs-mode:nil
135+
sgml-parent-document:nil
136+
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
137+
sgml-exposed-tags:nil
138+
sgml-local-catalogs:nil
139+
sgml-local-ecat-files:nil
140+
End:
141+
vim600: syn=xml fen fdm=syntax fdl=2 si
142+
vim: et tw=78 syn=sgml
143+
vi: ts=1 sw=1
144+
-->

0 commit comments

Comments
 (0)