tags:

views:

313

answers:

2

How do I use php sessions in XSLT for example for making a shopping cart for a webshop?

A user can browse the site and click "Add to cart" on several items. Each item should then be stored in a session variable. The user can at all time view the items selected by clicking "View cart".

+3  A: 

If you're using XSLT from within PHP, you can pass parameters to it by XSLTProcessor::setParameter(). You'll have to declare that parameter in XSL with

<xsl:param name="«param name»"/>

For example...

PHP:

// $xsl, $xml -- DOMDocument objects
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
$proc->setParameter(''/*default namespace*/, 'test_param', $aValue);
$proc->setParameter('', 'session_name', session_name());
$proc->setParameter('', 'session_id', session_id());
echo $proc->transformToXML($xml);

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:param name="test_param"/>
    <xsl:param name="session_name"/>
    <xsl:param name="session_id"/>
    <xsl:template match="/">
        <p>Your test parameter is: <xsl:value-of select="$test_param"/></p>
        <p>Your session name is: <xsl:value-of select="$session_name"/></p>
        <p>Your session ID is: <xsl:value-of select="$session_id"/></p>
        <p>
         <a>
          <xsl:attribute name="href">
            <xsl:value-of select="concat('http://example.com/index.php?',$session_name,'=',$session_id)"/&gt;
          </xsl:attribute>
          Link with session
        </a>
       </p> 
    </xsl:template>
</xsl:stylesheet>
vartec
I will try this! Thank you!
jorgen
A: 

you're probably calling the xslt processor on some XML, why don't you just add the session data to that xml ?

jab11