how do i not repeat repeated logic in my xslt code?

百般思念 提交于 2019-12-23 20:26:33

问题


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?

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 &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>

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


回答1:


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"/>

it will return:

 ThisStringWillBeCamelCase



回答2:


A better way is to write it in XSLT 2.0:

<xsl:analyze-string select="$in" regex="_.">
  <xsl:matching-substring>
    <xsl:value-of select="uppercase(substring(., 2, 1))"/>
  </xsl:matching-substring>
  <xsl:non-matching-substring>
     <xsl:value-of select="value-of select="."/>
  </xsl:non-matching-substring>
</xsl:analyze-string>

I'm afraid however hard you try, solving character manipulation problems in XSLT 1.0 is going to be tedious and verbose.



来源:https://stackoverflow.com/questions/6296656/how-do-i-not-repeat-repeated-logic-in-my-xslt-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!