Developer Blog - Inventic.eu
  • Skipper - The ORM Designer
  • VsBuilds - Parallel building
  • Pulpo - Free Skipper CLI

Tag: #xslt

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>
		&lt;xsl:variable name=&quot;Lower&quot;&gt;abcdefghijklmnopqrstuvwxyz&lt;/xsl:variable&gt;</p>
		&lt;xsl:for-each select=&quot;str:split($text,'')&quot;&gt;
			&lt;xsl:choose&gt;
				&lt;xsl:when test=&quot;contains($Upper,node())&quot;&gt;
					&lt;xsl:if test=&quot;position()&gt;1&quot;&gt;
						&lt;xsl:text&gt;-&lt;/xsl:text&gt;
					&lt;/xsl:if&gt;
					&lt;xsl:value-of select=&quot;translate(node(),$Upper,$Lower)&quot;/&gt;
				&lt;/xsl:when&gt;
				&lt;xsl:otherwise&gt;
					&lt;xsl:value-of select=&quot;node()&quot;/&gt;
				&lt;/xsl:otherwise&gt;
			&lt;/xsl:choose&gt;
		&lt;/xsl:for-each&gt;
	&lt;/xsl:template&gt;

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

28 Aug 2013

Posted by: ludek.vodicka

Programming #xslt

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:
		&lt;xsl:call-template name=&quot;ConvertXmlStyleToCamelCase&quot;&gt;
			&lt;xsl:with-param name=&quot;text&quot;&gt;orm-designer&lt;/xsl:with-param&gt;
		&lt;/xsl:call-template&gt;</p>
<p>Second:
		&lt;xsl:call-template name=&quot;ConvertXmlStyleToCamelCase&quot;&gt;
			&lt;xsl:with-param name=&quot;text&quot;&gt;orm-designer&lt;/xsl:with-param&gt;
			&lt;xsl:with-param name=&quot;firstLower&quot; select=&quot;false()&quot;/&gt;
		&lt;/xsl:call-template&gt;

Result:

First: ormDesigner
Second: OrmDesinger

XSLT Template:

	&lt;!-- =========================================================================== --&gt;
	&lt;!-- === Convert camel-case-text to CameCaseText                             === --&gt;
	&lt;!-- === (c) Inventic s.r.o. ORM Designer team (http://www.orm-designer.com) === --&gt;
	&lt;!-- =========================================================================== --&gt;
	&lt;xsl:template name=&quot;ConvertXmlStyleToCamelCase&quot;&gt;
		&lt;xsl:param name=&quot;text&quot;/&gt;
		&lt;xsl:param name=&quot;firstLower&quot; select=&quot;true()&quot;/&gt;</p>
		&lt;xsl:variable name=&quot;Upper&quot;&gt;ABCDEFGHIJKLMNOPQRSTUVQXYZ&lt;/xsl:variable&gt;
		&lt;xsl:variable name=&quot;Lower&quot;&gt;abcdefghijklmnopqrstuvwxyz&lt;/xsl:variable&gt;</p>
		&lt;xsl:for-each select=&quot;str:split($text,'-')&quot;&gt;
			&lt;xsl:choose&gt;
				&lt;xsl:when test=&quot;position()=1 and $firstLower = true()&quot;&gt;
					&lt;xsl:value-of select=&quot;substring(node(),1,1)&quot;/&gt;
				&lt;/xsl:when&gt;
				&lt;xsl:otherwise&gt;
					&lt;xsl:value-of select=&quot;translate(substring(node(),1,1),$Lower,$Upper)&quot;/&gt;
				&lt;/xsl:otherwise&gt;
			&lt;/xsl:choose&gt;
			&lt;xsl:value-of select=&quot;substring(node(),2,string-length(node()))&quot;/&gt;
		&lt;/xsl:for-each&gt;
	&lt;/xsl:template&gt;

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

28 Aug 2013

Posted by: ludek.vodicka

Programming #xslt