Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 19th, 2008, 08:00 AM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default selecting param dyn. by variable

Hi,

I try to select the value of a xsl:param. Therefore you have to provide its name. The name of the param is stored in a xsl:variable.

How do I write this?
Code:
Short sample:
<xsl:param name="foo:a" select="'Hallo'"/>
<xsl:variable name="paraname" select="'foo:a'" />
// this doesn't work
<xsl:value-of select="${$paraname}"/> World
Output should be:
Hallo world
The answer of course shouldn't be: <xsl:value-of select="$foo:a"/>, because that's not the problem. The sample above is the abstract of a more complicated situation.

Details and useable sample:
Code:
XML:
<?xml version="1.0" encoding="utf-8"?>
<title>This product was clicked <label id="clickrate"/> times and bought <label id="buyrate"/> times</title>
Code:
XSLT:
<?xml version="1.0" encoding="utf-8" ?>
 <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:foo="urn:foo"
 >
   <xsl:output method="html" encoding="utf-8" indent="yes" omit-xml-declaration="yes" />

   <xsl:param name="foo:clickrate" select="'1000'"/>
   <xsl:param name="foo:buyrate" select="'3'"/>


   <xsl:template match="text()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

   <xsl:template match="label">
     <xsl:variable name="labelid" select="@id" />

       <xsl:value-of select="concat('??value_of_param_foo:',$labelid,'??')"/>
   </xsl:template>
</xsl:stylesheet>
Code:
Output:
This product was clicked ??value_of_param_foo:clickrate?? times and bought ??value_of_foo:buyrate?? times
The red marked parts in the output should be '1000' and '3'.

One solution for the label-template could be:
Code:
<xsl:template match="label">
      <xsl:variable name="labelid" select="@id" />
        <xsl:choose>
            <xsl:when test="$labelid = 'clickrate'">
                <xsl:value-of select="$foo:clickrate"/>
            </xsl:when>
            <xsl:when test="$labelid = 'buyrate'">
                <xsl:value-of select="$foo:buyrate"/>
            </xsl:when>
            <xsl:otherwise></xsl:otherwise>
        </xsl:choose>
    </xsl:template>


But if you keep in mind that I've a lot of params to handle this xsl:choose statement becomes bigger and bigger.

I'd be grateful for any help. Thanks.

Best regards
polarbear



 
Old January 19th, 2008, 09:03 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

What you want to achieve is not possible with standard XSLT 1.0 or 2.0.
Even when you look at extension functions like saxon:evaluate http://www.saxonica.com/documentatio.../evaluate.html
you will find that the context the expression is evaluated in "does not include any variables from the calling environment."






Similar Threads
Thread Thread Starter Forum Replies Last Post
Using param/variable to copy attributes shahbhat XSLT 2 August 27th, 2008 04:03 PM
I want to Create a Dyn. .Input Matrix Of Textboxes sanchit_mum07 C# 0 September 12th, 2007 09:58 AM
xsl:variable holding name of an xsl:param perissos XSLT 0 December 5th, 2006 07:09 AM
<input> to Param or Variable bonekrusher XSLT 1 October 11th, 2006 06:00 PM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.