请向我解释如何使用最好的XSLT参数。在<xsl:param>和<xsl:with-param>方面
示例LOC:
<xsl:call-template name="ABC">
<xsl:with-param name="title" />
</xsl:call-template>发布于 2011-09-11 23:26:26
请告诉我怎样才能使用最好的XSLT参数。在
<xsl:param>和<xsl:with-param>方面
可以在全局级别的任何位置指定<xsl:param> (作为xsl:stylesheet的子级),或者如果它在模板中,则它必须是它的子级,并且必须位于xsl:template的任何非xsl:param子级之前。
这是允许模板或整个转换(在全局xsl:param的情况下)分别从模板的调用者/发起者或整个转换的调用者/启动器接收变化数据的工具。
在模板/转换的调用方/发起方一侧,使用xsl:with-param指令传递参数。它可以是xsl:apply-templates或xsl:call-template的子级。
xsl:param或xsl:with-param的name属性是必需的。它标识参数。
xsl:with-param的select属性可用于指定任何XPath表达式,其计算结果将传递给调用/应用的模板。
或者,可以在xsl:with-param的内容(正文)中指定该值。
xsl:with-param必须具有select属性或正文。但不是两个都是。
xsl:param还可以具有select属性或主体。在这种情况下,它们指定参数的默认值,如果调用方没有指定具有该名称的参数,则使用该参数。
最后,这里是一个完整的示例,说明了这些概念中的大多数
<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>...the结果为...
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参数。它的缺省值由被调用的template."love"被"like"替换。$pTarget和$pReplacement的值。如果转换的发起者决定为这些全局参数传递其他值(而不是代码中使用的默认值),则这些值将被传递给replace模板,而不是默认值"love";第二个调用根本不提供任何参数值,因此使用replace模板中的所有默认值--字符串"hate"被replace模板递归调用的字符串"disapprove".pText参数的值不是静态的,而是动态的calculated.xsl:apply-templates从外部启动replace模板。在这里,我们还展示了一个模板可以同时具有match和name属性,并且可以使用xsl:apply-templates和xsl:apply-templates来启动这样的模板发布于 2011-09-11 22:31:27
它用于传递在另一个模板中定义的参数:
<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>https://stackoverflow.com/questions/7378859
复制相似问题