在 JAVA 中使用 XSLT 将 XML 元素从小写转换为大写

Converting XML elements from lowercase to uppercase using XSLT in JAVA

我有下面的 XML,其中包含小写的所有元素。

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

这里我的要求是把所有的元素都转成大写

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

任何人请帮助我使用 XSLT 转换此 xml。

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>

它将产生以下输出

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

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

</xsl:stylesheet>