Converting XML Node Names to Uppercase (XSLT 1.0)

你说的曾经没有我的故事 提交于 2019-12-05 17:57:09

In XSLT 1.0 I would use the Identity Transformation and the translate() function:

<xsl:stylesheet 
    xmlns:xsl="http://www.w3.org/1999/XSL/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>

This transform will "upper case" elements only, not the attributes. It is easy anyway extend it to attributes just by changing the match pattern of the second template to *|@*.


As per the comments below and the specs, translate() function is not a sufficient solution for case conversion in all languages.

@empo 's solution is the one that can be used if the names consisted only of latin characters. If this isn't the case one can use a similar XSLT 2.0 transformation which replaces the translate() function in @empo's solution with the standard XPath 2.0 function upper-case().

Also, if you have just a few sylesheets, it may be much easier to modify the stylesheets themselves to handle mix-cased names:

That is:

Instead of

<xsl:template match="SOMENAME">

use:

 <xsl:template 
   match="*['SOMENAME' = translate(., $Lowercase, $Uppercase)]">
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!