An And
An And

Reputation: 155

Convert XML element names to camel-case

Following is my xml

<tag>
    <subTag1>
        <tagGroup11>
            <data111>
...

I need to select all the same xml elements but with camel case. So for example my new xml after XSLT transformation needs to look like

<Tag>
    <SubTag1>
        <TagGroup11>
            <Data111>A</Data111>
            <Data112>B</Data112>
            <Data113>C</Data113>
        </TagGroup11>
        <TagGroup12>
            <Data121>D</Data121>
            <Data122>E</Data122>
            <Data123>F</Data123>
        </TagGroup12>
    </SubTag1>
    <SubTag2>
        <TagGroup22>
            <Data221>twotwoone</Data221>
            <Data222>twotwotwo</Data222>
            <Data223>two two three</Data223>
        </TagGroup22>
        <TagGroup23>
            <Data231>twotwoone</Data231>
            <Data232>twotwotwo</Data232>
            <Data233>two two three</Data233>
        </TagGroup23>
    </SubTag2>
</Tag>

Can some one help me with the xslt for this?

Upvotes: 1

Views: 2181

Answers (1)

ABach
ABach

Reputation: 3738

@arkonautom makes a valid point: you have not fully defined your expectations for this transformation. That said, the following will do what you appear to want.

When this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:variable name="vLowercase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  <xsl:variable name="vUppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

  <xsl:template match="*">
    <xsl:element
      name="{concat(translate(substring(local-name(), 1, 1),
                              $vLowercase,
                              $vUppercase),
                    substring(local-name(), 2))}"
    >
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

...is run against the XML you provide (fleshed out with, I assume, the remainder of the structure):

<tag>
  <subTag1>
    <tagGroup11>
      <data111>A</data111>
      <data112>B</data112>
      <data113>C</data113>
    </tagGroup11>
    <tagGroup12>
      <data121>D</data121>
      <data122>E</data122>
      <data123>F</data123>
    </tagGroup12>
  </subTag1>
  <subTag2>
    <tagGroup22>
      <data221>twotwoone</data221>
      <data222>twotwotwo</data222>
      <data223>two two three</data223>
    </tagGroup22>
    <tagGroup23>
      <data231>twotwoone</data231>
      <data232>twotwotwo</data232>
      <data233>two two three</data233>
    </tagGroup23>
  </subTag2>
</tag>

...the wanted result is produced:

<?xml version="1.0" encoding="UTF-8"?>
<Tag>
  <SubTag1>
    <TagGroup11>
      <Data111>A</Data111>
      <Data112>B</Data112>
      <Data113>C</Data113>
    </TagGroup11>
    <TagGroup12>
      <Data121>D</Data121>
      <Data122>E</Data122>
      <Data123>F</Data123>
    </TagGroup12>
  </SubTag1>
  <SubTag2>
    <TagGroup22>
      <Data221>twotwoone</Data221>
      <Data222>twotwotwo</Data222>
      <Data223>two two three</Data223>
    </TagGroup22>
    <TagGroup23>
      <Data231>twotwoone</Data231>
      <Data232>twotwotwo</Data232>
      <Data233>two two three</Data233>
    </TagGroup23>
  </SubTag2>
</Tag>

Upvotes: 2

Related Questions