J.P
J.P

Reputation: 565

Converting XML elements from lowercase to uppercase using XSLT in JAVA

I have the below XML which contains all the elements in lowercase.

<data>
    <employee>
        <id>2784</id>
        <employeeFirstName></employeeFirstName>
        <employeeLastName nil="true"/></employeeLastName>
    </employee>
</data>

Here my requirement is to convert all the elements to uppercase.

<DATA>
        <EMPLOYEE>
            <ID>2784</ID>
            <EMPLOYEEFIRSTNAME></EMPLOYEEFIRSTNAME>
            <EMPLOYEELASTNAME nil="true"/></EMPLOYEELASTNAME>
        </EMPLOYEE>
</DATA>

Anyone please help me to convert this xml using XSLT.

Upvotes: 1

Views: 6472

Answers (2)

michael.hor257k
michael.hor257k

Reputation: 116959

In XSLT 2.0, this is rather trivial:

<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:element name="{upper-case(name())}">
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

nawazlj
nawazlj

Reputation: 821

Simplest way to do in XSLT 1.0

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XS/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="*">
        <xsl:element name="{
            translate(name(.),
            'abcdefghijklmnopqrstuvwxyz',
            'ABCDEFGHIJKLMNOPQRSTUVWXYZ')}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

It will produce the below output

<DATA>
    <EMPLOYEE>
        <ID>2784</ID>
        <EMPLOYEEFIRSTNAME/>
        <EMPLOYEELASTNAME nil="true"/>
    </EMPLOYEE>
</DATA>

Upvotes: 0

Related Questions