CakeFest 2024: The Official CakePHP Conference

XSLTProcessor::transformToDoc

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::transformToDocドキュメント に変換する

説明

public XSLTProcessor::transformToDoc(object $document, ?string $returnClass = null): DOMDocument|false

XSLTProcessor::importStylesheet() メソッドで与えられたスタイルシートを適用し、 ソースノードをドキュメント (例: DOMDocument) に変換します。

パラメータ

document

変換される DOMDocumentSimpleXMLElement、または libxml と互換性があるオブジェクトを指定します。

returnClass

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

戻り値

結果の ドキュメント を返します。 エラーが発生した場合は 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!

参考

add a note

User Contributed Notes 1 note

up
1
franp at free dot fr
17 years ago
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.
To Top