INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

XSLT and XML

Using XSL with PHP and passing Values by kb244
Posted: 3 Jan 05

This issue came up because I had a single XML document, and in one of the sections I had to output the list of items if none were specificed, and if one was specificed to only output that node. So this FAQ simply covers how to transform XML w/ XSL documents into a usable state, but also how to take data from PHP and pass them into the XSL parser.

CODE

// Xml and XSL files
$xml_file = "./sections/sitedata.xml";
$xsl_file = "./sections/".$page.".xsl";

//Check for Extra Parameters
$xtra = "A Value;"
/* you can also give multiple by changing $xtra into $xtra = array("name" => "value", "name2" => "value2"); then changing out the array('xtra' => $xtra) to just $xtra. */

// Allocate a new XSLT processor
$xh = xslt_create();
$fileBase = 'file://' . getcwd () . '/';
xslt_set_base ( $xh, $fileBase );
                
// Process the document
                    
$result = xslt_process($xh, $xml_file, $xsl_file, NULL, array(), array('xtra' => $xtra));
if (!$result) {
    // Something croaked. Show the error
    echo 'XSLT processing error: ' .xslt_error($xh) ;
}

Then in the XSL stylesheet:

CODE

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xsl:space="preserve">

<xsl:param name="xtra"/>

<xsl:template match="/">
    <xsl:apply-templates select="sitedata" />
</xsl:template>
....

Thats the declaration above. To actually obtain or use the value:

Before PHP 4.0.6

CODE

<xsl:value-of select="xtra" />

PHP 4.0.6 and newer

CODE

<xsl:value-of select="$xtra" />

In the way I used this if I only wanted it to show the tutorial I've passed a value for would be something like this:

CODE

<xsl:template match="tutorial">
<xsl:if test="./@id=$xtra">
....
      OUTPUT for this tutorial
....
</xsl:if>
</xsl:template>

Back to XML FAQ Index
Back to XML Forum

My Archive

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close