browser lang:en
Working in XSLT and PHP integration, one of the most necessary needs a developer necesitates, after XML-XSL integration, is how to pass variable or data inside the XSL script.
Why we necesite it? Well, for example if we do a dynamic Query (in this case XQuery) that use a Record (in this case one elemento of the XML tree), i mean the classic request (GET, POST o something else) but applied to the XSLT.
XML tree example: location.xml
<CityLocations>
<CityLocation>
<CityID>1</CityID>
<LocationName>New York</LocationName>
</CityLocation>
<CityLocation>
<CityID>2</CityID>
<LocationName>Chicago</LocationName>
</CityLocation>
</CityLocations>
Now we create the XSL file that execute the XQuery and receive the CityID parameter
Example:location.xsl.php
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<table width="100%">
<tr>
<th>CityId</th>
<th>LocationName</th>
</tr>
<xsl:for-each select="CityLocations/CityLocation[CityID=$CityID]">
<tr>
<td><xsl:value-of select="CityID"/></td>
<td><xsl:value-of select="LocationName"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
// ------------Applica un file XSL a un XML In PHP 5 --------------------
$cityLocations = "1";
// Carica il file dati
$xml = new DOMDocument("1.0");
$xml->load($_SERVER['DOCUMENT_ROOT'].'/xml/location.xml');
// Carica il file stile
$xsl = new DOMDocument("1.0");
$xsl->load($_SERVER['DOCUMENT_ROOT'].'/xslt/location.xsl.php');
// Crea un processore XSLT
$proc = new XSLTProcessor;
// Setta un parametro da trasformare nel file XSL
$proc->setParameter( '', 'CityID', $cityLocations);
$proc->importStyleSheet($xsl); // Importa il file xslt
$dom=$proc->transformToDoc($xml); // Output risultato a documento DOM
$dom->formatOutput=true;
echo $dom->saveXML($dom->documentElement); // Invia il risultato a un browser
Final result will be : CityID:1
PHP is one of the most widely used open-source server-side scripting languages that exist today. With over…
in:Scripts and tutorials (0 comments)In a FBML Facebook App, your quick jump menu will require a little tweak to work in FBJS…
in:Scripts and tutorials (0 comments)Every single day, someone, somewhere is discussing something important to your business; your brand, your executives, your…
in:Scripts and tutorials (0 comments)The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a…
in:Scripts and tutorials (0 comments)Because of the near-spherical shape of the Earth, calculating an accurate distance between two points requires the use…
in:Scripts and tutorials (1 comments)With this script we can limit the download speed // local file that should be send to the client $local_file…
in:Scripts and tutorials (0 comments)Some hosts disabled the ini setting allow_url_fopen. This also means that the ability to easily grab images…
in:Scripts and tutorials (0 comments)The act to verify if a file exists, is one of more important tasks related to files operations,…
in:Scripts and tutorials (0 comments)
