Transforming XML as it is being generated on a scanner
I'd go with a &lt;xsl:key ...&gt; index, like so (at top level):

&lt;xsl:key name="contains_RETURN" match="Barcode" use="contains(text(), 'RETURN')"/&gt;


and then rewrite your hotspot to:

&lt;xsl:when test="count(key('contains_RETURN', 'true')) &gt; 0"&gt;
  ...
&lt;/xsl:when&gt;


The idea is to partition Barcode nodes into two sets, the ones that contain 'RETURN' and the ones that don't.  The key function as shown then evaluates to a nodeset that only contains Barcode nodes that contain 'RETURN' and hence count works as expected.