title: Change parent attribute value based on child value
body: <p>I have to change the attribute of a parent node based on a combination of parent attirbute value and and child value. My input xml is as below:</p>
  
  <pre><code>&lt;filters&gt;
  &lt;sheetFilter filterUsage="table"  labelKey="WR" hidden="false"&gt;
      &lt;userLogin&gt;U0002&lt;/userLogin&gt;
      &lt;containers&gt;
            &lt;sheetTypeRef name="WorkRequest"/&gt;
      &lt;/containers&gt;
  &lt;/sheetFilter&gt;
  &lt;sheetFilter filterUsage="table"  labelKey="AR" hidden="false"&gt;
      &lt;userLogin&gt;U0003&lt;/userLogin&gt;
      &lt;containers&gt;
            &lt;sheetTypeRef name="ARRequest"/&gt;
      &lt;/containers&gt;
  &lt;/sheetFilter&gt;
  &lt;sheetFilter filterUsage="table"  labelKey="WR" hidden="false"&gt;
      &lt;userLogin&gt;U0002&lt;/userLogin&gt;
      &lt;containers&gt;
            &lt;sheetTypeRef name="WorkRequest"/&gt;
      &lt;/containers&gt;
  &lt;/sheetFilter&gt;
  &lt;/filters&gt;
  </code></pre>
  
  <p>In case of <code>@labelKey</code> and <code>userLogin</code> value combination is repeating, I have to replace the <code>@labelKey</code> with a counter appeneded and the desired output should be like:</p>
  
  <pre><code>&lt;filters&gt;    
      &lt;sheetFilter filterUsage="table" labelKey="WR1" hidden="false"&gt;
          &lt;userLogin&gt;U0002&lt;/userLogin&gt;
          &lt;containers&gt;
              &lt;sheetTypeRef name="WorkRequest"/&gt;
          &lt;/containers&gt;
      &lt;/sheetFilter&gt;
      &lt;sheetFilter filterUsage="table" labelKey="AR" hidden="false"&gt;
          &lt;userLogin&gt;U0003&lt;/userLogin&gt;
          &lt;containers&gt;
              &lt;sheetTypeRef name="WorkRequest"/&gt;
          &lt;/containers&gt;
      &lt;/sheetFilter&gt;
      &lt;sheetFilter filterUsage="table" labelKey="WR2" hidden="false"&gt;
          &lt;userLogin&gt;U0002&lt;/userLogin&gt;
          &lt;containers&gt;
              &lt;sheetTypeRef name="WorkRequest"/&gt;
          &lt;/containers&gt;
      &lt;/sheetFilter&gt;
  &lt;/filters&gt;
  </code></pre>
  
  <p>I have written the below xslt to apply the transformation:</p>
  
  <pre><code>&lt;xsl:template match="*|@*"&gt;
      &lt;xsl:copy disable-output-escaping="yes"&gt;
      &lt;xsl:apply-templates select="@*"/&gt;
      &lt;xsl:apply-templates select="node()"/&gt;
      &lt;/xsl:copy&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="sheetFilter[@labelKey='WR']"&gt;
      &lt;xsl:param name="i" select="1" /&gt;
      &lt;xsl:element name="{ local-name() }" disable-output-escaping="yes"&gt;
  
      &lt;xsl:for-each select="userLogin[text()='U0002']" &gt;  
          &lt;xsl:with-param name="i" select="$i +1"/&gt;
          &lt;xsl:if test="not(../sheetFilter[@labelKey])"&gt;
          &lt;xsl:apply-templates select="../@*"/&gt;
          &lt;/xsl:if&gt;
  
          &lt;xsl:attribute name="labelKey"&gt;
          &lt;xsl:value-of select="concat('WR', $i)"/&gt;
          &lt;/xsl:attribute&gt;
      &lt;/xsl:for-each&gt;
      &lt;xsl:apply-templates select="node()"/&gt;
      &lt;/xsl:element&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p>But I am getting is a compiler warning below:</p>
  
  <pre><code>Compiler warnings:
  Attribute 'labelKey' outside of element
  </code></pre>
  
  <p>And the output is not consistent, in some nodes the counter value is applied to 'labelKey' and in some nodes @lableKey itself is missing. Can anybody throw some light what went wrong in the above xslt? Also I would like to know is it possible to generalise the condition, for example  <code>match="sheetFilter[@labelKey='WR']"</code> is it possible to replace the <code>WR</code> and <code>U0002</code> with a generalised statement since I am not sure of the combination values that may repeat.</p>
  
  <p>Thanks in advance.
  Susan</p>