Problem to remove underscore in template

M

Mikael Petterson

Hi,

I got some help to go from:

bbBusState to BB_BUS_STATE.

However I found out :-( that the xml contained attributes like:

TxDeviceGroup and that becomes:

_TX_DEVICE_GROUP


This is the code I got from Marrow (thanks Marrow :)) (See below)
I must admit (Marrow) that I do not fully understand what the code
does so I am having problems to modify it to remove the underscore
for attributes that begins with a cap.

I have tried to add the following code (see My code below).

My strings became a mess :-(

//Mikael

My code
=======
<xsl:choose>
<xsl:when test="starts-with($u-str,'|')">
<xsl:variable name="u-str-len" select="string-length($u-str)"/>
<xsl:variable name="u-str-rest" select="substring($u-str,2,$u-str-len)"/>
<xsl:variable name="str-first-chr" select="substring($str,1,1)"/>
<xsl:call-template name="Camel2Underscore">
<xsl:with-param name="u-str" select="concat($str-first-chr,
$u-str-rest)"/>
</xsl:call-template>
</xsl:when>

=======================================================

<xsl:template name="Camel2Underscore">
<xsl:param name="str"/>
<xsl:param name="u-str"
select="translate($str,$ucase,'||||||||||||||||||||||||||')"/>

<!-- Here I tested to add my code -->

<xsl:choose>
<xsl:when test="substring($u-str,1,1) = '|'">
<xsl:text>_</xsl:text>
<xsl:value-of select="substring($str,1,1)"/>
<xsl:call-template name="Camel2Underscore">
<xsl:with-param name="str" select="substring($str,2)"/>
<xsl:with-param name="u-str" select="substring($u-str,2)"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="contains($u-str,'|')">
<xsl:variable name="runlen"
select="string-length(substring-before($u-str,'|'))"/>
<xsl:value-of
select="translate(substring($str,1,$runlen),$lcase,$ucase)"/>
<xsl:call-template name="Camel2Underscore">
<xsl:with-param name="str" select="substring($str,$runlen + 1)"/>
<xsl:with-param name="u-str" select="substring($u-str,$runlen +
1)"/>
</xsl:call-template>
</xsl:when>
<xsl:eek:therwise>
<xsl:value-of select="translate($str,$lcase,$ucase)"/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
 
D

Dimitre Novatchev

Without having to know the code that you were using, there are simple ways
to remove the leading underscore:

1.
<xsl:variable name="vresult1">
<xsl:call-template name="capitalize">
<xsl:with-param name="str" select="$someStr"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="theResult" select="substring($vresult1, 1 +
contains($allCaps, substring($someStr, 1, 1)))"/>

2.
<xsl:variable name="vresult1">
<xsl:call-template name="capitalize">
<xsl:with-param name="str" select="substring($someStr, 2)"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="theResult" select="concat(translate(substring($someStr,
1, 1), $lowercase, $allCaps), $vresult1)"/>


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
M

Mikael Petterson

Hmmmmmmm.... Simple for you yeah. Not for me.

You first define a variable called vresult1 then you call a
template called capitalize. And you send, I assume _TR_DEVICE_GROUP to
that template. Then you define a variable that is called theResult. I
guess this is where we get the TR_DEVICE_GROUP output. Which part do I
need to implement?


//Mikael
 
D

Dimitre Novatchev

Mikael Petterson said:
Hmmmmmmm.... Simple for you yeah. Not for me.

You first define a variable called vresult1 then you call a
template called capitalize. And you send, I assume _TR_DEVICE_GROUP to
that template.

No. I pass to it "TrDeviceGroup"

As result the value of $vresult1 will be _TR_DEVICE_GROUP

The "capitalize" template is all you had at the moment (regardless if it
were Marrow's solution, or the FXSL-based solution I proposed).
Then you define a variable that is called theResult. I
guess this is where we get the TR_DEVICE_GROUP output.
Yes.

Which part do I
need to implement?

Absolutely nothing to implement -- just define the variables in this way.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL
 
M

Mikael Petterson

Hi,

Thanks for the explanation. It really helped me out. You saved me since
I needed to write a code generator and discovered some inconsistencies.

Thanks again!! Have a nice evening!

//Mikael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top