Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xml

xslt

exslt

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)

like image 573
Pacerier Avatar asked Jan 17 '26 09:01

Pacerier


1 Answers

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.

like image 69
Michael Kay Avatar answered Jan 20 '26 01:01

Michael Kay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!