简体   繁体   中英

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
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?

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.

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.

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>
    <extraInfo>
        <heading>...</heading>
        <description>...</description>
    </extraInfo>
    <channel>
        ...
    </channel>
</rss>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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