title: Transforming nested XML elements to equal parent element; remove parent
body: <p>I have a piece of XML that looks as follows:</p>
  
  <pre><code>&lt;LISTOFHARDWARE&gt;
      &lt;HARDWARE&gt;
          &lt;item&gt;
              &lt;SERIALNUMBER&gt;D4242050101&lt;/SERIALNUMBER&gt;
              &lt;STATUS&gt;2 &lt;/STATUS&gt;
              &lt;STATUSREASON&gt;998 &lt;/STATUSREASON&gt;
              &lt;MACADDRESS /&gt;
              &lt;LINKEDSMARTCARD&gt;4135237101&lt;/LINKEDSMARTCARD&gt;
              &lt;PRODUCTDETAIL&gt;
                  &lt;TYPE&gt;DEVICE&lt;/TYPE&gt;
                  &lt;MODEL&gt;ABC123&lt;/MODEL&gt;
              &lt;/PRODUCTDETAIL&gt;
          &lt;/item&gt;
          &lt;item&gt;
              &lt;SERIALNUMBER&gt;D4242050101&lt;/SERIALNUMBER&gt;
              &lt;STATUS&gt;2 &lt;/STATUS&gt;
              &lt;STATUSREASON&gt;998 &lt;/STATUSREASON&gt;
              &lt;MACADDRESS /&gt;
              &lt;LINKEDSMARTCARD&gt;4135237101&lt;/LINKEDSMARTCARD&gt;
              &lt;PRODUCTDETAIL&gt;
                  &lt;TYPE&gt;DEVICE&lt;/TYPE&gt;
                  &lt;MODEL&gt;ABC123&lt;/MODEL&gt;
              &lt;/PRODUCTDETAIL&gt;
          &lt;/item&gt;
      &lt;/HARDWARE&gt;
  &lt;/LISTOFHARDWARE&gt;   
  </code></pre>
  
  <p>I would like the <code>&lt;item&gt;</code> elements to be replaced by <code>&lt;HARDWARE&gt;</code> elements (the immediate parent element), and the parent <code>&lt;HARDWARE&gt;</code> element to be removed, so that the result looks as follows:</p>
  
  <pre><code>&lt;LISTOFHARDWARE&gt;
      &lt;HARDWARE&gt;
          &lt;SERIALNUMBER&gt;D4242050101&lt;/SERIALNUMBER&gt;
          &lt;STATUS&gt;2 &lt;/STATUS&gt;
          &lt;STATUSREASON&gt;998 &lt;/STATUSREASON&gt;
          &lt;MACADDRESS /&gt;
          &lt;LINKEDSMARTCARD&gt;4135237101&lt;/LINKEDSMARTCARD&gt;
          &lt;PRODUCTDETAIL&gt;
              &lt;TYPE&gt;DEVICE&lt;/TYPE&gt;
              &lt;MODEL&gt;ABC123&lt;/MODEL&gt;
          &lt;/PRODUCTDETAIL&gt;
      &lt;/HARDWARE&gt;
      &lt;HARDWARE&gt;
          &lt;SERIALNUMBER&gt;D4242050101&lt;/SERIALNUMBER&gt;
          &lt;STATUS&gt;2 &lt;/STATUS&gt;
          &lt;STATUSREASON&gt;998 &lt;/STATUSREASON&gt;
          &lt;MACADDRESS /&gt;
          &lt;LINKEDSMARTCARD&gt;4135237101&lt;/LINKEDSMARTCARD&gt;
          &lt;PRODUCTDETAIL&gt;
              &lt;TYPE&gt;DEVICE&lt;/TYPE&gt;
              &lt;MODEL&gt;ABC123&lt;/MODEL&gt;
          &lt;/PRODUCTDETAIL&gt;
  
      &lt;/HARDWARE&gt;
  &lt;/LISTOFHARDWARE&gt;   
  </code></pre>
  
  <p>Is there a way I can do this in an XSL transformation that will work for any XML file that contains <code>&lt;item&gt;</code> elements?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>After the help on this answer and a little further research, I am almost there, but not quite. Consider the following:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:template match="@*|node()"&gt;
          &lt;xsl:copy&gt;
              &lt;xsl:apply-templates select="@*|node()" /&gt;
          &lt;/xsl:copy&gt;
      &lt;/xsl:template&gt;
  
      &lt;!--  Replace &lt;item&gt; with name of parent element --&gt;
      &lt;xsl:template match="item"&gt;
          &lt;xsl:element name="{name(..)}"&gt;
              &lt;xsl:apply-templates select="@*|node()" /&gt;
          &lt;/xsl:element&gt;
      &lt;/xsl:template&gt;
  
      &lt;!-- Remove immediate parent of &lt;item&gt; elements --&gt;
      &lt;xsl:template match="item/.."&gt; &lt;!-- Results in syntax error --&gt; 
              &lt;xsl:apply-templates select="@*|node()" /&gt;
      &lt;/xsl:template&gt;
  
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>The problem is that after successful of changing the <code>&lt;item&gt;</code> elements to the name of the parent, I cannot remove the parent of any <code>item</code> element by referencing its parent dynamically.</p>