Converting XML element names to camelCase format using XSLT

xslt camel case
xslt capitalize first letter
xslt functions example
xslt substring
xslt custom functions
xslt string functions
xslt 1.0 string functions
xslt contains

I need to transform all element names in a XML to camelCase naming strategy with XSLT.

Here is a response from coming server:

<ROOT_ELEMENT>
        <ELEMENT_1>2595959584</ELEMENT_1>
        <Element_Two>Lorem</Element_Two>
        <eLement_Three>Ipsum</eLement_Three>

        <BIRTH_date>
           <Day>21</Day>
           <Month>09</Month>
           <Year>1955</Year>
        </BIRTH_date>
 </ROOT_ELEMENT>

Here is what I want to transform:

<rootElement>
        <element1>2595959584</element1>
        <elementTwo>Lorem</elementTwo>
        <elementThree>Ipsum</elementThree>      
        <birthDate>
           <day>21</day>
           <month>09</month>
           <year>1955</year>
        </birthDate>
</rootElement>

Any help would be much appreciated.

Assuming there will be at most one _ separator in the element name, and assuming that the names contain no non-ASCII characters, you can use:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:param name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:param name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="name" select="name()" />
    <xsl:variable name="word2" select="substring-after($name, '_')" />
    <xsl:variable name="new-name">
        <xsl:value-of select="translate(substring-before(concat($name, '_'), '_'), $upper-case, $lower-case)"/> 
        <xsl:value-of select="translate(substring($word2, 1, 1), $lower-case, $upper-case)"/>   
        <xsl:value-of select="translate(substring($word2, 2), $upper-case, $lower-case)"/>  
    </xsl:variable>
    <xsl:element name="{$new-name}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Added:

If (as it seems) you're using a processor that supports the XSLT str:tokenize() extension function, you could do:

XSLT 1.0 + EXSLT

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings"
extension-element-prefixes="str">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="*">
    <xsl:param name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    <xsl:param name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>
    <xsl:variable name="new-name">
        <xsl:for-each select="str:tokenize(name(), '_')">
            <xsl:choose>
                <xsl:when test="position() = 1">
                    <xsl:value-of select="translate(., $upper-case, $lower-case)"/>   
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="translate(substring(., 1, 1), $lower-case, $upper-case)"/>   
                    <xsl:value-of select="translate(substring(., 2), $upper-case, $lower-case)"/>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each> 
    </xsl:variable>
    <xsl:element name="{$new-name}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

This removes the limit on the number of _ separators an element name can have. The other restriction remains: every possible character that can appear in an element name and has upper-case and lower-case variants must be listed in the $upper-case and $lower-case parameters.

Camel Case strings in XSLT, I need to transform all element names in a XML to camelCase naming strategy with XSLT. Here is a response from coming server: <ROOT_ELEMENT>� Convert XML element names to camel-case. to select all the same xml elements but with camel case. So for example my new xml after XSLT transformation needs to

In XSLT 3.0 it's

string-join(tokenize(name(), '_') ! 
  (upper-case(substring(., 1, 1)) || lower-case(substring(., 2))))

In 1.0 it's much more difficult, but still possible (at least, if your element names only use ASCII letters). You'll need a recursive named template to do the tokenizing (you can find one at http://www.exslt.org, look for str:tokenize in the strings module), and within the named template where you are processing one token, you'll need to use translate($x, 'abc...z', 'ABC...Z') to do the case conversion.

You can avoid the recursion and simplify the logic if you know there will only be one underscore in the name; in that case you can use substring-before() and substring-after() to get the two parts of the name.

Transformation of XML Element Names, I have to convert to HTML and format for display. In this case, we need the string to be normalized. The XML we receive has the possibility, and most of the time is, � XSL is great for transforming an XML file. The following XSL can be used to convert all element names into lower case.

One more way to capitalize first letter in XSLT 1.0 is as below:

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

<xsl:output method="xml" indent="yes" />

<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />

<xsl:template match="*">
    <xsl:variable name="convertElementName">
        <xsl:choose>
            <xsl:when test="contains(local-name(.),'_')">
                <xsl:value-of
                    select="concat(translate(substring-before(local-name(.),'_'), $uppercase, $smallcase),
                            translate(substring(substring-after(local-name(.),'_'),1,1), $smallcase, $uppercase),
                            translate(substring(substring-after(local-name(.),'_'),2,string-length(local-name(.))-1), $uppercase, $smallcase))" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="translate(local-name(.), $uppercase, $smallcase)" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

    <xsl:element name="{$convertElementName}">
        <xsl:apply-templates select="@* | node()" />
    </xsl:element>
