title: Avoid doing double calculations in XSL
body: <p>I'm working on a XSLT transformation. I'm fairly new to XSLT, and I'm having some trouble with it to put it mildly. I'm working on some input, and based on some calculations (checking intersections on polygons, which takes extensive amounts of time, since the data amount is huge), I want to add some elements to a collection.</p>
  
  <p>At the end of the output I want to have an indicator stating whether or not that list contains elements - really simple, but still having trouble. I found out that it's not possible to access the preliminary generated data inside the transformation, since apparently only the input is available.</p>
  
  <p>this is my input. Note that this input can be repeated several times, so the transformation iterates over all the "IterationResponses". </p>
  
  <pre><code>    &lt;IterationResponse Iteration="0"&gt;
      &lt;QueryResult&gt;
          &lt;Collection&gt;
              &lt;Element&gt;
                  &lt;Name&gt;City1&lt;/Name&gt;
                  &lt;number&gt;151&lt;/number&gt;
                  &lt;Link&gt;Link1&lt;/Link&gt;
                  &lt;GMLData&gt;[massive data amount]&lt;/GMLData&gt;
              &lt;/Element&gt;
              &lt;Element&gt;
                  &lt;Name&gt;City1&lt;/Name&gt;
                  &lt;number&gt;151&lt;/number&gt;
                  &lt;Link&gt;Link2&lt;/Link&gt;
                  &lt;GMLData&gt;[massive data amount]&lt;/GMLData&gt;
              &lt;/Element&gt;
              &lt;Element&gt;
                  &lt;Name&gt;City2&lt;/Name&gt;
                  &lt;number&gt;190&lt;/number&gt;
                  &lt;Link&gt;Link3&lt;/Link&gt;
                  &lt;GMLData&gt;[massive data amount]&lt;/GMLData&gt;
              &lt;/Element&gt;
              &lt;Element&gt;
                  &lt;Name&gt;City3&lt;/Name&gt;
                  &lt;number&gt;163&lt;/number&gt;
                  &lt;Link&gt;Link4&lt;/Link&gt;
                  &lt;GMLData&gt;[massive data amount]&lt;/GMLData&gt;
              &lt;/Element&gt;
          &lt;/Collection&gt;
      &lt;/QueryResult&gt;
      &lt;Selection&gt;
          &lt;cadastral&gt;
              &lt;cadastralPolygon&gt;
                  &lt;Polygon&gt;
                      &lt;exterior&gt;
                          &lt;LinearRing&gt;
                              &lt;posList&gt;[small amount of data]&lt;/posList&gt;
                          &lt;/LinearRing&gt;
                      &lt;/exterior&gt;
                  &lt;/Polygon&gt;
              &lt;/cadastralPolygon&gt;
          &lt;/cadastral&gt;
      &lt;/Selection&gt;
  &lt;/IterationResponse&gt;
  </code></pre>
  
  <p>And this is the transformation I currently have (simplified <strong>alot</strong> since the actual input, transformation and output is huge, but the issue remains)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                                xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                                xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
                                xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"&gt;
      &lt;xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/&gt;
      &lt;xsl:template match="/"&gt;
          &lt;xsl:variable name="var_bufferWidth" select="'-0.5'" /&gt;
  
          &lt;QueryResult&gt;
              &lt;xsl:choose&gt;
                  &lt;xsl:when test="count(//Error) &gt;= 1"&gt;
                  &lt;!-- Error handling --&gt;
                  &lt;/xsl:when&gt;
                  &lt;xsl:otherwise&gt;      
                      &lt;Overview&gt;
                          &lt;OverviewCollection&gt;
                              &lt;!--Iterate all iteration responses--&gt; 
                              &lt;xsl:for-each select="//IterationResponse"&gt;
                                  &lt;xsl:variable name="cadastralGML" select="Selection/cadastral/cadastralPolygon"/&gt;
                                  &lt;xsl:for-each select=".//Element"&gt;
                                      &lt;xsl:if test="func:Conflicts($cadastralGML, GMLData, $var_bufferWidth)"&gt;
                                          &lt;OverviewElement&gt;
                                              &lt;Number&gt;
                                                  &lt;xsl:value-of select="concat('0',Kommunenummer)"/&gt;
                                              &lt;/Number&gt;
                                              &lt;Name&gt;
                                                  &lt;xsl:value-of select="Name"/&gt;
                                              &lt;/Name&gt;
                                              &lt;Reference&gt;
                                                  &lt;xsl:value-of select="Link"/&gt;
                                              &lt;/Reference&gt;
                                          &lt;/OverviewElement&gt;
                                      &lt;/xsl:if&gt;
                                  &lt;/xsl:for-each&gt;
                              &lt;/xsl:for-each&gt;
                          &lt;OverviewCollection&gt;
                          &lt;OverviewIndicator&gt;
                            &lt;xsl:value-of select="count(/*//OverviewElement) &gt; 0"/&gt;
                          &lt;OverviewIndicator&gt;
                      &lt;/Overview&gt;
                  &lt;/xsl:otherwise&gt;
              &lt;/xsl:choose&gt;
          &lt;/QueryResult&gt;
  &lt;/xsl:template&gt;
  </code></pre>
  
  <p></p>
  
  <p>This is my current output.</p>
  
  <pre><code>&lt;Overview&gt;
    &lt;OverviewCollection&gt;
      &lt;OverviewElement&gt;
        &lt;Number&gt;0151&lt;/Number&gt;
        &lt;Name&gt;City1&lt;/Name&gt;
        &lt;Reference&gt;Link1&lt;/Reference&gt;
      &lt;/OverviewElement&gt;
    &lt;/OverviewCollection&gt;
    &lt;OverviewIndicator&gt;false&lt;/OverviewIndicator&gt;
  &lt;/Overview&gt;
  </code></pre>
  
  <p>Instead of false i want it to be true, since the final collection is not empty. Any help is greatly appreciated!</p>
  
  <p>Thanks in advance.</p>