Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format a string to Pascal case in XSLT?

I'm trying to format strings in XSLT that needs to be in pascal case to be used appropriately for the application I'm working with.

For example:

this_text would become ThisText
this_long_text would become ThisLongText

Is it possible to also set this up where I can send an input to the format so I do not have to recreate the format multiple times?

like image 756
OpenDataAlex Avatar asked Apr 15 '10 16:04

OpenDataAlex


People also ask

How do I format XSLT?

The format date on XSLT Technique uses a template dealing with Gregorian Time and Internationalization. The parameter for this date function is a picture string that encloses code in square brackets(D[07]). The name attributes declare date format or default date. It should be declared only once.

Is XSLT case sensitive?

xslt. The values of the tableName XML element is case insensitive.

What is string length in XSLT?

string-length() Function — Returns the number of characters in the string passed in as the argument to this function. If no argument is specified, the context node is converted to a string and the length of that string is returned.


1 Answers

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

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

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="text()">
  <xsl:call-template name="Pascalize">
   <xsl:with-param name="pText" select="concat(., '_')"/>
  </xsl:call-template>
 </xsl:template>

 <xsl:template name="Pascalize">
  <xsl:param name="pText"/>

  <xsl:if test="$pText">
   <xsl:value-of select=
    "translate(substring($pText,1,1), $vLower, $vUpper)"/>

   <xsl:value-of select="substring-before(substring($pText,2), '_')"/>

   <xsl:call-template name="Pascalize">
     <xsl:with-param name="pText"
       select="substring-after(substring($pText,2), '_')"/>
   </xsl:call-template>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<t>
  <a>this_text</a>
  <b>this_long_text</b>
</t>

produces the desired result:

<t>
    <a>ThisText</a>
    <b>ThisLongText</b>
</t>

BTW, this is camelCase and this is PascalCase

like image 156
Dimitre Novatchev Avatar answered Oct 19 '22 05:10

Dimitre Novatchev