我如何不在我的 xslt 代码中重复重复的逻辑? [英] how do i not repeat repeated logic in my xslt code?

查看:24
本文介绍了我如何不在我的 xslt 代码中重复重复的逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写此代码的更好方法是什么:

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 &lt;= 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?

实际上我自己试过了@xslt如果我们执行 `select="$position + $jump"`` 可以吗? 但是那个方法(或我称之为 hack)不起作用..所以我目前没有解决方案并且想知道如果有人可以提供帮助.

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.

基本上我是按照以下思路思考的:

<xsl:template name="CamelChain">
      <xsl:param name="input"/>
      <xsl:param name="position"/>
      <xsl:variable name="jump"/>
      <xsl:if test="$position &lt;= 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)

or well maybe something totally different or exotic. (XSLT 1.0 without extensions here)

推荐答案

正如@Michael-Key 明确指出的,XSLT 1.0 中的字符串操作是冗长的(好吧,乏味..depends :).

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:

  • 只使用一个参数
  • 修复了第一个字符的问题
  • 变量的使用(以便您可以看到它们是如何工作的)
  • 模板为每个 _ 调用一次自身,而不是像您那样为每个字符调用一次(显然不是?)
  • only one parameter is used
  • the problem with the first character is fixed
  • use of variables (so that you can see how they works)
  • the template call itself once for each _ and not once for each character as your does (apparently isnt?)
<xsl:template name="CamelCase">

    <xsl:param name="input" select="'this_string_will_be_camel_case'"/>

    <xsl:variable name="camel">
        <xsl:variable name="sub" select="substring-before($input,'_')"/> 
        <xsl:choose>
            <xsl:when test="not(string-length($sub)=0)">
                <xsl:value-of select="$sub"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$input"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:variable name="case">
        <xsl:value-of select="translate(
            substring($camel,1,1),
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ')"/>
    </xsl:variable>

    <xsl:value-of select="concat($case,substring($camel,2))"/>

    <xsl:if test="not(string-length($camel)=0)">
        <xsl:call-template name="CamelCase">
            <xsl:with-param name="input" select="substring-after($input,'_')"/>
        </xsl:call-template>
    </xsl:if>

</xsl:template>

例如,如果您将其称为:

For instance, if you call it as:

 <xsl:call-template name="CamelCase"/>

它会返回:

 ThisStringWillBeCamelCase

这篇关于我如何不在我的 xslt 代码中重复重复的逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