homepage Welcome to WebmasterWorld Guest from 54.211.47.170
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
passing variable to xsl
hame22




msg:702230
 2:59 pm on Jul 25, 2005 (gmt 0)

Hi, i am looking to pass a variable from a php page into an xsl stylesheet

i have found some code but it does not seem to work.

stylesheet:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head><title>test</title></head>
<body>
<h2>Activity</h2>
<xsl:param name="myvar"/>

<xsl:for-each select ="Lom/general">
<h3> Activity Name: <p><B> <xsl:value-of select="title"/></B></p></h3>
<p>Description:</p><p><xsl:value-of select="description"/></p>
</xsl:for-each>

<xsl:for-each select="Lom/technical">

<p>Activity format: <xsl:value-of select="format"/></p>
</xsl:for-each>
<xsl:for-each select="Lom/technical/location">
<a href="{@link}">

<p>Click to access</p>
</a>


</xsl:for-each>
My PHP variable : <h3><xsl:value-of select="$myvar"/></h3><br />

</body>
</html>
</xsl:template></xsl:stylesheet>

php page

<?php
// Gonna contain PHP-XML output

$xh = xslt_create();

$p_id = 'cia01';

// Read plain PHP-XML output
//$xmlData = file_get_contents ('http://somethin.example.net/activities/dev/keywords/test.php', "r");

$c = curl_init('http://somethin.example.net/activities/dev/keywords/test.php?p_id='.$p_id.'');
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($c);

// Stack up output into one String
//while ($line = fgets ($xmlData))
//$xml .= $line;
//print $xml;

$arguments = array(
'/_xml' => $xml,
);

$parameters = array (
'myvar' => 'test'
);

// Process the document
$result = xslt_process($xh, 'arg:/_xml', 'xml2.xsl', NULL, $arguments, $parameters);

// Print out your transformed document
echo $result;

xslt_free($xh);

?>

would appreciate any ideas or help

thanks in advance

alex

[edited by: jatar_k at 5:18 pm (utc) on July 25, 2005]
[edit reason] generalized url [/edit]

 

choster




msg:702231
 1:42 pm on Aug 3, 2005 (gmt 0)

I don't use PHP with XML, but to create a universal parameter, you need to set it in the head of yuor stylesheet, before the first template. TO wit,

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:param name="myvar" />
<xsl:template match="/">
[...]

A param created inside a template is local to the template.

vinyljunkie




msg:702232
 3:31 am on Aug 5, 2005 (gmt 0)

Here is some objected oriented code that I use on my site to generate my XSL. I've gotten my result set in other code for my application and passed it to my class. Hope this helps.


<?php

class cdtrust_rss
{

/*
------------------------------------------------------------
Variable Definitions
------------------------------------------------------------
*/
var $db;
var $result;
var $channel_title;
var $channel_link;
var $channel_descript;
var $channel_filename;

/*
------------------------------------------------------------
The constructor function
------------------------------------------------------------
*/
function cdtrust_rss($db, $Result, $channel_title, $channel_link, $channel_descript,
$channel_filename)
{
$this->db = &$db;
$this->result = $Result;
$this->channel_title = $channel_title;
$this->channel_link = $channel_link;
$this->channel_descript = $channel_descript;
$this->channel_filename = $channel_filename;
}
/*
------------------------------------------------------------
cdtrust_rssgen
------------------------------------------------------------
*/
function cdtrust_rssgen()
{

// Figure out the current date/time.
$current_date = mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));
$rssgen_date = strftime("%B %d, %Y %H:%M:%S", $current_date);

$Display .= <<<EOD
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet href="$this->channel_filename.xsl" type="text/xsl"?>
<!-- RSS generation done by 'napathon.net' -->
<rss version="2.0">
<channel>
<title>$this->channel_title</title>
<link>$this->channel_link</link>
<description>$this->channel_descript&lt;br&gt;&lt;br&gt;
Last generated: $rssgen_date PDT</description>
<docs>http://www.napathon.net/</docs>
EOD;
if (mysql_num_rows($this->result) == 0) {

$Display .= <<<EOD

<item>
<title>
What?! No New arrivals? Yes, it's true. I've gone a whole month without adding a single
thing to my collection. Check back in a few days, and this page will probably have my
latest acquisition.
</title>
<description>
</description>
<link>http://www.napathon.net/</link>
</item>

EOD;
}
else {

while ($row = mysql_fetch_array ($this->result))
{
$AlbumTitle = $row['albumtitle'];
$AlbumTitle = str_replace(" &", " &amp;", $AlbumTitle);
$Format = $row['format'];
$AlbumID = $row['albumid'];
$DateAdded = $row['date_added'];
if ($row['artist_the'] == null) {
$ArtistName = $row['artist'];
}
else {
$ArtistName = $row['artist_the'] . " " . $row['artist'];
}
$ArtistName = str_replace(" &", " &amp;", $ArtistName);

$Display .= <<<EOD

<item>
<title>$ArtistName - $AlbumTitle on $Format</title>
<description>Added to the database on $DateAdded</description>
<link>http://www.napathon.net/AlbumID$AlbumID.php</link>
</item>

EOD;

}
}

$Display .= <<<EOD

</channel>
</rss>
EOD;

$this->display = $Display;
}

/*
------------------------------------------------------------
getRSS
------------------------------------------------------------
*/
function getRSS()
{
return $this->display;
}
}
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved