XSLTProcessor::registerPHPFunctionNS

PHP の関数を、名前空間付きの XSLT 関数として登録する

説明

public void XSLTProcessor::registerPHPFunctionNS(string $namespaceURI, string $name, callable $callable)

このメソッドは、PHP の関数を名前空間付きの XSLT 関数として、 XSL スタイルシート中で使えるようにします。

パラメータ

namespaceURI
名前空間の URI
name
名前空間の中での、ローカルな関数名
callable
スタイルシート中で XSL 関数が呼ばれたときにコールされる PHP の関数。 ノードリストが コールバック のパラメータとして渡された場合、 ノードリストはマッチした DOM ノードを含む配列になります。

エラー / 例外

  • コールバック名が正しくない場合、   ValueError がスローされます。
  • options が不正なオプションを含む場合、 ValueError をスローします。
  • overrideEncoding が未知のエンコーディングである場合、 ValueError をスローします。
  • 指定されたコールバックが callable でない場合、 TypeError がスローされます。

戻り値

値を返しません。

例1 スタイルシートから簡単な PHP 関数をコールする例

<?php
$xml = <<<EOB
<allusers>
<user>
 <uid>bob</uid>
</user>
<user>
 <uid>joe</uid>
</user>
</allusers>
EOB;
$xsl = <<<EOB
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:my="urn:my.ns">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="allusers">
 <html><body>
   <h2><xsl:value-of select="my:count(user/uid)" /> users</h2>
   <table>
   <xsl:for-each select="user">
     <tr>
      <td>
       <xsl:value-of select="my:uppercase(string(uid))"/>
      </td>
     </tr>
   </xsl:for-each>
   </table>
 </body></html>
</xsl:template>
</xsl:stylesheet>
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);
?>

参考

  • DOMXPath::registerPhpFunctionNS
  • DOMXPath::registerPhpFunctions
  • XSLTProcessor::registerPhpFunctions