PHP 8.3.4 Released!

XSLTProcessor::transformToDoc

(PHP 5, PHP 7, PHP 8)

XSLTProcessor::transformToDocTransforma em um documento

Descrição

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

Transforma o nó de origem em um documento (ex.: DOMDocument) aplicando a folha de estilos fornecida pelo método XSLTProcessor::importStylesheet().

Parâmetros

document

O objeto DOMDocument, SimpleXMLElement, ou outro compatível com libxml a ser transformado.

returnClass

Este parâmetro opcional pode ser usado para que XSLTProcessor::transformToDoc() retorne um objeto da classe especificada. Esta classe deve estender ou ser da mesma classe de document.

Valor Retornado

O documento resultante ou false em caso de erro.

Exemplos

Exemplo #1 Transformando em um DOMDocument

<?php

// Carrega a fonte XML
$xml = new DOMDocument;
$xml->load('collection.xml');

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

// Configura o transformador
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // anexa as regras xsl

echo trim($proc->transformToDoc($xml)->firstChild->wholeText);

?>

O exemplo acima produzirá:

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

Veja Também

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