如何使用处理指令php设置xsl属性

时间:2013-06-15 12:25:14

标签: php html xml xslt xml-parsing

我在使用PHP处理指令尝试将值设置到属性中时遇到了问题:

XSLT

<li itemprop="startDate">
    <xsl:attribute name="content">
        <xsl:processing-instruction name="php">
            echo "Monday";
        ?</xsl:processing-instruction>
    </xsl:attribute>
    Monday
</li>

页面呈现正常,但属性始终为空。

输出

<li itemprop="startDate" content="">Monday</li>

我期待PHP在属性

中回显一个值

2 个答案:

答案 0 :(得分:2)

如果您使用PHP通过XSLT转换XML,则可以在php中使用:

$proc->setParameter(null, 'day', 'Monday');

$proc->transformToXML($xml);

然后在你的XSLT中使用这个变量:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
exclude-result-prefixes="php"
xsl:extension-element-prefixes="php">

<xsl:param name="day"/> <!-- Set the parameter -->

<xsl:attribute name='content'>
    <xsl:value-of select="$day"/>
</xsl:attribute>

一切顺利!

答案 1 :(得分:0)

您没有说明如何打开XML。但由于回声,我认为它可能/应该有php指令包含。

xsl:processing-instruction在这里没有意义。试试这个:

<li itemprop="startDate">
    <xsl:attribute name="content">
        <?php
            echo "Monday";
        ?>
    </xsl:attribute>
    Monday
</li>