|
| 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 | +--> |
0 commit comments