xml XSLT 参数的使用;<xsl:param> & <xsl:with-param>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7378859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of XSLT Params; <xsl:param> & <xsl:with-param>
提问by Siva Charan
Please explain me how best XSLT param can be used.
in terms of <xsl:param>& <xsl:with-param>
请向我解释如何最好地使用 XSLT 参数。在<xsl:param>& 方面<xsl:with-param>
Sample LOC:
示例位置:
<xsl:call-template name="ABC">
<xsl:with-param name="title" />
</xsl:call-template>
回答by Dimitre Novatchev
Please explain me how best XSLT param can be used. in terms of
<xsl:param>&<xsl:with-param>
请向我解释如何最好地使用 XSLT 参数。在
<xsl:param>& 方面<xsl:with-param>
<xsl:param>can be specified at the global level anywhere (as a child of xsl:stylesheet) or if it is within a template, it must be its child and it must precede any non-xsl:paramchild of xsl:template.
<xsl:param>可以在全球范围内的任何地方(的孩子中指定xsl:stylesheet),或者如果它是一个模板中,它必须是它的孩子,它必须先于任何非xsl:param的孩子xsl: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.
这是允许模板或整个转换(在 global 的情况下xsl:param)分别从模板或整个转换的调用者/发起者接收不同数据的工具。
On the side of the caller/initiator of the template/transformation, parameters are passed by using an xsl:with-paraminstruction. it can be a child of xsl:apply-templatesor xsl:call-template.
在模板/转换的调用者/发起者一侧,使用xsl:with-param指令传递参数。它可以是xsl:apply-templates或的孩子xsl:call-template。
The nameattribute of either xsl:paramor xsl:with-paramis mandatory. It identifies the parameter.
所述name的任一属性xsl:param或xsl:with-param是强制性的。它标识参数。
The select attribute of xsl:with-parammay be used to specify anyXPath expression, the result of whose evaluation is passed to the called/applied template.
的 select 属性xsl:with-param可用于指定任何XPath 表达式,其计算结果将传递给调用/应用模板。
Alternatively, the value can be specified in the content (body) of xsl:with-param.
或者,可以在 的内容(正文)中指定该值xsl:with-param。
xsl:with-parammust have either a selectattribute or a body. but not both of them.
xsl:with-param必须有一个select属性或一个主体。但不是他们两个。
An xsl:paramcan also have a select attribute or body. In this case, these specify the default valueof the parameter and it is used if no parameter with this name has been specified by the caller.
Anxsl:param也可以有一个选择属性或主体。在这种情况下,这些指定参数的默认值,如果调用者没有指定具有此名称的参数,则使用它。
Finally, here is a complete example illustrating most of these concepts:
最后,这里有一个完整的例子来说明大多数这些概念:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pTarget" select="'love'"/>
<xsl:param name="pReplacement" select="'like'"/>
<xsl:template match="/*">
<xsl:call-template name="replace">
<xsl:with-param name="pPattern" select="$pTarget"/>
<xsl:with-param name="pRep" select="$pReplacement"/>
</xsl:call-template>
<xsl:text>
</xsl:text>
<xsl:call-template name="replace"/>
<xsl:text>
</xsl:text>
<xsl:apply-templates select="text()">
<xsl:with-param name="pPattern" select="$pTarget"/>
<xsl:with-param name="pRep" select="'adore'"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="text()" name="replace">
<xsl:param name="pText" select="."/>
<xsl:param name="pPattern" select="'hate'"/>
<xsl:param name="pRep" select="'disapprove'"/>
<xsl:if test="string-length($pText) >0">
<xsl:choose>
<xsl:when test="not(contains($pText, $pPattern))">
<xsl:value-of select="$pText"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring-before($pText, $pPattern)"/>
<xsl:value-of select="$pRep"/>
<xsl:call-template name="replace">
<xsl:with-param name="pPattern" select="$pPattern"/>
<xsl:with-param name="pRep" select="$pRep"/>
<xsl:with-param name="pText" select=
"substring-after($pText, $pPattern)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied on this XML document...
当应用于此 XML 文档时...
<t>Sports stars we really love, love to hate, hate</t>
...the result is...
……结果……
Sports stars we really like, like to hate, hate
Sports stars we really love, love to disapprove, disapprove
Sports stars we really adore, adore to hate, hate
Explanation:
说明:
The
replacetemplate is called twice. In both calls thepTextparameter 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
$pTargetand$pReplacementare 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 thereplacetemplate and not the defaults"love"and"like".The second call doesn't provide any parameter values at all, so all defaults in the
replacetemplate are used -- the string"hate"is replaced by the string"disapprove".Note that the
replacetemplate calls itself recursively, so that all occurrences of the pattern are replaced by the replacement.Also, the values of the
pTextparameter of the recursive calls aren't static, but are dynamically calculated.The third time the
replacetemplate is initiated from outside is viaxsl:apply-templates. Here we also show that a template can have both amatchand anameattribute at the same time and it is possible that such a template can be initiated both usingxsl:apply-templatesandxsl:call-template.
该
replace模板被称为两次。在两个调用中pText都省略了参数。其默认值由被调用的模板使用。第一个调用提供了模式和替换参数,因此
"love"被替换为"like"。请注意,全局参数的值
$pTarget和$pReplacement是通过的。如果转换的发起者决定为这些全局参数传递其他值(不是此代码中使用的默认值),则这些值将传递给replace模板,而不是默认值"love"和"like"。第二个调用根本不提供任何参数值,因此
replace使用模板中的所有默认值——字符串"hate"被替换为字符串"disapprove"。请注意,
replace模板以递归方式调用自身,以便所有出现的模式都被替换替换。此外,
pText递归调用的参数值不是静态的,而是动态计算的。第三次
replace从外部启动模板是 viaxsl:apply-templates。在这里,我们还展示了一个模板可以同时具有 amatch和 aname属性,并且可以使用xsl:apply-templates和来启动这样的模板xsl:call-template。
回答by TheCodeKing
It's used to pass a param defined in another template:
它用于传递在另一个模板中定义的参数:
<xsl:param name="globalParam"></xsl:param>
<xsl:call-template name="ABC">
<xsl:with-param name="title" select="'A Title'" />
</xsl:call-template>
<xsl:template name="ABC">
<xsl:param name="title"/>
<xsl:value-of select="$title" />
<xsl:value-of select="$globalParam" />
</xsl:template>

