Pass PHP variable as a parameter to XSL - php

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

Related

How to add an xslt stylesheet in php using SimpleXmlElement to an existing xml document?

I have this xml document in a file called text.xml
The xml document currently looks like this:
<?xml version="1.0" encoding="UTF-8"?>
......
<rss version="2.0"...>
Right after the xml version declaration, I want to add the line:
<?xml-stylesheet type="text/xml" href="style.xslt"?>
In more detail this is what I want to do:
I first want to convert the string contents of the file to an xml document.
$mystr = file_get_contents(path to my text.xml file);
$myxml = new SimpleXmlElement($mystr);
I then want to add an attribute to my XML document to link an XSLT stylesheet.
$myxml->addAttribute(_____);
using the SimpleXmlElement addAttribute method, but the addAttribute method allows for the name, value, and namespace--How would I even use the namespace? Since I have three parameters it makes the most sense for the href to be in the namespace parameter spot.
So I have tried to fill in the blank with 'xml-stylesheet','text/xml','style.xslt' which doesn't work because I am getting the error that "Attribute requires prefix for namespace". How do I get this to work?
Link to php docs: http://php.net/manual/en/simplexmlelement.addattribute.php
Edit: This is different from the other question because I am trying to link an xslt stylesheet to an rss document. Using the "duplicate questions'" solution causes the xslt stylesheet to be linked after the rss version declaration which is not good enough.
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"...>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
....
Instead I want:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xml" href="test.xslt"?>
<rss version="2.0"...>
....

simplexml load multiple files at once

I'm using XML to fetch some language data.
The XML markup is as follows:
<language>
<item>
<key>KEY</key>
<string>STRING</string>
</item>
<item>
....
</item>
</language>
I have 2 XML-files with the same markup. I want to load them both with simplexml_load_file. Does anyone know what's the best approach to do this?
I tried something like this:
<?php
$xml = simplexml_load_file('...url_file_1...');
$xml .= simplexml_load_file('...url_file_2...');
?>
But thats not working. But thats what I want: all the data from the 2 (and eventually more) files in one variable.
Edit: I want to make use of the simpleXML library if possible.

This XML file does not appear to have any style information associated with it

Trying to work on a website using Dreamweaver and PHP for development.
On the live site, I suddenly found this error:
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
<channel>
<title>
ABCDED
</title>
<cf:treatAs xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005">list</cf:treatAs>
<link>http://events.sjsu.edu/default.aspx</link>
<description>RSS Feed ABCD Events Calendar</description>
</channel>
</rss>
Actually, are are echoing a XML file into a PHP page ?
$xml = simplexml_load_file('URL');
foreach ($xml->channel->channel as $value){
$var1 = $value->link;
$var2 = $value->description;
}
echo $var1."<br>".$var2;
Display it in php.
Hope it works!

How to wrap my rss feed with asXML function

I have a page called rss.php that contains PHP SQL and XML which dynamically, and might I add PERFECTLY, produces the XML needed for the rss feed for my podcast. Only problem is, its a PHP file. I need to get the stuff this page spits out in to a file named rss.xml for iTunes to accept it. I came across this little bit of code in another thread:
echo $xml->asXML('filename.xml');
and this on http://php.tonnikala.org:
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...
?>
Problem is when I wrap my XML in this code, the browser gives me an error and it doesn't load. This may be because my XML isn't just XML, it's PHP and SQL. And now that I think about it, maybe it's cause my PHP hasn't been processed by the server yet...
Anyway, what I want to do is get this rss.php page to spit out XML and save it to a file called rss.xml. Also, can I control how often this happens? Or will it happen every time the page loads?
The answer to my question turned out to be that I DIDN'T need the page to be an XML document at all. As long as the declaration at the beginning says it's XML, its fine. Here's what I did. Remember place this at the VERY TOP of the PHP page.
<?php echo('<?xml version="1.0" encoding="UTF-8"?>') ?>

RSS not listing in IE but working in FF and other browsers

I have a RSS feed, for some reason I don't see any text on IE but in other browsers it's working.
The rss is starting with :
<?xml version="1.0"?>'
<rss version="2.0">'
Can it be the rss or xml version? or something else, I really don't know,
IE identifies that the page is a RSS feed, but doesn't show any text.
I think the header should be like this, assuming your feed is UTF-8 encoded:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
You can validate your feed over here: http://validator.w3.org/feed/#validate_by_uri. For your feed there are two errors. Maybe fixing them will help.
Another point, do you send the correct Content-Type header? In PHP you can do that like this:
header("Content-Type: application/rss+xml");

Categories