Set XSLT Parameters from PHP

Use the XSLTProcessor::setParameter( ) method: The first argument is the namespace and can be null

$uid = '123';
 
$dom  = new DOMDocument
$dom->load('addresses.xml');
$xsl  = new DOMDocument
$xsl->load('stylesheet.xsl');
 
$xslt = new XSLTProcessor();
$xslt->importStylesheet($xsl);
$xslt->setParameter(NULL, 'user_id', $uid);
print $xslt->transformToXML($dom);

stylesheet.xsl:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>
 
<xsl:template match="/addresses/person">
  <xsl:if test="userid=$user_id">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:if>
</xsl:template>
</xsl:stylesheet>
Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker