简体   繁体   English

将PHP变量作为参数传递给XSL

[英]Pass PHP variable as a parameter to XSL

I have a PHP page that outputs XML by changing the header type and outputting the xml with an XSL stylesheet for an RSS feed: 我有一个PHP页面,它通过更改标头类型并使用RSS提要的XSL样式表输出xml来输出XML:

<?php
header('Content-Type: text/xml; charset=utf-8');
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
echo '<?xml-stylesheet title="XSL_formatting" type="text/xsl" href="/path/feed.xsl"?>' . "\n";
?>
<channel>
    <item>...</item>...
</channel>

I want to pass some PHP variables from the original page to the XSL, how do I do this? 我想将一些PHP变量从原始页面传递到XSL,该怎么做?

Example: I have the variables... 示例:我有变量...

$header = "This is a cool page";
$description = "This is a description";

...that I want to pass to the XSL page and use within it meaning the title can be changed through PHP and dynamically changed in the XSL rather than hard coding it. ...我想传递到XSL页面并在其中使用,这意味着标题可以通过PHP更改,并可以在XSL中动态更改,而不是对其进行硬编码。

It cannot be passed through XML because I am using the XSL as a fallback and therefore the title should not be displayed when the browser supports RSS. 它不能通过XML传递,因为我使用XSL作为后备,因此当浏览器支持RSS时不应显示标题。 Also it must come from that page rather than referencing another file with that variable. 而且它必须来自该页面,而不是使用该变量引用另一个文件。

Something like 就像是

<?xml-stylesheet title="XSL_formatting" type="text/xsl" param-header="<?=$header?>' param-description="<?=$description?>' href="/path/feed.xsl"?>

Let me know if there is a better way to achieve this. 让我知道是否有更好的方法可以实现这一目标。

*UPDATE - This doesn't work because the extra tags don't validate because they aren't part of a namespace. *更新-这是行不通的,因为多余的标签不属于名称空间,因此无法通过验证。


Actually forgot to put the rss tags into the question but when I added XML to store the PHP variables between the rss and channel tags, I could use them in the XSL file dynamically and the XML used to store them doesn't render when viewed in an RSS reader, eg 实际上忘记了将rss标记放入问题中,但是当我添加XML以将PHP变量存储在rss和通道标记之间时,我可以在XSL文件中动态使用它们,而用于存储它们的XML在查看时不呈现。 RSS阅读器,例如

<rss>
    <extraInfo>
        <heading>...</heading>
        <description>...</description>
    </extraInfo>
    <channel>
        ...
    </channel>
</rss>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM