请解释如何最好地使用 XSLT 参数。
在 <xsl:param>
方面& <xsl:with-param>
示例 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>
<xsl:param>
可以在全局级别的任何地方指定(作为 xsl:stylesheet
的子级)或者如果它在模板中,则它必须是它的子级并且它必须在任何非 xsl:param
之前xsl:template
的 child .
这是允许模板或整个转换(在全局 xsl:param
的情况下)分别从模板或整个转换的调用者/发起者接收不同数据的工具。
在模板/转换的调用者/发起者一侧,使用 xsl:with-param
传递参数操作说明。它可以是 xsl:apply-templates
的 child 或 xsl:call-template
.
name
xsl:param
之一的属性或 xsl:with-param
是强制性的。它标识参数。
xsl:with-param
的选择属性可用于指定任何 XPath 表达式,其计算结果将传递给调用/应用的模板。
或者,可以在 xsl:with-param
的内容(正文)中指定值.
xsl:with-param
必须有一个 select
属性或 body 。但不是他们两个。
安xsl:param
也可以有一个选择属性或主体。在这种情况下,这些指定参数的默认值,如果调用者没有指定具有此名称的参数,则使用它。
最后,这里有一个完整的例子来说明其中的大部分概念:
<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>
应用于此 XML 文档时...
<t>Sports stars we really love, love to hate, hate</t>
...结果是...
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
解释:
replace
模板被调用两次。在这两个调用中pText
省略参数。它的默认值由被调用的模板使用。第一个调用提供模式和替换参数,所以
"love"
被"like"
取代.请注意全局参数的值
$pTarget
和$pReplacement
被通过。如果转换的发起者决定为这些全局参数传递其他值(不是此代码中使用的默认值),这些值将传递给replace
模板而不是默认值"love"
和"like"
.第二次调用根本不提供任何参数值,因此
replace
中的所有默认值使用模板——字符串"hate"
替换为字符串"disapprove"
.请注意
replace
模板以递归方式调用自身,以便所有出现的模式都被替换项替换。此外,
pText
的值递归调用的参数不是静态的,而是动态计算的。第三次
replace
模板从外部启动是通过xsl:apply-templates
.在这里,我们还展示了一个模板可以同时具有match
和一个name
同时使用属性,这样的模板可以同时使用xsl:apply-templates
启动。和xsl:call-template
.
关于xml - XSLT 参数的使用; <xsl :param> & <xsl:with-param>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7378859/