Welcome to WebmasterWorld Guest from 54.144.48.252

Forum Moderators: httpwebwitch

Message Too Old, No Replies

XSL, Php5 and variables

Passing variable to XSL with Php5

     
1:41 am on Jul 8, 2009 (gmt 0)

5+ Year Member



Hello Everyone,

I have an issue that is driving me crazy. I have a xml document and associated stylesheet. I simply want to be able to 1: declare a top level variable 2. Pass a variable value from the URL to the stylesheet so that, for example, if [someurl.com...] - the information for the 3rd ID is displayed. Below i have sample code but i can't seem to get this to work with Php 5. Does anyone have a clue as to what is going on. After spending about a week looking through the documents for Php5 and XML, I am totally confused. Note: I am looking for the simplest / most direct solution.

Thanks,

<?php

// XML string
$xml = '<?xml version="1.0"?>
<para>
change me
</para>';

// XSL string
$xsl = '
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="ISO-8859-1" indent="no"
omit-xml-declaration="yes" media-type="text/html"/>
<xsl:param name="myvar"/>
<xsl:param name="mynode"/>
<xsl:template match="/">
My PHP variable : <xsl:value-of select="$myvar"/><br />
My node set : <xsl:value-of select="$mynode"/>
</xsl:template>
</xsl:stylesheet>';

$xh = xslt_create();

// the second parameter will be interpreted as a string
$parameters = array (
'myvar' => 'test',
'mynode' => '<foo>bar</foo>'
);

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

echo xslt_process($xh, 'arg:/_xml', 'arg:/_xsl',NULL, $arguments, $parameters);

?>

2:31 am on Jul 8, 2009 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member



seems to me like XSLT might not be the easiest tool for this.

To get some data out of "node x", you can construct an XPATH describing that node, and use some of the simpler PHP methods for grabbing the value of that node.

This will get you started:
[php.net...]

Example #1 on that page does what you're describing

 

Featured Threads

Hot Threads This Week

Hot Threads This Month