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 ca开发者_StackOverflow社区tching 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.
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.
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">
<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">
<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
精彩评论