</xsl:template>

<xsl:template match="@*">
    <xsl:attribute name="{local-name(.)}">
        <xsl:value-of select="." />
    </xsl:attribute>
</xsl:template>
</xsl:stylesheet>

Hundreds of useful examples, The identity transformation ID creates asXML with XML element names in uppercase letters. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" I have a situation where I am receiving data from a web service and the data is in all caps. I have to convert to HTML and format for display. In this case, we need the string to be normalized. The XML we receive has the possibility, and most of the time is, large, sometimes being over a MB in size. Performance is an issue.

<xsl:template match="*">
    <xsl:choose>
        <xsl:when test="contains(name(), '_')">
            <xsl:element name="{concat(substring-before(lower-case(name()), '_'), substring(substring-after(upper-case(name()), '_'), 1,1), substring(substring-after(lower-case(name()), '_'), 2))}">
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
                <xsl:element name="{lower-case(name())}">
                    <xsl:apply-templates/>
                </xsl:element>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

One more Answer if it is possible use this code.

Converting Case - XSLT Cookbook [Book], Hundreds of reusable examples of XSLT functions from the FunctX XSLT functx :camel-case-to-words, Turns a camelCase string into space-separated words functx:distinct-element-names, The distinct names of all elements in an XML functx:format-as-title-en, Moves title words like "the" and "a" to the end of strings. The XSL code is written within the XML document with the extension of (.xsl). In other words, an XSLT document is a different kind of XML document. XML Namespace: XML Namespaces are the unique names . XML Namespace is a mechanism by which element or attribute is assigned to a group. XML Namespace is used to avoid the name conflicts in the XML

Convert lower to upper case for XML element without any attributes , Converting Case Problem You want to convert an uppercase string to Solution Use the XSLT translate( ) function. Selection from XSLT Cookbook [Book] method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <xsl:variable name="test" select=" 'The rain in Spain falls mainly in the plain'� So in the example above, the checker element would not get copied into the new file because it has a val attribute and the corresponding element in the look-up file is the first one and it has an f-element whose name is val. That's enough for me to know that it should be disregared.

XML Elements, I got anothe code bit from Oxygen but that is converting all value to upper case. I tried my best but not able <xsl:stylesheet version="1.0" xmlns:xsl="http://www. w3.org/1999/XSL/Transform" <xsl:variable name="upperCase"� Copying, Deleting, and Renaming Elements. June 7, 2000. Bob DuCharme. Welcome to "Transforming XML." Each column will explain how to handle two or three basic document manipulation tasks using the W3C Standard that was spun off from the Extensible Stylesheet Language (XSL): the XSL Transformations Language, or XSLT.

Convert hyphenated tags in XML to camelCase using java regex , XSLT Introduction XSL Languages XSLT Transform XSLT <template> XSLT < value-of> XSLT An XML element is everything from (including) the element's start tag to (including) the element's end tag. Any name can be used, no words are reserved (except xml). Camel case is a common naming rule in JavaScripts . The XSL Transformer fully supports XML namespaces, but the declarations MUST be explicit and MUST be on the root XML element of both your XML file and your XSL file. See the XSLT Examples section for details. *The maximum size limit for file upload is 2 megabytes.

Comments
  • Thanks for your reply. But i just have met with XSLT today. I haven't known anything about XSLT yet. Is there any complete solution for this problem.
  • I'm not prepared to write XSLT 1.0 code for a problem like this, it's absolutely no fun at all. Move forward to a later version, and people like me are more likely to offer help.
  • Actually, it's more difficult in XSLT 3.0 too: the above will return PascalCase, while the requirement is for camelCase (first character is in lowercase).
  • This question is tagged XSLT 1.0. Your answer requires XSLT 2.0.
  • Where does lower-case function come from? I got this error. javax.xml.transform.TransformerException: Could not find function: lower-case
  • This answer was XSLT 2.0