【问题标题】:Print php array in xslt在 xslt 中打印 php 数组
【发布时间】:2009-03-16 21:21:22
【问题描述】:

有没有办法在 xslt 中打印一个 php 数组?我正在使用 php 会话并尝试在 xslt 样式表中打印孔数组。

【问题讨论】:

  • 更具体一点。你到底想做什么?

标签: php xslt session


【解决方案1】:

要在 XSLT 样式表中使用您的数组,您首先必须将您的数组转换为 XML 表示。这种表示很大程度上取决于您的数组结构。一个简单的方法是:

$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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多