tags:

views:

32

answers:

2

The situation is I have two xslt files: one is called from my ASP.NET code, and there, the second xslt file is imported.

What I'd like to accomplish is to pass a parameter to the first one so the second xslt(the one that is imported at the first xslt) can read it.

My c# code looks like this:

    var oArgs = new XsltArgumentList();
    oArgs.AddParam("fbLikeFeatureName", "", "Facebook_Like_Button");
    ltlContentBody.Text = xmlUtil.TransformXML(oXmlDoc, Server.MapPath(eSpaceId + "/styles/ExploringXSLT/ExploreContentObjects.xslt"), true);

And I'm catching the param at the first xslt this way:

<xsl:param name="fbLikeFeatureName" />

And then, passing it to the second xslt like this(previously, I import that file):

<xsl:call-template name="Articles">
    <xsl:with-param name="fbLikeFeatureName"></xsl:with-param>
  </xsl:call-template>

Finally, I'm catching the param on the second xslt file as following:

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

What Am I doing wrong? I'm kind of new at xslt.

+2  A: 

You are not setting the value of the parameter when you pass it to the Articles template. Try

<xsl:call-template name="Articles">
  <xsl:with-param name="fbLikeButtonName" select="$fbLikeFeatureName"/>
</xsl>

and ten

<xsl:template name="Articles">
  <xsl:param name="fbLikeButtonName"/>
   ...
  <xsl:value-of select="$fbLikeButtonName"/>
   ...
</xsl:template>

When using with-param, the name attribute is set to the name of the parameter as used by the called template (Articles in this case). You then use select (or the body of xsl:with-param) to set the value.

Kathy Van Stone
Thank you. It doesn't seem to be working though. At least no text is displaying on the browser(it's supposed to display the param's value).
Brian Roisentul
Is the name attrobite of with-param precisely the same as the name attribute of param? Does fbLikeFeatureName have a value outside of Articles?
Kathy Van Stone
Well, if you look at the ASP.NET code I wrote above you'll see its value is "Facebook_Like_Button".
Brian Roisentul
@Brian I mean does it appear if you use it directly somewhere in the transform (including in Articles) rather than passing it as a parameter.
Kathy Van Stone
Also, I don't know ASP.NET, so I'm not certain where oargs is being used in the transform as it does not appear in the last line of code you gave
Kathy Van Stone
+1  A: 

You don't need to "pass" the parameter from the first stylesheet to the imported stylesheet. When you declare the param at the top level on the first stylesheet, it is automatically visible to all imported stylesheets. Consider the following stylesheets:

template1.xsl:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:import href="template2.xsl"/>
    <xsl:param name="input-param"/>
    <xsl:template match="/">
        <xsl:apply-templates select="doc"/>
    </xsl:template>
</xsl:stylesheet>

Which imports template2.xsl:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
    <xsl:output method="text"/>
    <xsl:template match="doc">
        <xsl:value-of select="$input-param"/>
    </xsl:template>
</xsl:stylesheet>

I then transformed the following doc:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="template1.xsl"?>
<doc/>

with the input parameter "input-param" set to "This is a test". I get the following output (Saxon-B 9.1.0.7):

This is a test
Dan Menes
I've confirmed that the above code works on MSXML as well
Dan Menes
I don't know why I can't make it work...I'm doing everything you told me to.
Brian Roisentul
Well, I'll admit I'm much more of a XSL expert than a C# expert, but where in your code sample are you attaching the parameter list to the transform? I see where you create oArgs, and I see where you populate it, but it isn't mentioned on the line where you do the transform.
Dan Menes
Poking around on the web, it looks like oArgs needs to be included as the third argument of xmlUtil.TransformXML().
Dan Menes
Yes, you're both right. I included it before, but then I excluded it to test something and forgot to include it back. Thank you! It works great.
Brian Roisentul