Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 07:09 AM
Mikael Petterson
Guest
 
Posts: n/a
Default xsl syntax to generate all caps with _

Hi,

I have the following in my xml file:

<attribute name="bbBusState">

I need to generate to the following:

public static final String BB_BUS_STATE_ATTR_TYPE.

Any hints???

//Mikael

  #2  
Old July 20th, 2005, 07:09 AM
Marrow
Guest
 
Posts: n/a
Default Re: xsl syntax to generate all caps with _

Hi Mikael,

Try the translate() function, e.g.

<xsl:value-of
select="translate(@name,'abcdefghijklmnopqrstuvwxy z','ABCDEFGHIJKLMNOPQRSTUV
WXYZ')"/>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator

"Mikael Petterson" <mikaelpetterson@hotmail.com> wrote in message
news:9WK7b.3317$P51.5569@amstwist00...[color=blue]
> Hi,
>
> I have the following in my xml file:
>
> <attribute name="bbBusState">
>
> I need to generate to the following:
>
> public static final String BB_BUS_STATE_ATTR_TYPE.
>
> Any hints???
>
> //Mikael
>[/color]


  #3  
Old July 20th, 2005, 07:09 AM
Marrow
Guest
 
Posts: n/a
Default Re: xsl syntax to generate all caps with _

Ooops, didn't read the question properly...

Try something like...

== XML =========================================
<?xml version="1.0"?>
<root>
<attribute name="bbBusState"/>
</root>
== end of XML ==================================

== XSL =========================================
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:variable name="lcase" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="ucase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<xsl:template match="root">
<xsl:apply-templates select="attribute"/>
</xsl:template>

<xsl:template match="attribute">
<xsl:text>public static final String </xsl:text>
<xsl:call-template name="Camel2Underscore">
<xsl:with-param name="str" select="@name"/>
</xsl:call-template>
<xsl:text>_ATTR_TYPE</xsl:text>
</xsl:template>

<xsl:template name="Camel2Underscore">
<xsl:param name="str"/>
<xsl:param name="u-str"
select="translate($str,$ucase,'||||||||||||||||||| |||||||')"/>
<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:otherwise>
<xsl:value-of select="translate($str,$lcase,$ucase)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
== end of XSL ==================================

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator


"Marrow" <marrow-NO-@-SPAM-marrowsoft.com> wrote in message
news:HKQ7b.1169$lj5.907367@newsfep2-win.server.ntli.net...[color=blue]
> Hi Mikael,
>
> Try the translate() function, e.g.
>
> <xsl:value-of
>[/color]
select="translate(@name,'abcdefghijklmnopqrstuvwxy z','ABCDEFGHIJKLMNOPQRSTUV[color=blue]
> WXYZ')"/>
>
> Hope this helps
> Marrow
> http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
> http://www.topxml.com/Xselerator
>
> "Mikael Petterson" <mikaelpetterson@hotmail.com> wrote in message
> news:9WK7b.3317$P51.5569@amstwist00...[color=green]
> > Hi,
> >
> > I have the following in my xml file:
> >
> > <attribute name="bbBusState">
> >
> > I need to generate to the following:
> >
> > public static final String BB_BUS_STATE_ATTR_TYPE.
> >
> > Any hints???
> >
> > //Mikael
> >[/color]
>
>[/color]


  #4  
Old July 20th, 2005, 07:09 AM
Dimitre Novatchev
Guest
 
Posts: n/a
Default Re: xsl syntax to generate all caps with _


"Mikael Petterson" <mikaelpetterson@hotmail.com> wrote in message
news:9WK7b.3317$P51.5569@amstwist00...[color=blue]
> Hi,
>
> I have the following in my xml file:
>
> <attribute name="bbBusState">
>
> I need to generate to the following:
>
> public static final String BB_BUS_STATE_ATTR_TYPE.
>
> Any hints???[/color]

Hi Mikael,

One can simply use the "str-map" template of FXSL as follows:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:testmap="my:testmap"
exclude-result-prefixes="xsl testmap"[color=blue]
>[/color]
<xsl:import href="str-map.xsl"/>

<xsl:variable name="vLower" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="vUpper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="/">
<xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>
<xsl:variable name="vName">
<xsl:call-template name="str-map">
<xsl:with-param name="pFun" select="$vTestMap"/>
<xsl:with-param name="pStr" select="*/@name"/>
</xsl:call-template>
</xsl:variable>

public static final String <xsl:text/>
<xsl:value-of select="$vName"/>
</xsl:template>

<testmap:testmap/>
<xsl:template match="testmap:*">
<xsl:param name="arg1"/>

<xsl:if test="contains($vUpper, $arg1)">_</xsl:if>

<xsl:value-of select="translate($arg1, $vLower, $vUpper)"/>
</xsl:template>

</xsl:stylesheet>

When this transformation is applied on your source.xml:

<attribute name="bbBusState"/>

the wanted output is produced:

public static final String BB_BUS_STATE



Hope this helped.


=====
Cheers,

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


 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles