title: Struggling with xslt grouping and querying against xpath count
body: <p>So I'm stuck with XSLT 1.0 </p>
  
  <p>In my XML I have something like this:</p>
  
  <pre><code>    &lt;BenefitDefinitions&gt;
        &lt;BenefitDefinition&gt;
           &lt;BenefitCode&gt;A&lt;/BenefitCode&gt;
           &lt;FulfillmentName&gt;Test&lt;/FulfillmentName&gt;
        &lt;/BenefitDefinition&gt;
        &lt;BenefitDefinition&gt;
           &lt;BenefitCode&gt;B&lt;/BenefitCode&gt;
           &lt;FulfillmentName&gt;Test&lt;/FulfillmentName&gt;
        &lt;/BenefitDefinition&gt;
        &lt;BenefitDefinition&gt;
           &lt;BenefitCode&gt;C&lt;/BenefitCode&gt;
           &lt;FulfillmentName&gt;Test&lt;/FulfillmentName&gt;
        &lt;/BenefitDefinition&gt;
      &lt;/BenefitDefinitions&gt;
  ...
      &lt;Coverage&gt;
        &lt;Cover&gt;
          &lt;BenefitCode&gt;A&lt;/BenefitCode&gt;
        &lt;Cover&gt;
      &lt;/Coverage&gt;
  </code></pre>
  
  <p>The logic needs to be where there are multiple benefit Definitions that have that same fulfillmentName - I need to check if any of them exist as a cover.  With the linkage between the two being the BenefitCode.</p>
  
  <p>I tried using a key to get the benefitDefinitions into groups based on the fulfillment name</p>
  
  <pre><code>  &lt;xsl:key name="benefits-by-fulfillmentname" match="BenefitDefinition" use="FulfillmentName/text()" /&gt;
  </code></pre>
  
  <p>and I can iterate through the groups where there is more than 1</p>
  
  <pre><code>  &lt;xsl:for-each select="//BenefitDefinition[count(. | key('benefits-by-fulfillmentname', FulfillmentName/text())[1]) = 1]"&gt;
  </code></pre>
  
  <p>I could also iterate through the benefitDefinitions of that group by:</p>
  
  <pre><code>&lt;xsl:for-each select="key('benefits-by-fulfillmentname', FulfillmentName/text())"&gt;
  </code></pre>
  
  <p>But what I can't figure out is how I can make use of the key to get a count.</p>
  
  <p>I want something like this:</p>
  
  <pre><code>&lt;xsl:value-of select="count(../../../../Contract/Risks/PersonContractRisk[RiskId = $riskId]/Coverage/Cover[BenefitCode=key('benefits-by-fulfillmentname', BenefitCode)])"/&gt;
  </code></pre>
  
  <p>But that's not working - I'm not skilled with xslt so not too surprising.</p>
  
  <p>All my searching just tells me how to count the benefitDefinitions that belong in that group.. but I need to go more advanced and find out how many covers exist for that benefitDefinition group.</p>
  
  <p>Pretty much where I want to go to (in case this helps) is for each benefitDefinition group if there is a cover output 1 thing, otherwise output something else.</p>