XSLT snippet to convert string from CamelCase to hyphens (Hyphenize)

Here is a snippet to convert string in XSLT template from CamelCase to hyphenized text. (Developed today while creating new import/export templates for Doctrine2 YAML support).

Usage:

<xsl:call-template name="ConvertCamelToXmlStyle">
	<xsl:with-param name="text">OrmDesigner</xsl:with-param>
</xsl:call-template>

Result:

orm-designer

XSLT Template:

	<!-- =========================================================================== -->
	<!-- === Convert CameCaseText to camel-case-text                             === -->
	<!-- === (c) Inventic s.r.o. ORM Designer team (http://www.orm-designer.com) === -->
	<!-- =========================================================================== -->
	<xsl:template name="ConvertCamelToXmlStyle">
		<xsl:param name="text"/>
		<xsl:variable name="Upper">ABCDEFGHIJKLMNOPQRSTUVQXYZ ,</xsl:variable>
		<xsl:variable name="Lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>

		<xsl:for-each select="str:split($text,'')">
			<xsl:choose>
				<xsl:when test="contains($Upper,node())">
					<xsl:if test="position()>1">
						<xsl:text>-</xsl:text>
					</xsl:if>
					<xsl:value-of select="translate(node(),$Upper,$Lower)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="node()"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:for-each>
	</xsl:template>

Feel free to use in whatever way you find it useful.

XSLT snippet to convert string from hyphens to CamelCase

Here is a snippet to convert string in XSLT template from hyphenized text to CamelCase. (Developed today while creating new import/export templates for Doctrine2 YAML support).

Usage:

First:
		<xsl:call-template name="ConvertXmlStyleToCamelCase">
			<xsl:with-param name="text">orm-designer</xsl:with-param>
		</xsl:call-template>

Second:
		<xsl:call-template name="ConvertXmlStyleToCamelCase">
			<xsl:with-param name="text">orm-designer</xsl:with-param>
			<xsl:with-param name="firstLower" select="false()"/>
		</xsl:call-template>

Result:

First: ormDesigner
Second: OrmDesinger

XSLT Template:

	<!-- =========================================================================== -->
	<!-- === Convert camel-case-text to CameCaseText                             === -->
	<!-- === (c) Inventic s.r.o. ORM Designer team (http://www.orm-designer.com) === -->
	<!-- =========================================================================== -->
	<xsl:template name="ConvertXmlStyleToCamelCase">
		<xsl:param name="text"/>
		<xsl:param name="firstLower" select="true()"/>

		<xsl:variable name="Upper">ABCDEFGHIJKLMNOPQRSTUVQXYZ</xsl:variable>
		<xsl:variable name="Lower">abcdefghijklmnopqrstuvwxyz</xsl:variable>

		<xsl:for-each select="str:split($text,'-')">
			<xsl:choose>
				<xsl:when test="position()=1 and $firstLower = true()">
					<xsl:value-of select="substring(node(),1,1)"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="translate(substring(node(),1,1),$Lower,$Upper)"/>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:value-of select="substring(node(),2,string-length(node()))"/>
		</xsl:for-each>
	</xsl:template>

Feel free to use in whatever way you find it useful.