xslt_process
(PHP 4 >= 4.0.3)
xslt_process -- XSLデータを有する文字列によりXMLデータを変換する
説明
mixed
xslt_process ( resource xh, string xml, string xsl [, string result [, array arguments [, array parameters]]])
xslt_process()関数は、新規XSLT拡張の中心関数で
す。引数バッファの概念により、ほとんど全ての型の入力ソースを用い
てXSLT変換を実行可能です。引数バッファは、Sablotron XSLTプロセッ
サ(現在、この拡張モジュールがサポートする惟一のXSLTプロセッサ)か
ら得た概念です。
xslt_process()関数で変換する最も簡単な方法は、
XMLファイルをXSLTファイルで変換し、結果を新しいXMLドキュメント(ま
たはHTMLドキュメント)を含む3番目のファイルに出力することです。
これをsablotronを行うのは、かなり簡単です。
例 1.
XMLファイルとXSLファイルを新規XMLファイルに変換するために
xslt_process()を使用する
<?php
// Allocate a new XSLT processor $xh = xslt_create();
// Process the document if (xslt_process($xh, 'sample.xml', 'sample.xsl', 'result.xml')) { print "SUCCESS, sample.xml was transformed by sample.xsl into result.xml"; print ", result.xml has the following contents\n<br>\n"; print "<pre>\n"; readfile('result.xml'); print "</pre>\n"; } else { print "Sorry, sample.xml could not be transformed by sample.xsl into"; print " result.xml the reason is that " . xslt_error($xh) . " and the "; print "error code is " . xslt_errno($xh); }
xslt_free($xh);
?>
|
|
この機能は優れていますが、特にWeb環境では、結果を直接出力したい場
合があります。そこで、xslt_process()の3番目の
引数を省略した場合(またはその引数にNULL値を指定した場合)、ファイ
ルに書き込む替わりに自動的にXSLT変換後の出力を返します。
例 2.
XMLファイルとXSLファイルを結果XMLデータを含む変数に変換するため
にxslt_process()を使用する
<?php
// Allocate a new XSLT processor $xh = xslt_create();
// Process the document, returning the result into the $result variable $result = xslt_process($xh, 'sample.xml', 'sample.xsl'); if ($result) { print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result"; print " variable, the \$result variable has the following contents\n<br>\n"; print "<pre>\n"; print $result; print "</pre>\n"; } else { print "Sorry, sample.xml could not be transformed by sample.xsl into"; print " the \$result variable the reason is that " . xslt_error($xh) . print " and the error code is " . xslt_errno($xh); }
xslt_free($xh);
?>
|
|
上の二つのケースは、XSLT変換の最も簡単な場合です。これは、多くの
場合には通用しますが、時々、データベースまたはソケットのような外
部ソースからXMLとXSLTコードを取得する場合があります。このような場
合、XMLまたはXSLTデータを変数に有することになります。
実用アプリケーションでは、これらをファイルにダンプする際のオーバー
ヘッドは大きいと言えます。このような場合こそ、XSLT
"argument" 構文が役に立ちます。
xslt_process()関数のXMLおよびXSLT引数としてファ
イルの替わりに引数配列(xslt_process()関数の5番
目のパラメータ)で指定した値に置換される"argument place
holders"を指定することが可能です。
以下にファイルを全く使用せずにXMLおよびXSLTを結果変数に処理する例
を示します。
例 3.
XMLデータを含む変数とXSLTデータを含む変数をXMLデータ出力結果を
含む変数に変換するためにxslt_process()を使用
する
<?php // $xml and $xsl contain the XML and XSL data
$arguments = array( '/_xml' => $xml, '/_xsl' => $xsl );
// Allocate a new XSLT processor $xh = xslt_create();
// Process the document $result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments); if ($result) { print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result"; print " variable, the \$result variable has the following contents\n<br>\n"; print "<pre>\n"; print $result; print "</pre>\n"; } else { print "Sorry, sample.xml could not be transformed by sample.xsl into"; print " the \$result variable the reason is that " . xslt_error($xh) . print " and the error code is " . xslt_errno($xh); } xslt_free($xh); ?>
|
|
最後に、xslt_process()関数の最後の引数は、XSLT
ドキュメントに渡したいあらゆる引数です。これらのパラメータは、
<xsl:param name="parameter_name">命令を用いて
XSLファイルの中でアクセスすることが可能です。