我如何不在我的 xslt 代码中重复重复的逻辑?
编写此代码的更好方法是什么:
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:if test="$position <= string-length($input)">
<xsl:choose>
<xsl:when test="substring($input, $position, 1) = '_'">
<xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($input, $position, 1)"/>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
好吧,它很干净,但我相信它可以更干净。现在说我正在重复这个逻辑:
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$new_position"/>
</xsl:call-template>
那么基本上有人有任何解决方案吗?
我实际上自己尝试过@ xslt 可以吗如果我们这样做 `select="$position + $jump"`? 但该方法(或我所说的 hack)不起作用..所以我目前没有解决方案,想知道是否有人可以提供帮助。
基本上我的想法是这样的:
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:variable name="jump"/>
<xsl:if test="$position <= string-length($input)">
<xsl:choose>
<xsl:when test="substring($input, $position, 1) = '_'">
<xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<!-- set jump to 2 -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($input, $position, 1)"/>
<!-- set jump to 1 -->
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + $jump"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
或者也许是一些完全不同或异国情调的东西。 (此处为不带扩展的 XSLT 1.0)
what's a better way to write this code:
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:if test="$position <= string-length($input)">
<xsl:choose>
<xsl:when test="substring($input, $position, 1) = '_'">
<xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 2"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($input, $position, 1)"/>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + 1"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
Ok its clean but I believe it can be cleaner. Say right now I'm repeating this logic:
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$new_position"/>
</xsl:call-template>
So basically does anyone have any solution?
I've actually tried it myself @ xslt is it ok if we do `select="$position + $jump"`? but that method (or hack as i call it) is not working.. so i'm currently out of solutions and was wondering if someone could help.
Basically I was thinking along the lines of:
<xsl:template name="CamelChain">
<xsl:param name="input"/>
<xsl:param name="position"/>
<xsl:variable name="jump"/>
<xsl:if test="$position <= string-length($input)">
<xsl:choose>
<xsl:when test="substring($input, $position, 1) = '_'">
<xsl:value-of select="translate(substring($input, $position + 1, 1), 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
<!-- set jump to 2 -->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($input, $position, 1)"/>
<!-- set jump to 1 -->
</xsl:otherwise>
</xsl:choose>
<xsl:call-template name="CamelChain">
<xsl:with-param name="input" select="$input"/>
<xsl:with-param name="position" select="$position + $jump"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
or well maybe something totally different or exotic. (XSLT 1.0 without extensions here)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更好的方法是用 XSLT 2.0 编写:
恐怕无论您多么努力,在 XSLT 1.0 中解决字符操作问题都将是乏味且冗长的。
A better way is to write it in XSLT 2.0:
I'm afraid however hard you try, solving character manipulation problems in XSLT 1.0 is going to be tedious and verbose.
正如 @Michael-Key 明确指出的那样,XSLT 1.0 中的字符串操作非常冗长(好吧,乏味......取决于:)。
我查看了您的模板,我认为仅在模板范围内递归调用一次模板就不容易获得,除非您改变了设计模板的想法。
另请注意,您的模板不会将输入单词的第一个字母大写。这是想要的吗?
然而,由于存在更加乏味和冗长的风险,我想向您展示这种方法,其中:
_
一次,而不是像您那样为每个字符一次(显然不是?)例如,如果您将其称为:
它将返回:
As @Michael-Key clearly states, string manipulation in XSLT 1.0 is verbous (well, tedious..depends :).
I've looked at your template and I think is not that easy to obtain just once the template recursive invocation inside the template scope, unless you change your mind on how you have designed the template.
Do note also that your template is not going to upper case the first letter of the input word. Is that wanted?
However, with risk of being even more tedious and verbose, I want to show you this approach where:
_
and not once for each character as your does (apparently isnt?)For instance, if you call it as:
it will return: