(PHP 5 >= 5.0.4, PHP 7, PHP 8)
XSLTProcessor::registerPHPFunctions — PHP 関数を XSLT 関数として利用できるようにする
このメソッドは、PHP 関数を XSL スタイルシートでの XSLT 関数として利用できるようにします。
functions のデフォルトは null です。
つまり、デフォルトのままこのメソッドを呼び出すと、
すべての PHP 関数にアクセスできるようになります。
コールバック名が正しくない場合、 ValueError がスローされます。
options が不正なオプションを含む場合、
ValueError をスローします。
overrideEncoding が未知のエンコーディングである場合、
ValueError をスローします。
指定されたコールバックが callable でない場合、 TypeError がスローされます。
値を返しません。
| バージョン | 説明 |
|---|---|
| 8.4.0 | コールバック名が正しくない場合、 ValueError がスローされるようになりました。 callable でない値を指定した場合、 TypeError がスローされるようになりました。 |
| 8.4.0 |
functions を 配列 にする際、
コールバックとして callable を使用できるようになりました。
|
例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:php="http://php.net/xsl">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="allusers">
<html><body>
<h2>Users</h2>
<table>
<xsl:for-each select="user">
<tr><td>
<xsl:value-of
select="php:function('ucfirst',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->registerPHPFunctions();
$proc->importStyleSheet($xsldoc);
echo $proc->transformToXML($xmldoc);
?>