How to get xml Nodes which are in lower case using XSLT 1.0

247 Views Asked by At

I need to get XML nodes which are in lower case and values of it using XSLT 1.0 and display the output as XML

        <main>
           <ACAT>Cat Name A </ACAT>
           <bcat>Cat Name b </bcat>
           <ccat>Cat Name c </ccat>
           <dcat>Cat Name d </dcat>
           <ECAT>Cat Name E </ECAT>
           <fcat>Cat Name f </fcat>
        </main>

Mu desired output is

        <main>
           <bcat>Cat Name b </bcat>
           <ccat>Cat Name c </ccat>
           <dcat>Cat Name d </dcat>
           <fcat>Cat Name f </fcat>
        </main>
1

There are 1 best solutions below

0
On BEST ANSWER

All you need is the identity template to copy existing nodes...

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

Then another template to ignore nodes that aren't lower-case. In XSLT 1.0 this can be done by using the translate statement, to translate uppercase letters to lowercase, and checking if the result is different

<xsl:template match="*[translate(name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') != name()]" />

Try this XSLT

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

    <xsl:template match="*[translate(name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') != name()]" />

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