title: XML to XML Mapping using XSLT
body: <p>I am new to XSLT and trying to map one XML to another XML using xslt, here is my first XML</p>
  
  <pre><code>&lt;root&gt;
      &lt;record&gt;
          &lt;element name="LoginId"&gt;a&lt;/element&gt;
          &lt;element name="name"&gt;Admin Manager&lt;/element&gt;
          &lt;element name="password"&gt;12345&lt;/element&gt;
          &lt;element name="Age"&gt;28&lt;/element&gt;
          &lt;element name="Sex"&gt;M&lt;/element&gt;
      &lt;/record&gt;
      &lt;record&gt;
          &lt;element name="LoginId"&gt;b&lt;/element&gt;
          &lt;element name="name"&gt;HR exec&lt;/element&gt;
          &lt;element name="password"&gt;pass1&lt;/element&gt;
          &lt;element name="Age"&gt;26&lt;/element&gt;
          &lt;element name="Sex"&gt;F&lt;/element&gt;
      &lt;/record&gt;
      &lt;record&gt;
          &lt;element name="LoginId"&gt;c&lt;/element&gt;
          &lt;element name="name"&gt;PR Manager&lt;/element&gt;
          &lt;element name="password"&gt;pass2&lt;/element&gt;
          &lt;element name="Age"&gt;27&lt;/element&gt;
          &lt;element name="Sex"&gt;M&lt;/element&gt;
      &lt;/record&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>I need to convert this XML to the following</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;final&gt;
      &lt;test&gt;
          &lt;UID&gt;a&lt;/UUID&gt;
          &lt;Name&gt;HR manager&lt;/Name&gt;
          &lt;Groups&gt;admingroup&lt;/Groups&gt;
          &lt;Password&gt;12345&lt;/Password&gt;
      &lt;/test&gt;
      &lt;test&gt;
          &lt;UID&gt;b&lt;/UUID&gt;
          &lt;Name&gt;HR exec&lt;/Name&gt;
          &lt;Groups&gt;admingroup&lt;/Groups&gt;
          &lt;Password&gt;pass1&lt;/Password&gt;
      &lt;/test&gt;
      &lt;test&gt;
          &lt;UID&gt;c&lt;/UUID&gt;
          &lt;Name&gt;PR manager&lt;/Name&gt;
          &lt;Groups&gt;admingroup&lt;/Groups&gt;
          &lt;Password&gt;pass2&lt;/Password&gt;
      &lt;/test&gt;
  &lt;/final&gt;
  </code></pre>
  
  <p>i tried following xslt for transformation</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; 
  - &lt;xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"&gt;
  - &lt;xsl:template match="/"&gt;
  - &lt;test&gt;
  - &lt;xsl:for-each select="root/record"&gt;
    &lt;xsl:apply-templates select="element" /&gt; 
    &lt;/xsl:for-each&gt;
    &lt;/test&gt;
    &lt;/xsl:template&gt;
  - &lt;xsl:template match="element"&gt;
  - &lt;test&gt;
    &lt;Employee /&gt; 
  - &lt;UID&gt;
    &lt;xsl:value-of select="@LoginId" /&gt; 
    &lt;/UID&gt;
  - &lt;xsl:choose&gt;
  - &lt;xsl:when test="@name = ''"&gt;
  - &lt;Name&gt;
    &lt;xsl:text&gt;demo employee&lt;/xsl:text&gt; 
    &lt;/Name&gt;
    &lt;/xsl:when&gt;
  - &lt;xsl:otherwise&gt;
  - &lt;Name&gt;
    &lt;xsl:value-of select="@name" /&gt; 
    &lt;/Name&gt;
    &lt;/xsl:otherwise&gt;
    &lt;/xsl:choose&gt;
  - &lt;Groups&gt;
    &lt;xsl:text&gt;admingroup&lt;/xsl:text&gt; 
    &lt;/Groups&gt;
  - &lt;Password&gt;
    &lt;xsl:value-of select="@password" /&gt; 
    &lt;/Password&gt;
    &lt;/test&gt;
    &lt;/xsl:template&gt;
    &lt;/xsl:transform&gt;
  </code></pre>
  
  <p>but this xslt is generating following XML output</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;impex&gt;
      &lt;final&gt;
          &lt;Employee /&gt;
          &lt;UID /&gt;
          &lt;Name&gt;LoginId&lt;/Name&gt;
          &lt;Groups&gt;admingroup&lt;/Groups&gt;
          &lt;Password /&gt;
      &lt;/final&gt;
  
  total 15 &lt;final&gt;&lt;/final&gt; with similar output
  </code></pre>
  
  <p>i can do it easily in Java but some how have to do in xslt and only problem i am facing is the repetition of <code>&lt;element&gt;</code> tag with different attribute values</p>
  
  <p>any help in this regard will be much helpful for me</p>