xsl syntax to generate all caps with _

Mikael Petterson

Usenet Archive
Joined
Nov 3, 2002
Messages
27
Reaction score
0
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
 

Marrow

Usenet Archive
Joined
Apr 9, 2001
Messages
408
Reaction score
0
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:eek:utput 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:eek:therwise>
<xsl:value-of select="translate($str,$lcase,$ucase)"/>
</xsl:eek:therwise>
</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
 

Dimitre Novatchev

Usenet Archive
Joined
Nov 7, 2000
Messages
737
Reaction score
0
Mikael Petterson said:
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???

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"<xsl:import href="str-map.xsl"/>

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

<xsl:eek:utput 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
 

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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,802
Members
249,594
Latest member
henrywalsh

Latest Threads

Top