title: xsl:variable xsl:copy-of select
body: <p>I have the following XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;XmlTest&gt;
      &lt;Pictures attr="Pic1"&gt;Picture 1&lt;/Pictures&gt;
      &lt;Pictures attr="Pic2"&gt;Picture 2&lt;/Pictures&gt;
      &lt;Pictures attr="Pic3"&gt;Picture 3&lt;/Pictures&gt;
  &lt;/XmlTest&gt;
  </code></pre>
  
  <p>While this XSL does what is expected (output the attr of the first picture):</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:template match="/XmlTest"&gt;
          &lt;xsl:variable name="FirstPicture" select="Pictures[1]"&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:value-of select="$FirstPicture/@attr"/&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>It seems to be not possible to do the same inside the variable declaration using xsl:copy-of:</p>
  
  <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" &gt;
      &lt;xsl:template match="/XmlTest"&gt;
          &lt;xsl:variable name="FirstPicture"&gt;
              &lt;xsl:copy-of select="Pictures[1]"/&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:value-of select="$FirstPicture/@attr"/&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Curious:
  If I just select "$FirstPicture" instead of "$FirstPicture/@attr" in the second example, it outputs the text node of Picture 1 as expected...</p>
  
  <p>Before you all suggest me to rewrite the code:
  This is just a simplified test, my real aim is to use a named template to select a node into the variable FirstPicture and reuse it for further selections.</p>
  
  <p>I hope someone could help me to understand the behavior or could suggest me a proper way to select a node with code which could be easily reused (the decission which node is the first one is complex in my real application). Thanks.</p>
  
  <p><strong>Edit (thanks to Martin Honnen):</strong>
  This is my working solution example (which additionally uses a seperate template to select the requested picture node), using the MS XSLT processor:</p>
  
  <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      version="1.0"&gt;
      &lt;xsl:template match="/XmlTest"&gt;
          &lt;xsl:variable name="FirstPictureResultTreeFragment"&gt;
              &lt;xsl:call-template name="SelectFirstPicture"&gt;
                  &lt;xsl:with-param name="Pictures" select="Pictures" /&gt;
              &lt;/xsl:call-template&gt;
          &lt;/xsl:variable&gt;
          &lt;xsl:variable name="FirstPicture" select="msxsl:node-set($FirstPictureResultTreeFragment)/*"/&gt;
          &lt;xsl:value-of select="$FirstPicture/@attr"/&gt;
          &lt;!-- further operations on the $FirstPicture node --&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template name="SelectFirstPicture"&gt;
          &lt;xsl:param name="Pictures"/&gt;
          &lt;xsl:copy-of select="$Pictures[1]"/&gt;
      &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Not nice, that it is in XSLT 1.0 not possible to output a node directly from a template, but with the extra variable it is at least not impossible.</p>