我有以下要求:
<TAG_ONE>
<TAG_TWO>Abc</TAG_TWO>
<TAG_THREE>Xyz</TAG_THREE>
</TAG_ONE>
Run Code Online (Sandbox Code Playgroud)
我需要将其转换为以下XML:
<TagOne>
<TagTwo>Abc</TagTwo>
<TagThree>Xyz</TagThree>
</TagOne>
Run Code Online (Sandbox Code Playgroud)
首选XSLT 1.0解决方案。
基本上,元素名称应以大写字母开头,并且每个下划线后出现的字母应为大写字母。然后删除下划线。请注意,这仅需要应用于元素名称,而不能应用于文本。
这是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:variable name="new-name">
<xsl:call-template name="PascalCase">
<xsl:with-param name="text" select="name()"/>
</xsl:call-template>
</xsl:variable>
<xsl:element name="{$new-name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template name="PascalCase">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'_'"/>
<xsl:param name="upper-case" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
<xsl:param name="lower-case" select="'abcdefghijklmnopqrstuvwxyz'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:value-of select="translate(substring($token, 1, 1), $lower-case, $upper-case)" />
<xsl:value-of select="translate(substring($token, 2), $upper-case, $lower-case)" />
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="PascalCase">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
请注意,这只会转换明确列出的字符。
| 归档时间: |
|
| 查看次数: |
469 次 |
| 最近记录: |