您将如何使用 XSL 将所有元素和所有属性从 PascalCase 转换为 camelCase?

How would you use XSL to transform all elements and all attributes from PascalCase to camelCase?

我想将我的 XML 文件中的 ALL/ANY 元素和属性(不仅仅是我下面的小示例中的显式 elements/attributes)从 PascalCase 转换为 camelCase。

有人有可以执行此操作的 XSL 转换吗?

这个:

<?xml version="1.0" encoding="utf-8" ?>
<Config Version="2" Name="Test">
    <Process Name="Main">
        X
    </Process>
</Config>

应该变成这样:

<?xml version="1.0" encoding="utf-8" ?>
<config version="2" name="Test">
    <process name="Main">
        X
    </process>
</config>

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
  <xsl:output method="xml" indent="yes"/>

  <!-- everything not mentioned below (e.g. text, comments, processing instructions) -->
  <xsl:template match="node()">
    <xsl:value-of select="."/>
  </xsl:template>

  <!-- attributes -->
  <xsl:template match="@*">
    <xsl:attribute name="{concat(translate(substring(local-name(), 1, 1),'QWERTYUIOPASDFGHJKLZXCVBNM','qwertyuiopasdfghjklzxcvbnm'), substring(local-name(), 2))}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <!-- elements -->
  <xsl:template match="*">
    <xsl:element name="{concat(translate(substring(local-name(), 1, 1),'QWERTYUIOPASDFGHJKLZXCVBNM','qwertyuiopasdfghjklzxcvbnm'), substring(local-name(), 2))}">
      <xsl:apply-templates select="@* | node()"/>
    </xsl:element>
  </xsl:template>


</xsl:stylesheet>

注意:如果使用 XSLT 2,您可以将 translate 函数替换为 lower-case 函数。