Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending a variable to XSL via PHP 1

Status
Not open for further replies.

johnarthouse

Technical User
Jul 28, 2009
5
GB
I have a simple xml file with information for car parks, each has a unique id.
I want the details of one car park to be displayed after clicking on a static html link.

The link looks like:

Code:
<a href="detail.php?park=4">Link</a>

a sample of the XML file looks like this:

Code:
<CPINFOWEB>
<CARPARK>
<ID>4</ID>
<CostCode>16</CostCode>
<Name>Old Marylebone Road</Name>
<Address>300 Old Marylebone Road</Address>
<Town>London</Town>
<Postcode>NW1 5RJ</Postcode>
<Telephone>123456</Telephone>
</CARPARK>
</CPINFOWEB>

the PHP page contains this:

Code:
<?php
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML("CPINFOWEB-2.xml");
$mm_xsl->setXSL("CPINFOWEB-2.xsl");
echo $mm_xsl->Transform();
?>

plus an include for MM_XSLTransform.class.php that Dreamweaver pops in for me.

the XSL file contains

Code:
<xsl:for-each select="dataroot/CPINFOWEB/CARPARK[ID='4']">

which works great to display the details for carpark ID 4, because 4 is hard coded

SO, what I want to do is have [ID='4'] be whatever ID number is in the link.
So that actually works.

Any help would be gratefully appreciated. It's on a PHP 4.4.9 server
Reply With Quote
 
I am not familiar with MM_. The way it is done is generally this.

[1] Set a _top-level_ paramter in the xsl document. It's value is indeterminate.
[tt]
<xsl:param name="parkID" />
[/tt]
You can for testing purpose and getting familiar with the device by "hard code" its value to 4 like this.
[tt]
<xsl:param name="parkID" select="'4'" /> <!-- testing only -->
[/tt]
[2] In the xsl document, change the hard code part to this.
[tt]
<xsl:for-each select="dataroot/CPINFOWEB/CARPARK[ID=$parkID]">
[/tt]
[3] In the php script, it should have a method usually name setParameter() (such as php5 XSLTProcessor) or similar (check your document on mm_). Then the calling of it would be something like this.
[tt]
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML("CPINFOWEB-2.xml");
$mm_xsl->setXSL("CPINFOWEB-2.xsl");
[blue]//pseudo-code - null namespace
$mm_xsl->setParameter("","parkID",$_GET['park']);[/blue]
echo $mm_xsl->Transform();[/tt]
 
Many thanks tsuji

I'm having problems getting the ID number to work

<xsl:for-each select="dataroot/CPINFOWEB/CARPARK[ID=$parkID]">

It doesn't select car park 4 ion the xml. However, if I put
<xsl:value-of select="$parkID" /> in the body, it works fine and displays 4.

I've played around with single and double quotes, bit can't get
<xsl:for-each select="dataroot/CPINFOWEB/CARPARK[ID=$parkID]">
to work.

When I added your code [3] to the php file, I got this:
Fatal error: Call to undefined method: mm_xsltransform->setparameter()

I've attached the MM_XSLTransform.class.php

Really grateful for your help, I'm sure it's nearly right. I'm really new to xsl - know nothing about namespace etc.
 
 http://www.arthousedesign.co.uk/britannia/MM_XSLTransform.class.php
[4] pre-set-parameter debugging

[4.1] You said it does not work with 4 being hard-coded via xsl:param element. This shouldn't be. Make sure you understand what a top-level element means. It means xsl:param element must be directly under the xsl root, ie, xsl:stylesheet element. Add this immediately after xsl:stylesheet tag.
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]">
<xsl:param name="parkID" select="'4'" />
<!-- etc etc your existing line with the line [ID='4'] replaced. -->
</xsl:stylesheet>
[/tt]
[4.2] With this behind, you can tackle the setParameter php script.

[5] Could you make available a copy by renaming the MM_XSLTransform.class.php to MM_XSLTransform.class.txt so I can load it in the browser to look into it?

[5.1] I said the line mm_xsl->setParameter() is a pseudo-code. It cannot be signed out without looking into the class, for sure. (Note: it might be a member of mm_xsl, not something unknown till now mm_xsltransform.)
 
Thanks, I've got it. I'll take a look of it tomorrow. I'm calling the day off.
 
[6] I've browsed over the class construction. The member addParameter() member is provisioned for the purpose. Hence, this should be about the flow of the logic.
[tt]
$mm_xsl = new MM_XSLTransform();
$mm_xsl->setXML("CPINFOWEB-2.xml");
$mm_xsl->setXSL("CPINFOWEB-2.xsl");
[blue]$mm_xsl->addParameter("parkID",$_GET['park']);[/blue]
echo $mm_xsl->Transform();[/tt]
 
That's perfect! - big thanks tsuji for all your help with this. Very grateful!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top