title: Accessing attributes from dynamically created sibling elements
body: <p>Learning XSLT!</p>
  
  <p>I have an XML source tree fragment that looks like this:</p>
  
  <p>Using Saxon and XSLT 2.0 to transform...</p>
  
  <pre><code>   &lt;?xml version = "1.0" encoding = "UTF-8"?&gt;
      &lt;Chapter revision="B" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
          &lt;Content Ref="X123"&gt;
              &lt;Ref name="INT1"/&gt;
              &lt;Ref name="INT2"/&gt;
              &lt;Ref name="INT3"/&gt;
              &lt;Ref name="INT4"/&gt;
              &lt;Ref name="INT5"/&gt;
          &lt;/Content&gt;
          &lt;Data&gt;
              &lt;Reference name="INT1" Function="CONDUCTOR"&gt;&lt;/Reference&gt;
              &lt;Reference name="INT2" Function="SIGNAL"&gt;&lt;/Reference&gt;
              &lt;Reference name="INT3" Function="MIXED"&gt;&lt;/Reference&gt;
              &lt;Reference name="INT4" Function="PLANE"&gt;&lt;/Reference&gt;
              &lt;Reference name="INT5" Function="CORE"&gt;&lt;/Reference&gt;
          &lt;/Data&gt;
      &lt;/Chapter&gt;
  </code></pre>
  
  <p>I want it to produce this:</p>
  
  <pre><code>&lt;Chapter&gt;
     &lt;Content&gt;
        &lt;Ref id="INT1" Function="CONDUCTOR"&gt;INT1&lt;/Ref&gt;
        &lt;Ref id="INT2" Function="SIGNAL"&gt;INT2&lt;/Ref&gt;
        &lt;Ref id="INT3" Function="MIXED"&gt;INT3&lt;/Ref&gt;
        &lt;Ref id="INT4" Function="PLANE"&gt;INT4&lt;/Ref&gt;
        &lt;Ref id="INT5" Function="CORE"&gt;INT5&lt;/Ref&gt;
     &lt;/Content&gt;
  &lt;/Chapter&gt;
  </code></pre>
  
  <p>Here's my template fragment:</p>
  
  <pre><code>&lt;xsl:template match="/"&gt;
      &lt;xsl:apply-templates select="/Chapter"/&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="/Chapter"&gt;
      &lt;xsl:element name="{name()}"&gt;
          &lt;xsl:apply-templates select="Content"/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Content"&gt;
      &lt;xsl:element name="{name()}"&gt;
          &lt;xsl:apply-templates select="Ref"/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="Ref"&gt;
      &lt;xsl:element name="{name()}"&gt;
  
          &lt;xsl:attribute name="id"&gt;
              &lt;xsl:value-of select="@name"/&gt;
          &lt;/xsl:attribute&gt;
  
          &lt;xsl:attribute name="Function"&gt;
              &lt;xsl:value-of select="/Chapter/Data/Reference[@name=/Chapter/Data/Reference/@name]/@Function"/&gt;
          &lt;/xsl:attribute&gt;
  
          &lt;xsl:value-of select="@name"/&gt;
  
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>But the template above produces this: 
  It is obviously picking up the values from every node</p>
  
  <pre><code> &lt;Chapter&gt;
         &lt;Content&gt;
            &lt;Ref id="INT1" Function="CONDUCTOR SIGNAL MIXED PLANE CORE"&gt;INT1&lt;/Ref&gt;
            &lt;Ref id="INT2" Function="CONDUCTOR SIGNAL MIXED PLANE CORE"&gt;INT2&lt;/Ref&gt;
            &lt;Ref id="INT3" Function="CONDUCTOR SIGNAL MIXED PLANE CORE"&gt;INT3&lt;/Ref&gt;
            &lt;Ref id="INT4" Function="CONDUCTOR SIGNAL MIXED PLANE CORE"&gt;INT4&lt;/Ref&gt;
            &lt;Ref id="INT5" Function="CONDUCTOR SIGNAL MIXED PLANE CORE"&gt;INT5&lt;/Ref&gt;
         &lt;/Content&gt;
      &lt;/Chapter&gt;
  </code></pre>
  
  <p>What do I need to supply as a predicate value to step through the attribute values?</p>
  
  <p>Many thanks</p>