In most cases if you expect XML (or XHTML) as output you better use transformToXML() directly. You gain better control over xsl:output attributes, notably omit-xml-declaration.
Instead of :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$dom = $proc->transformToDoc($xml);
echo $dom->saveXML();
do use :
$proc = new XSLTProcessor();
$proc->importStylesheet($xsl);
$newXml = $proc->transformToXML($xml);
echo $newXml;
In the first case, <?xml version="1.0" encoding="utf-8"?> is added whatever you set the omit-xml-declaration while transformToXML() take the attribute into account.
XSLTProcessor::transformToDoc
(PHP 5)
XSLTProcessor::transformToDoc — DOMDocument に変換する
説明
XSLTProcessor::importStylesheet() メソッドで与えられたスタイルシートを適用し、 ソースノードを DOMDocument に変換します。
パラメータ
-
doc -
変換されるノードを指定します。
返り値
結果の DOMDocument 。
エラーが発生した場合は FALSE 。
例
例1 DOMDocument への変換
<?php
// XML ソースをロードする
$xml = new DOMDocument;
$xml->load('collection.xml');
$xsl = new DOMDocument;
$xsl->load('collection.xsl');
// 変換の設定を行う
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // XSL ルールを適用する
echo trim($proc->transformToDoc($xml)->firstChild->wholeText);
?>
上の例の出力は以下となります。
Hey! Welcome to Nicolas Eliaszewicz's sweet CD collection!
franp at free dot fr ¶
6 years ago
