How to change first letter to lowercase using XPath or XSLT
Asked Answered
R

2

5

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?

Ratio answered 20/10, 2014 at 14:14 Comment(1)
how can i use fn:lower-case() in an expression? using it like this: <property expression="{fn:lower-case(get-property('MARKET'))}" name="LOWERCASE_MARKET" scope="default" type="STRING"/> gives an error: Invalid XPath expression for attribute 'expression'Buoyant
T
6

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))"/>
Tyus answered 20/10, 2014 at 14:21 Comment(0)
F
1

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>
Fishman answered 20/10, 2014 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.