xsl:variable
-
Does anyone know how to call a stylesheet and pass in a variable value ? I'm doing some docs using the XML generated by .NET and I want to pass in the node to look up for each page that documents one function, so I can pull out the parameters, etc. I hope to do this with one XSL, but at the moment it's something like <xsl;for-each select="[contains[@name, "M:XMLObject:functionname"]/> This works, but I'm not sure how to change 'functionname' dynamically, except by passing the xsl through another xsl sheet, and I don't want to do that, I want to set the value with a variable, and set the variable as I call the sheet. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
Does anyone know how to call a stylesheet and pass in a variable value ? I'm doing some docs using the XML generated by .NET and I want to pass in the node to look up for each page that documents one function, so I can pull out the parameters, etc. I hope to do this with one XSL, but at the moment it's something like <xsl;for-each select="[contains[@name, "M:XMLObject:functionname"]/> This works, but I'm not sure how to change 'functionname' dynamically, except by passing the xsl through another xsl sheet, and I don't want to do that, I want to set the value with a variable, and set the variable as I call the sheet. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
I think what you're after is the xsl:param, combined with an xsl:call-tempalte or xsl:apply-templates call. For example, I use the following template to remove duplicates from a deeply nested list [
/Body/Details/Item/Ref[@Type='Note']/Identifier
]:<xsl:template name="DeDupe">
<xsl:param name="list" />
<xsl:param name="sep" select="', '" /><xsl:for-each select="$list"> <xsl:variable name="curr" select="string(.)" /> <xsl:variable name="i" select="position()" /> <xsl:choose> <xsl:when test="count($list\[$i > position() and string(.) = $curr\]) > 0" /> <xsl:otherwise> <xsl:if test="position() != 1"><xsl:copy-of select="$sep" /></xsl:if> <xsl:value-of select="$curr" /> </xsl:otherwise> </xsl:choose> </xsl:for-each>
</xsl:template>
...
<xsl:call-template name="DeDupe">
<xsl:with-param name="list" select="XPath expression" />
</xsl:call-template> -
I think what you're after is the xsl:param, combined with an xsl:call-tempalte or xsl:apply-templates call. For example, I use the following template to remove duplicates from a deeply nested list [
/Body/Details/Item/Ref[@Type='Note']/Identifier
]:<xsl:template name="DeDupe">
<xsl:param name="list" />
<xsl:param name="sep" select="', '" /><xsl:for-each select="$list"> <xsl:variable name="curr" select="string(.)" /> <xsl:variable name="i" select="position()" /> <xsl:choose> <xsl:when test="count($list\[$i > position() and string(.) = $curr\]) > 0" /> <xsl:otherwise> <xsl:if test="position() != 1"><xsl:copy-of select="$sep" /></xsl:if> <xsl:value-of select="$curr" /> </xsl:otherwise> </xsl:choose> </xsl:for-each>
</xsl:template>
...
<xsl:call-template name="DeDupe">
<xsl:with-param name="list" select="XPath expression" />
</xsl:call-template>Sort of - but I hoped to be able to call the stylesheet and pass in the variable, not run another transform to get it. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
-
Sort of - but I hoped to be able to call the stylesheet and pass in the variable, not run another transform to get it. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
So you're looking for something like:
<xsl:template name="ListItems">
<xsl:param name="list" />
<xsl:param name="name" /><xsl:for-each select="$list\[contains(@name, $name)\]"> **<-- Do something here -->** </xsl:for-each>
</xsl:template>
...
<xsl:call-template name="ListItems">
<xsl:with-param name="list" select="XPath expression" />
<xsl:with-param name="name" select="'M:XMLObject:functionname'" />
</xsl:call-template> -
Does anyone know how to call a stylesheet and pass in a variable value ? I'm doing some docs using the XML generated by .NET and I want to pass in the node to look up for each page that documents one function, so I can pull out the parameters, etc. I hope to do this with one XSL, but at the moment it's something like <xsl;for-each select="[contains[@name, "M:XMLObject:functionname"]/> This works, but I'm not sure how to change 'functionname' dynamically, except by passing the xsl through another xsl sheet, and I don't want to do that, I want to set the value with a variable, and set the variable as I call the sheet. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002
Christian - use
xsl:param
as a direct child of thexsl:stylesheet
root element. You can then access the parameter in any XPath expression as $param-name. The parameters can be added using theaddParameter
ofIXSLProcessor
if you're accessing MSXML programmatically. If you're using a command-line tool, obviously, it depends, but MSXSL (Microsofts c-line XSL tool) has a "param=value" syntax. HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p' -
Christian - use
xsl:param
as a direct child of thexsl:stylesheet
root element. You can then access the parameter in any XPath expression as $param-name. The parameters can be added using theaddParameter
ofIXSLProcessor
if you're accessing MSXML programmatically. If you're using a command-line tool, obviously, it depends, but MSXSL (Microsofts c-line XSL tool) has a "param=value" syntax. HTH Stuart Dootson 'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'Wow - that is EXACTLY what I was looking for, thanks. Christian Hey, at least Logo had, at it's inception, a mechanical turtle. VB has always lacked even that... - Shog9 04-09-2002 During last 10 years, with invention of VB and similar programming environments, every ill-educated moron became able to develop software. - Alex E. - 12-Sept-2002