XSLTProcessor::registerPHPFunctions
PHP 関数を XSLT 関数として利用できるようにする
説明
public void XSLTProcessor::registerPHPFunctions(arraystringnull $functions = null)
このメソッドは、PHP 関数を XSL スタイルシートでの XSLT
関数として利用できるようにします。
警告
functions のデフォルトは null です。
つまり、デフォルトのままこのメソッドを呼び出すと、
すべての PHP 関数にアクセスできるようになります。
パラメータ
-
functions
-
このパラメータは、XSLT からコールされる信頼できる関数のみを許可します。
このパラメータは、以下のいずれかになります:
null (すべての関数を許可する),
string (関数名),
関数名を値に持つ数値添字の array,
関数名がキーで callable な値を持つ連想配列。
エラー / 例外
-
コールバック名が正しくない場合、
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: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);
?>
参考
- DOMXPath::registerPhpFunctions