title: I need an XSL transform to insert a child element of the selected node using data from an ancestor
body: <p>My XML looks like this:</p>
  
  <pre><code>&lt;SCL&gt;
    &lt;IED name="D60_220SW1"&gt;
      &lt;AccessPoint name="S1"&gt;
        &lt;Server&gt;
          &lt;LDevice inst="Master"&gt;
            &lt;LN0 lnClass="LLN0" inst="" lnType="LLN0_0"&gt;
              &lt;ReportControl name="URCB01" rptID="XYZ" &gt;
                &lt;TrgOps dchg="true" /&gt;
                &lt;OptFields configRef="true" /&gt;
               &lt;RptEnabled max="1" /&gt;
              &lt;/ReportControl&gt;
              &lt;ReportControl name="URCB02" rptID="PAC" datSet="PAC" &gt;
                &lt;TrgOps dchg="true" qchg="true" period="true" /&gt;
                &lt;OptFields configRef="true" /&gt;
               &lt;RptEnabled max="1" /&gt;
              &lt;/ReportControl&gt;
            &lt;/LN0&gt;
          &lt;/LDevice&gt;
        &lt;/Server&gt;
      &lt;/AccessPoint&gt;
    &lt;/IED&gt;
  &lt;/SCL&gt;
  </code></pre>
  
  <p>I need a transform to select all <code>ReportControl</code> nodes where <code>@rptID="PAC"</code> and insert a child node of the <code>RptEnabled</code> node with attribute values dependent on the value of the name attribute of the <code>IED</code> ancestor node.</p>
  
  <p>The result should look like this:</p>
  
  <pre><code>&lt;SCL&gt;
    &lt;IED name="D60_220SW1"&gt;
      &lt;AccessPoint name="S1"&gt;
        &lt;Server&gt;
          &lt;LDevice inst="Master"&gt;
            &lt;LN0 lnClass="LLN0" inst="" lnType="LLN0_0"&gt;
              &lt;ReportControl name="URCB01" rptID="XYZ" &gt;
                &lt;TrgOps dchg="true" /&gt;
                &lt;OptFields configRef="true" /&gt;
               &lt;RptEnabled max="1" /&gt;
              &lt;/ReportControl&gt;
              &lt;ReportControl name="URCB02" rptID="PAC" datSet="PAC" &gt;
                &lt;TrgOps dchg="true" qchg="true" period="true" /&gt;
                &lt;OptFields configRef="true" /&gt;
                &lt;RptEnabled max="1" &gt;
                  &lt;ClientLN iedName="APACC_1" apRef="S2" ldInst="LD0" lnClass="ITCI" lnInst="1" /&gt;
                &lt;/RptEnabled&gt;
              &lt;/ReportControl&gt;
            &lt;/LN0&gt;
          &lt;/LDevice&gt;
        &lt;/Server&gt;
      &lt;/AccessPoint&gt;
    &lt;/IED&gt;
  &lt;/SCL&gt;
  </code></pre>
  
  <p>The values of the <code>iedName</code> and <code>apRef</code> attributes on the inserted <code>ClientLN</code> node are determined by the value of the name attribute on the <code>IED</code> ancestor node. Like this (I'm using C# pseudo code to illustrate):</p>
  
  <pre><code>iedName="APACC_" + name.Contains("_220") ? "1": "2"
  </code></pre>
  
  <p>I'm sure this is doable, but I can't figure out how, and I wasn't able to find an applicable example.</p>