title: Handling special characters in XSLT to get well-formed output XML
body: <p>I had requirement of transforming below input XML to desired output XML format. With help of this forum, I got the solution as below:</p>
  <p><strong>Input XML</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;dataset  xmlns=&quot;http://developer.cognos.com/schemas/xmldata/1/&quot;  xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;metadata&gt;
      &lt;item name=&quot;Employee Id&quot; /&gt;
      &lt;item name=&quot;Employee Name&quot; /&gt;
      &lt;item name=&quot;Department Name&quot; /&gt;
  &lt;/metadata&gt;
  &lt;data&gt;
      &lt;row&gt;
        &lt;value&gt;1&lt;/value&gt;
        &lt;value Salutation=&quot;Dr.&quot; &gt;John&lt;/value&gt;
        &lt;value&gt;Finance&lt;/value&gt;
      &lt;/row&gt;
      &lt;row&gt;
        &lt;value&gt;2&lt;/value&gt;
        &lt;value Salutation=&quot;Mr.&quot; &gt;Peter&lt;/value&gt;
        &lt;value&gt;Admin&lt;/value&gt;
      &lt;/row&gt;
  &lt;/data&gt;
  &lt;/dataset&gt;
  </code></pre>
  <p><strong>XSLT Transformation</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;xsl:stylesheet version=&quot;1.0&quot; xmlns:c=&quot;http://developer.cognos.com/schemas/xmldata/1/&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
  &lt;xsl:output indent=&quot;yes&quot;/&gt;
  &lt;xsl:strip-space elements=&quot;*&quot;/&gt;
  &lt;xsl:variable name=&quot;vNames&quot; select=&quot;/*/c:metadata/*/@name&quot; /&gt;
  &lt;xsl:template match=&quot;/*/c:data&quot;&gt;
      &lt;dataset&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/dataset&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match=&quot;c:row&quot;&gt;
      &lt;row&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/row&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match=&quot;c:row/*&quot;&gt;
      &lt;xsl:variable name=&quot;vPos&quot; select=&quot;position()&quot;/&gt;
      &lt;xsl:element name=&quot;{translate($vNames[$vPos], ' ', '_')}&quot;&gt;
          &lt;xsl:apply-templates select=&quot;@*&quot;/&gt;
          &lt;xsl:apply-templates/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  &lt;xsl:template match=&quot;@*&quot;&gt;
      &lt;xsl:attribute name=&quot;{name()}&quot;&gt;
          &lt;xsl:value-of select=&quot;.&quot; /&gt;
      &lt;/xsl:attribute&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  <p><strong>Desired Output XML</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-16&quot;?&gt;
  &lt;dataset xmlns:c=&quot;http://developer.cognos.com/schemas/xmldata/1/&quot;&gt;
  &lt;row&gt;
      &lt;Employee_Id&gt;1&lt;/Employee_Id&gt;
      &lt;Employee_Name Salutation=&quot;Dr.&quot;&gt;John&lt;/Employee_Name&gt;
      &lt;Department_Name&gt;Finance&lt;/Department_Name&gt;
  &lt;/row&gt;
  &lt;row&gt;
      &lt;Employee_Id&gt;2&lt;/Employee_Id&gt;
      &lt;Employee_Name Salutation=&quot;Mr.&quot;&gt;Peter&lt;/Employee_Name&gt;
      &lt;Department_Name&gt;Admin&lt;/Department_Name&gt;
  &lt;/row&gt;
  </code></pre>
  
  <p>However, I came across a special scenario which breaks this solution. The attribute values in Input XML can start with Number, Special Character or Space.</p>
  <p><strong>New Input XML</strong></p>
  <pre><code>&lt;?xml version=&quot;1.0&quot;?&gt;
  &lt;dataset  xmlns=&quot;http://developer.cognos.com/schemas/xmldata/1/&quot;  xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
  &lt;metadata&gt;
      &lt;item name=&quot;1Employee Id&quot; /&gt;
      &lt;item name=&quot; Employee Name&quot; /&gt;
      &lt;item name=&quot;$Department Name&quot; /&gt;
  &lt;/metadata&gt;
  &lt;data&gt;
  &lt;row&gt;
      &lt;value&gt;1&lt;/value&gt;
      &lt;value Salutation=&quot;Dr.&quot; &gt;John&lt;/value&gt;
      &lt;value&gt;Finance&lt;/value&gt;
  &lt;/row&gt;
  &lt;row&gt;
      &lt;value&gt;2&lt;/value&gt;
      &lt;value Salutation=&quot;Mr.&quot; &gt;Peter&lt;/value&gt;
      &lt;value&gt;Admin&lt;/value&gt;
  &lt;/row&gt;
  &lt;/data&gt;
  &lt;/dataset&gt;
  </code></pre>
  <p>Since <strong>name</strong> attribute values are translated to Element names, above transformation fails as Element names cannot start with Number or Space.
  In this case I would like to replace those characters with some valid characters for Element name say <code>_</code> or <code>C_</code> to get the same desired Output XML.</p>
  <p>How do I handle this scenario?</p>