title: XSLT: replace the value of an attribute based on the existing attribute value
body: <p>If an anchor <code>&lt;a class="section-ref"&gt;</code> has an <code>@href</code> value that links to an <code>@id</code> of a <code>&lt;div class="sect1"&gt;</code> which is a child of a <code>&lt;div class="chunk"&gt;</code>, I want to change the value of the anchor <code>@href</code> to the <code>@id</code> of the parent chunk. I'm unsure how to find the <code>@id</code> of the parent chunk through XSLT/XPath.</p>
  
  <p>XML:</p>
  
  <pre><code>&lt;book&gt;
  &lt;div&gt;
      &lt;p&gt;In the &lt;a class="section-ref" href="#i2398" id="i2397"&gt;next section&lt;/a&gt;, we turn lorem ipsum.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="extend" id="i100949"&gt;
      &lt;div class="check" id="i100950"&gt;
          &lt;h1 class="title"&gt;Check&lt;/h1&gt;
          &lt;a class="other-ref" href="folder/other-ref.xml" id="i100953"&gt;&lt;/a&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="chunk" id="i100954"&gt;
      &lt;h1 class="title"&gt;8.4&lt;/h1&gt;
      &lt;div class="other-section" id="i100955"&gt;
          &lt;p id="i100956"&gt; Lorem Ipsum&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="sect1" id="i2398"&gt;
          &lt;h1 class="title"&gt;Lorem Ipsum&lt;/h1&gt;
          &lt;p&gt;Blah blah blah&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>XSLT:</p>
  
  <pre><code>&lt;!-- Identity template --&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;!--Change section-ref @href to parent chunk @id if they are @ids of a sect1--&gt;
  
  &lt;xsl:template match="a[@class='section-ref']"&gt;
      &lt;xsl:variable name="section-id" select="substring-after(./@href, '#')"/&gt;
      &lt;xsl:variable name="section" select="$section-id = //div[@class='chunk']/div[@class='sect1']/@id"/&gt;
  
      &lt;xsl:choose&gt;
          &lt;xsl:when test="$section"&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:attribute name="href" select="NEED XPATH HERE"/&gt;
                  &lt;xsl:apply-templates select="(node() | @*) except @href"/&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
              &lt;xsl:copy&gt;
                  &lt;xsl:apply-templates select="node() | @*"/&gt;
              &lt;/xsl:copy&gt;
          &lt;/xsl:otherwise&gt;
      &lt;/xsl:choose&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>&lt;book&gt;
  &lt;div&gt;
      &lt;p&gt;In the &lt;a class="section-ref" href="i100954" id="i2397"&gt;next section&lt;/a&gt;, we turn lorem ipsum.&lt;/p&gt;
  &lt;/div&gt;
  &lt;div class="extend" id="i100949"&gt;
      &lt;div class="check" id="i100950"&gt;
          &lt;h1 class="title"&gt;Check&lt;/h1&gt;
          &lt;a class="other-ref" href="folder/other-ref.xml" id="i100953"/&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="chunk" id="i100954"&gt;
      &lt;h1 class="title"&gt;8.4&lt;/h1&gt;
      &lt;div class="other-section" id="i100955"&gt;
          &lt;p id="i100956"&gt; Lorem Ipsum&lt;/p&gt;
      &lt;/div&gt;
      &lt;div class="sect1" id="i2398"&gt;
          &lt;h1 class="title"&gt;Lorem Ipsum&lt;/h1&gt;
          &lt;p&gt;Blah blah blah&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>Note that the section-ref anchor @href value changes to the @id of the parent chunk (i100954):</p>
  
  <pre><code>&lt;p&gt;In the &lt;a class="section-ref" href="i100954" id="i2397"&gt;next section&lt;/a&gt;, we turn lorem ipsum.&lt;/p&gt;
  </code></pre>