BizTalk Utilities CV ,   Jobs ,   Code library  
 
 
Page 1 of 2

 

Previous Page  

XSLT Case Conversion Solution, cont.

Download the source

 

Full Source Code

XML File

We are passing information about how a sentence should be converted.

<?xml version="1.0"?>
<stringfuncexample>
<convert tocase='proper'>HOW ARE YOU TODAY?</convert>
<convert tocase='proper'>RECURSION is really fun?</convert>
<convert tocase='proper'>XSLT can BE WRITTEN like a proCEDUral Language?</convert>
<convert tocase='upper'>ConVerTINg to uppEr Case IS an intereSTING exerCIse?</convert>
<convert tocase='lower'>ConVerTINg to lowEr Case IS an intereSTING exerCIse?</convert>
<convert tocase='proper'>ConVerTINg to propEr Case IS an intereSTING exerCIse?</convert>
</stringfuncexample>

XSL File

In the following XSL we examine the tocase attribute, to determine whether our text should be lower, upper or proper case, which we then output into a table.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" />

  <xsl:variable name="lcletters">
  abcdefghijklmnopqrstuvwxyz</xsl:variable>

  <xsl:variable name="ucletters">
  ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable>

  <xsl:template match="/">
    <html>
      <head />

      <body>
        <table width='90%' border='1'>
          <tr>
            <td>Original Value</td>

            <td>Conversion</td>

            <td>New Value</td>
          </tr>

          <xsl:for-each select="stringfuncexample/convert">
            <tr>
              <xsl:if test="position() mod 2 = 1">
                <xsl:attribute name='bgcolor'>gray</xsl:attribute>
              </xsl:if>

              <td>
                <xsl:value-of select="." />
              </td>

              <td>
                <xsl:value-of select="@tocase" />
              </td>

              <td>
                <xsl:call-template name='convertcase'>
                  <xsl:with-param name='toconvert' select='.' />

                  <xsl:with-param name='conversion'
                  select='@tocase' />
                </xsl:call-template>
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>

  <xsl:template name='convertcase'>
    <xsl:param name='toconvert' />

    <xsl:param name='conversion' />

    <xsl:choose>
      <xsl:when test='$conversion="lower"'>
        <xsl:value-of
        select="translate($toconvert,$ucletters,$lcletters)" />
      </xsl:when>

      <xsl:when test='$conversion="upper"'>
        <xsl:value-of
        select="translate($toconvert,$lcletters,$ucletters)" />
      </xsl:when>

      <xsl:when test='$conversion="proper"'>
        <xsl:call-template name='convertpropercase'>
          <xsl:with-param name='toconvert'>
            <xsl:value-of
            select="translate($toconvert,$ucletters,$lcletters)" />
          </xsl:with-param>
        </xsl:call-template>
      </xsl:when>

      <xsl:otherwise>
        <xsl:value-of select='$toconvert' />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template name='convertpropercase'>
    <xsl:param name='toconvert' />

    <xsl:if test="string-length($toconvert) > 0">
      <xsl:variable name='f'
      select='substring($toconvert, 1, 1)' />

      <xsl:variable name='s' select='substring($toconvert, 2)' />

      <xsl:call-template name='convertcase'>
        <xsl:with-param name='toconvert' select='$f' />

        <xsl:with-param name='conversion'>upper</xsl:with-param>
      </xsl:call-template>

      <xsl:choose>
        <xsl:when test="contains($s,' ')">
        <xsl:value-of select='substring-before($s," ")' />

          
        <xsl:call-template name='convertpropercase'>
          <xsl:with-param name='toconvert'
          select='substring-after($s," ")' />
        </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
          <xsl:value-of select='$s' />
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

HTML Output

Original ValueConversionNew Value
HOW ARE YOU TODAY? proper How Are You Today?
RECURSION is really fun? proper Recursion Is Really Fun?
XSLT can BE WRITTEN like a proCEDUral Language? proper Xslt Can Be Written Like A Procedural Language?
ConVerTINg to uppEr Case IS an intereSTING exerCIse? upper CONVERTING TO UPPER CASE IS AN INTERESTING EXERCISE?
ConVerTINg to lowEr Case IS an intereSTING exerCIse? lower converting to lower case is an interesting exercise?
ConVerTINg to propEr Case IS an intereSTING exerCIse? proper Converting To Proper Case Is An Interesting Exercise?

Page 1 of 2

 

Previous Page  
 

Recent Jobs

Software Developers Needed in Charl
Sr. Software Engineer - Analytics
Immediate Mainframe openings for Ch
Immediate TANDEM-TAL openings for C
Immediate ASP.NET/C# Openings for C

View all Jobs (Add yours)
View all CV (Add yours)



conference calling
swimming pool builder
spfxmasks
water softener
Teleconference
Host Department NOLIMIT Web Hosting
MSN
sunglasses


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP