title: XSLT Duplicating node & all child nodes & attributes with new values
body: <p>Good day ....
  I am trying to duplicate nodes with updated/new element text and/or attribute values.  </p>
  
  <p>My input XML file:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;products author="Jesper"&gt;
    &lt;product id="p1"&gt;
      &lt;name&gt;Delta&lt;/name&gt;
      &lt;price&gt;800&lt;/price&gt;
      &lt;stock&gt;4&lt;/stock&gt;
      &lt;country&gt;Denmark&lt;/country&gt;
    &lt;/product&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>The desired XML output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;products author="Jesper"&gt;
    &lt;product id="p1"&gt;
      &lt;name&gt;Delta&lt;/name&gt;
      &lt;price&gt;800&lt;/price&gt;
      &lt;stock&gt;4&lt;/stock&gt;
      &lt;country&gt;Denmark&lt;/country&gt;
    &lt;/product&gt;
    &lt;product id="NEW_p1"&gt;
      &lt;name&gt;NEW_Delta&lt;/name&gt;
      &lt;price&gt;NEW_800&lt;/price&gt;
      &lt;stock&gt;NEW_4&lt;/stock&gt;
      &lt;country&gt;NEW_Denmark&lt;/country&gt;
    &lt;/product&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>After some time, the XSLT that I currently have is as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
                  exclude-result-prefixes="msxsl"&gt;
    &lt;xsl:output method="xml" indent="yes"/&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;xsl:template match ="product"&gt;
      &lt;xsl:copy&gt;
        &lt;xsl:apply-templates select="@*|node()"/&gt;
      &lt;/xsl:copy&gt;
      &lt;product&gt;
        &lt;xsl:attribute name ="id"&gt;
          &lt;xsl:value-of select ="concat('NEW_',@id"/&gt;
        &lt;/xsl:attribute&gt;
        &lt;xsl:copy&gt;
          &lt;xsl:apply-templates select="node()"/&gt;
        &lt;/xsl:copy&gt;
      &lt;/product&gt;
    &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>However, using the above transform, I get the following XML output:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;products author="Jesper"&gt;
    &lt;product id="p1"&gt;
      &lt;name&gt;Delta&lt;/name&gt;
      &lt;price&gt;800&lt;/price&gt;
      &lt;stock&gt;4&lt;/stock&gt;
      &lt;country&gt;Denmark&lt;/country&gt;
    &lt;/product&gt;
    &lt;product id="NEW_p1"&gt;&lt;product&gt;
      &lt;name&gt;Delta&lt;/name&gt;
      &lt;price&gt;800&lt;/price&gt;
      &lt;stock&gt;4&lt;/stock&gt;
      &lt;country&gt;Denmark&lt;/country&gt;
    &lt;/product&gt;&lt;/product&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>As you can see, the <em>product</em> element is added whilst I declared a new <em>product</em> element with new @id value.  Since I use  to process child nodes I believe this processes the <em>product</em> element again.  </p>
  
  <p>Also, I need help in updating the child node's values (prepending 'NEW_' to each value).  Searching the vast questions on this site, I believe I need a template like so:</p>
  
  <pre><code>&lt;xsl:template match="*"&gt;
    &lt;xsl:element name ="{local-name()}"&gt;
      &lt;!--for all attributes--&gt;
      &lt;xsl:copy-of select ="@*"/&gt;
      &lt;xsl:value-of select = "."/&gt;
    &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Thank you in advance for any suggestions/ideas with my issue.  </p>
  
  <p><strong>UPDATED</strong>
  Thank you @Mathias for your answer to my initial question.  The answer provided brought one more question involving recursion to deeper levels of an XML structure.  </p>
  
  <p>Input XML file:</p>
  
  <pre><code>    &lt;products author="Jesper"&gt;
        &lt;product id="p1"&gt;
          &lt;name&gt;Delta
            &lt;innerName&gt;MiddleDelta
              &lt;baseName&gt;FinalDelta&lt;/baseName&gt;
            &lt;/innerName&gt;
          &lt;/name&gt;
          &lt;price&gt;800&lt;/price&gt;
          &lt;stock&gt;4&lt;/stock&gt;
          &lt;country&gt;Denmark
            &lt;city&gt;Copenhagen&lt;/city&gt;
          &lt;/country&gt;
        &lt;/product&gt;
      &lt;/products&gt;
  </code></pre>
  
  <p>And the Updated desire output file is as such:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;products author="Jesper"&gt;
    &lt;product id="p1"&gt;
      &lt;name&gt;Delta
        &lt;innerName&gt;MiddleDelta
          &lt;baseName&gt;FinalDelta&lt;/baseName&gt;
        &lt;/innerName&gt;
      &lt;/name&gt;
      &lt;price&gt;800&lt;/price&gt;
      &lt;stock&gt;4&lt;/stock&gt;
      &lt;country&gt;Denmark
        &lt;city&gt;Copenhagen&lt;/city&gt;
      &lt;/country&gt;
    &lt;/product&gt;
    &lt;product id="NEW_p1"&gt;
      &lt;name&gt;NEW_Delta
        &lt;innerName&gt;NEW_MiddleDelta
          &lt;baseName&gt;NEW_FinalDelta&lt;/baseName&gt;
        &lt;/innerName&gt;
      &lt;/name&gt;
      &lt;price&gt;NEW_800&lt;/price&gt;
      &lt;stock&gt;NEW_4&lt;/stock&gt;
      &lt;country&gt;NEW_Denmark
        &lt;city&gt;NEW_Copenhagen&lt;/city&gt;
      &lt;/country&gt;
    &lt;/product&gt;
  &lt;/products&gt;
  </code></pre>
  
  <p>I can only guess that using templates would work as seeing each node has varying level child nodes.  Thank you in advance for ideas/suggestions in this.</p>