Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change first letter to lowercase using XPath or XSLT

I am using wso2esb-4.8.1.

I wish to change my request first letter to lowercase. I am getting request parameters like this:

 <property name="Methodname" expression="//name/text()" scope="default" type="STRING"/>

In this way I am getting names like

GetDetails, CallQuerys, ChangeService...

Whereas I wish to change all the names to be like this:

getDetails, callQuerys, changeService...

If I wanted to upper or lower the case of the entire name, I could use XPath's fn:upper-case() and fn:lower-case() functions, but my requirement is different.

How can I change all the first letters only to lowercase?

Is it possible with XPath or XSLT?

like image 633
user3595078 Avatar asked Oct 20 '14 14:10

user3595078


2 Answers

XPath 1.0:

<property name="Methodname" scope="default" type="STRING" 
          expression="concat(translate(substring(//name/text(), 1, 1), 
                                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                       'abcdefghijklmnopqrstuvwxyz'), 
                             substring(//name/text(), 2))"/>

XPath 2.0:

<property name="Methodname" scope="default" type="STRING" 
          expression="concat(lower-case(substring(//name/text(), 1, 1)), 
                             substring(//name/text(), 2))"/>
like image 77
kjhughes Avatar answered Oct 04 '22 21:10

kjhughes


Just to add to kjhughes answer / this answer here, you'll probably want to use this in conjunction with the identity transform to copy the remainder of the document unscathed:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

    <xsl:template match="@name[ancestor::property]">
        <xsl:attribute name="name">
            <xsl:value-of select="concat(translate(substring(., 1, 1), 
                           'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                           'abcdefghijklmnopqrstuvwxyz'), substring(., 2))"/>
        </xsl:attribute>
    </xsl:template>

</xsl:stylesheet>
like image 42
StuartLC Avatar answered Oct 04 '22 20:10

StuartLC