2009-03-16 6 views
2

Gibt es eine Möglichkeit, ein PHP-Array in xslt zu drucken? Ich arbeite mit PHP-Sitzungen und versuche, das Loch-Array im XSLT-Stylesheet zu drucken.Drucken PHP-Array in xslt

+0

Seien Sie ein bisschen spezifischer. Was genau versuchst du zu tun? – vartec

Antwort

6

Um Ihr Array in einem XSLT-Stylesheet zu verwenden, müssen Sie zuerst Ihr Array in eine XML-Repräsentation konvertieren. Diese Darstellung hängt stark von Ihrer Array-Struktur ab. Ein einfacher Weg wäre:

$array = (
    'key1' => 'value1', 
    'key2' => 'value2', 
    'key3' => 'value3' 
); 

// ext/DOM can also be used to create XML representation 
$xml = new XMLWriter(); 
$xml->openMemory(); 
$xml->startDocument('1.0', 'UTF-8'); 
$xml->startElement('array'); 
// simple one-dimensional array-traversal - depending on your array structure this can be much more complicated (e.g. recursion) 
foreach ($array as $key => $value) { 
     $xml->writeElement($key, $value); 
} 
$xml->endElement(); 

/* 
* $xml will look like 
* <array> 
*  <key1>value1</key1> 
*  <key2>value2</key2> 
*  <key3>value3</key3> 
* </array> 
*/ 

// convert XMLWriter document into a DOM representation (can be skipped if XML is created with ext/DOM) 
$doc = DOMDocument::loadXML($xml->outputMemory()); 

// Load XSL stylesheet 
$xsl = DOMDocument::load('stylesheet.xsl'); 

// Fire-up XSLT processor 
$proc = new XSLTProcessor(); 
$proc->importStyleSheet($xsl); 

// Output transformation 
echo $proc->transformToXML($xml);