XSLTProcessor::transformToXml

XML に変換する

説明

public stringnullfalse XSLTProcessor::transformToXml(object $document)

xsltprocessor::importStylesheet メソッドで与えられたスタイルシートを適用し、 ソースノードを文字列に変換します。

パラメータ

document

変換される Dom\Document, DOMDocument, SimpleXMLElement、または libxml と互換性があるオブジェクトを指定します。

returnClass

XSLTProcessor::transformToDoc が返すオブジェクトのクラス名を指定する、オプションのパラメータです。ここで指定するクラスは、document と同じものか、それを継承したクラスであるべきです。

エラー / 例外

PHP のコールバックを呼び出す式を使う際は、 以下のエラーが発生する可能性があります。

  • PHP のコールバックを呼び出しているのにコールバックが登録されていなかったり、 名前付きコールバックが登録されていない場合、 Error がスローされます。
  • php:function を使っていて、 ハンドラ名が文字列でない場合、 TypeError がスローされます。
  • DOM でないオブジェクトがコールバックから返された場合、 Error がスローされます。

戻り値

変換結果の文字列。エラーが発生した場合は false

例1 文字列への変換

<?php

// XML ソースをロードする
$xml = new DOMDocument;
$xml->load('collection.xml');

$xsl = new DOMDocument;
$xsl->load('collection.xsl');

// 変換の設定を行う
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // XSL ルールを適用する

echo $proc->transformToXML($xml);

?>

上の例の出力は以下となります。

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

<h1>Fight for your mind</h1><h2>by Ben Harper - 1995</h2><hr>
<h1>Electric Ladyland</h1><h2>by Jimi Hendrix - 1997</h2><hr>

例2 Dom\Document を使って文字列に変換する

<?php

$xml = Dom\XMLDocument::createFromFile('collection.xml');
$xsl = Dom\XMLDocument::createFromFile('collection.xsl');

// 変換の設定を行う
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // XSL ルールを適用する

echo $proc->transformToXML($xml);

?>

上の例の出力は以下となります。

Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!

<h1>Fight for your mind</h1><h2>by Ben Harper - 1995</h2><hr>
<h1>Electric Ladyland</h1><h2>by Jimi Hendrix - 1997</h2><hr>

参考

  • XSLTProcessor::transformToDoc
  • XSLTProcessor::transformToUri