xslt从模板中获取调用模板名称的任何方法
我对XSLT非常陌生。我想知道是否有任何方法可以从模板中获取调用模板的名称xslt从模板中获取调用模板名称的任何方法,xslt,xslt-1.0,Xslt,Xslt 1.0,我对XSLT非常陌生。我想知道是否有任何方法可以从模板中获取调用模板的名称 <xsl:element name="parent"> <xsl:choose> <xsl:when test="$myVariable = 'process1'"> <xsl:call-template name="templateA"/> </xsl:when> <
<xsl:element name="parent">
<xsl:choose>
<xsl:when test="$myVariable = 'process1'">
<xsl:call-template name="templateA"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="templateB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:template name="templateA">
<!-- Some Other Tags Here -->
<xsl:call-template name="templateB />"
</xsl:template>
<xsl:template name="templateb"> <!-- very big template -->
<!-- existing tags here -->
<!-- Add a new tag here only if called via templateA -->
</xsl:template>
我现在得到了以下有点复杂的结构。一个模板直接包含一次,通过另一个模板包含一次。只有从特定模板调用时,我才需要将新标记添加到此模板
<xsl:element name="parent">
<xsl:choose>
<xsl:when test="$myVariable = 'process1'">
<xsl:call-template name="templateA"/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="templateB"/>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:template name="templateA">
<!-- Some Other Tags Here -->
<xsl:call-template name="templateB />"
</xsl:template>
<xsl:template name="templateb"> <!-- very big template -->
<!-- existing tags here -->
<!-- Add a new tag here only if called via templateA -->
</xsl:template>
您可以使用参数
<xsl:template name="templateB"> <!-- very big template -->
<xsl:param name="calledFrom" select="" />
<!-- existing tags here -->
<xsl:if test="$calledFrom = 'templateA">
<!-- Add a new tag here only if called via templateA -->
</xsl:if>
</xsl:template>
然后就这样叫它
<xsl:call-template name="templateB">
<xsl:with-param name="calledFrom" select="'templateA'" />
</xsl:call-template>
您可以使用参数
<xsl:template name="templateB"> <!-- very big template -->
<xsl:param name="calledFrom" select="" />
<!-- existing tags here -->
<xsl:if test="$calledFrom = 'templateA">
<!-- Add a new tag here only if called via templateA -->
</xsl:if>
</xsl:template>
然后就这样叫它
<xsl:call-template name="templateB">
<xsl:with-param name="calledFrom" select="'templateA'" />
</xsl:call-template>
如果函数/模板需要知道从何处调用,那么设计就有问题。传递一个参数当然是修复代码的直接方法,但是堆积参数和基于参数值添加条件逻辑会导致无法维护的意大利面条
这里没有足够的代码来评估设计,但我想问一下,为什么它没有更多地使用模板规则而不是命名模板。明智地使用apply模板很可能会更自然地解决这个问题 如果函数/模板需要知道从何处调用,那么设计就有问题。传递一个参数当然是修复代码的直接方法,但是堆积参数和基于参数值添加条件逻辑会导致无法维护的意大利面条
这里没有足够的代码来评估设计,但我想问一下,为什么它没有更多地使用模板规则而不是命名模板。明智地使用apply模板很可能会更自然地解决这个问题 传递参数就是解决方案,我不知道它们是否在嵌套模板中传递
适合我的场景的解决方案是
参数通过隧道传输(传递)到XSLT2.0中默认调用的模板,但在XSLT1.0中,我们需要指定tunnel=“yes”
。使用tunellingmyVariable
可以访问调用的模板。传递参数就是解决方案,我不知道它们是否在嵌套模板中传递
适合我的场景的解决方案是
参数通过隧道传输(传递)到XSLT2.0中默认调用的模板,但在XSLT1.0中,我们需要指定tunnel=“yes”
。使用tunellingmyVariable
可以访问名为的模板。Yup。非常感谢您的回复。我在这里处理遗留代码:)处理遗留代码时,我的经验法则是总是尝试让它比我发现它时更易于维护。除非可能没有规范和回归测试,在这种情况下,我的经验法则是:不要改变任何东西!是的。非常感谢您的回复。我在这里处理遗留代码:)处理遗留代码时,我的经验法则是总是尝试让它比我发现它时更易于维护。除非可能没有规范和回归测试,在这种情况下,我的经验法则是:不要改变任何东西!