Please explain me how best XSLT param can be used.
in terms of <xsl:param>
& <xsl:with-param>
Sample LOC:
<xsl:call-template name="ABC">
<xsl:with-param name="title" />
</xsl:call-template>
Please explain me how best XSLT param can be used.
in terms of <xsl:param>
& <xsl:with-param>
Sample LOC:
<xsl:call-template name="ABC">
<xsl:with-param name="title" />
</xsl:call-template>
<xsl:param>
can be specified at the global level anywhere (as a child ofxsl:stylesheet
) or if it is within a template, it must be its child and it must precede any non-xsl:param
child ofxsl:template
.This is the facility that allows a template or the whole transformation (in case of a global
xsl:param
) to receive varying data from the caller/initiator of the template or of the whole transformation, respectively.On the side of the caller/initiator of the template/transformation, parameters are passed by using an
xsl:with-param
instruction. it can be a child ofxsl:apply-templates
orxsl:call-template
.The
name
attribute of eitherxsl:param
orxsl:with-param
is mandatory. It identifies the parameter.The select attribute of
xsl:with-param
may be used to specify any XPath expression, the result of whose evaluation is passed to the called/applied template.Alternatively, the value can be specified in the content (body) of
xsl:with-param
.xsl:with-param
must have either aselect
attribute or a body. but not both of them.An
xsl:param
can also have a select attribute or body. In this case, these specify the default value of the parameter and it is used if no parameter with this name has been specified by the caller.Finally, here is a complete example illustrating most of these concepts:
When applied on this XML document...
...the result is...
Explanation:
The
replace
template is called twice. In both calls thepText
parameter is omitted. Its default value is used by the called template.The first call provides the pattern and replacement parameters, so
"love"
is replaced by"like"
.Do note that the values of the global parameters
$pTarget
and$pReplacement
are passed through. If the initiator of the transformation decides to pass other values (not the defaults that are used in this code) for these global parameters, these values will be passed to thereplace
template and not the defaults"love"
and"like"
.The second call doesn't provide any parameter values at all, so all defaults in the
replace
template are used -- the string"hate"
is replaced by the string"disapprove"
.Note that the
replace
template calls itself recursively, so that all occurrences of the pattern are replaced by the replacement.Also, the values of the
pText
parameter of the recursive calls aren't static, but are dynamically calculated.The third time the
replace
template is initiated from outside is viaxsl:apply-templates
. Here we also show that a template can have both amatch
and aname
attribute at the same time and it is possible that such a template can be initiated both usingxsl:apply-templates
andxsl:call-template
.It's used to pass a param defined in another template: