title: Changing the value of an attribute with xslt
body: <p>There are a number of other questions that deal with this area, but I can't make the solutions there work</p>
  
  <p>I have a transform that splits up a Spring bean file containing multiple beans into a set of files, each with one bean</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"&gt;
  &lt;xsl:template match="/beans/bean" xml:space="preserve"&gt; 
              &lt;xsl:result-document href='{translate(normalize-space(./property[@name="name"])," ", "_")}.xml'
              doctype-public="-//SPRING//DTD BEAN//EN"
              doctype-system="http://www.springframework.org/dtd/spring-beans.dtd"&gt;
  &lt;beans&gt;
      &lt;xsl:copy-of select="."/&gt;
  &lt;/beans&gt;
  &lt;/xsl:result-document&gt;
  &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>This works fine, producing files that start like this:</p>
  
  
  
  <pre><code>&lt;!DOCTYPE beans
    PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;
  &lt;beans&gt;
      &lt;bean class="com.class.mine.Policy" name="bean1"&gt;
          &lt;property name="name"&gt;
  </code></pre>
  
  <p>In order that the file looks identical to one originally created with just one bean in it (since the file will be checked into source code management, I want only real differences), I want to change the <code>name="beanN"</code> attribute to <code>name="bean0"</code> in each file. Note that generally the bean tag can contain additional attributes as well as the <code>class</code> and <code>name</code> ones.</p>
  
  <p>Nothing I've tried based on other answers here works. Mostly, I get errors about "Cannot create an attribute node whose parent is a document node" and "Cannot create an attribute node after creating a child of the containing element".</p>
  
  <p>So I'm missing something obvious. What do I need to add to make this extra change?</p>
  
  <p>Update: the problem seems partly to do with the xml:space="preserve". This is presumably causing text nodes to be created. With that removed, I don't get the errors so often but I still can't get the transform right</p>
  
  <p>(and the output has</p>
  
  <pre><code>&lt;beans&gt;&lt;bean&gt;
  </code></pre>
  
  <p>How do I get a linefeed between these tags? I can use indent="yes" to get the output formatted, but I really want to preserve the input format as it was originally, making changes only where I have to)</p>
  
  <p>Update: One of my many attempts. This one runs, but is basically an identity transform. result-doc is commented out to make debugging easier for now.  I really don't understand properly how xsl:attribute works so a decent explanation of that might help.</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"&gt;
  &lt;xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/&gt;
  &lt;xsl:template match="beans/bean"&gt; 
  &lt;!--             &lt;xsl:result-document href='{translate(normalize-space(./property[@name="name"])," /", "_+")}.xml'
              doctype-public="-//SPRING//DTD BEAN//EN"
              doctype-system="http://www.springframework.org/dtd/spring-beans.dtd"
              omit-xml-declaration="no"&gt; --&gt;
  &lt;beans&gt;
  &lt;xsl:apply-templates select="bean"/&gt;
  &lt;xsl:copy-of select="." /&gt; 
  &lt;/beans&gt;
  &lt;!-- &lt;/xsl:result-document&gt; --&gt;
  &lt;/xsl:template&gt;
   &lt;xsl:template match="bean/@*"&gt;
        &lt;xsl:copy&gt;
            &lt;xsl:attribute name="name"&gt;myvalue&lt;/xsl:attribute&gt;
             &lt;xsl:copy-of select="@*|node()"/&gt;
        &lt;/xsl:copy&gt;
     &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>