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

时间:2021-12-17 11:49:40

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页面,它通过更改标题类型输出XML,并使用XSL样式表为RSS提要输出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. Also it must come from that page rather than referencing another file with that variable.

它不能通过XML传递,因为我使用XSL作为后备,因此当浏览器支持RSS时不应显示标题。此外,它必须来自该页面,而不是使用该变量引用另一个文件。

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.

让我知道是否有更好的方法来实现这一目标。

1 个解决方案

#1


0  

*UPDATE - This doesn't work because the extra tags don't validate because they aren't part of a namespace.

* UPDATE - 这不起作用,因为额外的标签不验证,因为它们不是命名空间的一部分。


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, e.g.

实际上忘了把rss标签放到问题中但是当我添加XML来存储rss和channel标签之间的PHP变量时,我可以动态地在XSL文件中使用它们,并且用于存储它们的XML在查看时不会呈现一个RSS阅读器,例如

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

#1


0  

*UPDATE - This doesn't work because the extra tags don't validate because they aren't part of a namespace.

* UPDATE - 这不起作用,因为额外的标签不验证,因为它们不是命名空间的一部分。


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, e.g.

实际上忘了把rss标签放到问题中但是当我添加XML来存储rss和channel标签之间的PHP变量时,我可以动态地在XSL文件中使用它们,并且用于存储它们的XML在查看时不会呈现一个RSS阅读器,例如

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