2

Is there any way to print a php array in xslt? I'm working with php sessions and trying to print the hole array in the xslt stylesheet.

1
  • Be a bit more specific. What exactly are you trying to do? Commented Mar 16, 2009 at 9:04

1 Answer 1

6

To use your array in a XSLT stylesheet you first have to convert your array into a XML representation. This representation strongly depends on your array-structure. A simple way would be:

$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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.