Forum Moderators: open

Message Too Old, No Replies

Using a parameter in an XSL:IF element

         

Stu_Rogers

9:14 am on Sep 6, 2005 (gmt 0)

10+ Year Member



I hope this is possible - I'm pretty sure it is but I'm new to all this so can't figure it out.

I am using PHP's xslt_process to merge and display an XML and XSL document.

My XML is like...

<page number="1">
<content>this is page 1</content>
</page>

<page number="2">
<content>this is page 2</content>
</page>

<page number="3">
<content>this is page 3</content>
</page>

I want to pass a parameter from PHP to XSL to determine which page gets displayed.

I know how to pass a parameter and display it like this...

<xsl:param name="page" />
<xsl:value-of select="$page" />

But how do I use the parameter within an if statement? I'm looking for something like..

<xsl:if test="$page = 1">

(but that doesn't work!)

Any help or pointers appreciated - many thanks.

Alternative Future

9:27 am on Sep 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if it will work but creating a variable called internalPageVar that holds the value of your parameter sent from PHP the inwardPageVar like so:

<xsl:variable name="internalPageVar" select="inwardPageVar"/>

Then use

<xsl:choose>
<xsl:when test="$internalPageVar = '1'">

</xsl:when>
<xsl:when ...>
</xsl:when>
<xsl:otherwise>
<xsl:otherwise>
</xsl:choose>

Might work remember that XSL is useless with creating and doing things on the fly - but I am not fully sure of your intention.

-George

Stu_Rogers

10:44 am on Sep 6, 2005 (gmt 0)

10+ Year Member



My intention is to keep one XML document to hold data used throughout my website. Then use PHP to determine which data gets included where, for example page.php?number=3. I appreciate that having multiple XML documents would solve this, but I was hoping to do it with one.

I haven't yet tried the code you suggest but what is "inwardPageVar" and "internalPageVar"? Are they names you have made up or names that XSL will understand. (Google doesn't know them!).

Thanks.

Alternative Future

11:02 am on Sep 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stu_Rogers,

>>what is "inwardPageVar" and "internalPageVar"?

Yeah just names I made up to show a better meaning for them i.e inwardPageVar was you incoming parameter and internalPageVar was the local variable to the page.

-George

Stu_Rogers

11:08 am on Sep 6, 2005 (gmt 0)

10+ Year Member



Sorry Alternative Future, I see what you are doing now - using xsl:variable to assign my PHP parameter to an internal variable.

I have tried this but no luck. I must admit at this point, that I have just realised the code I provided in my first post does work...

<xsl:if test="$page = 1">
do some tranformation
</xsl:if>

But it doesn't solve my problem because it doesn't reference the attribute of my XML page tag, <page number="1">

I guess I'm just trying to run before I can walk and will need to do another few weeks of reading before I have the skills to target an attribute in this way.

Alternative Future

11:13 am on Sep 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stu_Rogers,

Not too sure on params but where you assign your local variable it should reference the xml something like:

<xsl:variable name="page" select="page/@number"/>
I am using xsl:variables as I am more sure of them ;-)

number is at the root of the page element and should be reference with the @ sign

Lets know if that assists any

-George

danseagrave

8:29 am on Sep 12, 2005 (gmt 0)

10+ Year Member



Hi,

I have been using something similar to


<xsl:choose>
<xsl:when test="$myvar='a'">
do something
</xsl:when>
<xsl:when test="myvar='b'">
do something else
</xsl:when>
</xsl:choose>

don't know it it will work any better/with parameters. I know I used params at some point - but it was some time ago and i forget now.

-- oops - just spotted the original reply, *shame*