首页 > 解决方案 > 如何在 XSLT 中编写 PHP

问题描述

我一直在尝试在 .xsl 文件中编写一个简单的 php echo 语句,但是当我输入处理指令标签时,我查看的每个站点似乎都对我不起作用。我什至尝试更改 php 的命名空间,但我不确定为什么我的代码没有输出 echo 语句。这是我的 .xsl 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:php="http://php.net/xsl"
    exclude-result-prefixes="php" version="1.0">
    <xsl:output omit-xml-declaration="yes" method="html" />
    <xsl:template match="/">
        <html> 
            <head>
            </head>
            <body bgcolor="#000">
                    <div class="main" style="background:lightblue;">
                        <xsl:processing-instruction name="php">
                        echo 'OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!';
                        </xsl:processing-instruction>
                        <xsl:for-each select="webpage/content/main">
                            <a href="{link}" style="color:#000; text-decoration:none;">
                                <h1><xsl:value-of select="heading" /></h1>
                                <h2><xsl:value-of select="subheading" /></h2>
                                <div class="img">
                                    <img src="../images/{image}" width="100%" height="auto"/>
                                </div>
                                <xsl:value-of select="description" />
                            </a>
                        </xsl:for-each>
                    </div>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

我还尝试使用以下标签来包围 html 标签,然后在其间的任何地方使用处理指令标签:

<xsl:result-document href="example.php" method="html">
</xsl:result-document>

并且仍然没有运气。有人可以告诉我我做错了什么吗?

标签: phpxmlxslt

解决方案


XSLT 和序列化规范(所有版本)说这应该产生输出<?php echo 'OK IT DOESN't WORK!!!!!!!!!!!!!!!!!!!';>(加上一些空格)。这实际上没有用。由于难以回忆的历史原因,它被这样定义。HTML5 对处理指令没有任何意义,它们以对 PHP 甚至没有用的格式进行序列化。

我不知道你为什么没有看到这个输出,但这个问题有点学术,因为正确的输出无论如何对你没有用。

如果我没记错的话,XML 输出方法对于生成 PHP 脚本也不是很有用,因为 PHP 在一般情况下不是格式良好的 XML。


推荐阅读