title: XSLT - filter out elements that are not x-referenced
body: <p>I have developed a (semi-)identity transformation from which I need to filter out elements that are unused.</p>
  
  <p>The source XML contains 2001 "zones". No more, no less.</p>
  
  <p>It also contains any number of devices, which are placed in these zones.
  One specific example source XML contains 8800 of these devices.
  More than one device can be placed in the same zone.</p>
  
  <p>Zone 0 is a "null zone", meaning that a device placed in this zone is currently unassigned.
  This means that the number of real zones is 2000.</p>
  
  <p>Simplified source XML:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;configuration&gt;
    &lt;zones&gt;
      &lt;zone id="0"&gt;
      ...
      &lt;zone id="2000"/&gt;
    &lt;/zones&gt;
    &lt;devices&gt;
      &lt;device addr="1"&gt;
        &lt;zone&gt;1&lt;/zone&gt;
      &lt;/device&gt;
      ...
      &lt;device addr="8800"&gt;
        &lt;zone&gt;1&lt;/zone&gt;
      &lt;/device&gt;
    &lt;/devices&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>The problem we have is that out of the 2000 usable zones, most often only roughly 200 of these contain one or more devices.
  I need to whittle out unused zones. There are reasons for this which serve only to detract from the question at hand, so if you don't mind I will not elaborate here.</p>
  
  <p>I currently have this problem solved, like so:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;xsl:for-each select="zones/zone[@id &amp;gt; 0]"&gt;
    &lt;xsl:when test="/configuration/devices/device[zone=current()/@id]"&gt;
      &lt;xsl:call-template name="Zone"/&gt;
    &lt;/xsl:when&gt;
  &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>And this works.
  But on some of the larger projects the transformation takes absolute ages.
  That is because in pseudo code this translates to:</p>
  
  <pre><code>  for each &lt;zone&gt; in &lt;zones&gt;
      find any &lt;device&gt; in &lt;devices&gt; with reference to &lt;zone&gt;
      if found
        apply zone template
      endif
    endfor
  </code></pre>
  
  <p>With 2000 zones to iterate over - and each iteration triggering up to 8800 searches for a qualifying device - you can imagine this taking a very long time.</p>
  
  <p>And to compound problems, <a href="http://xmlsoft.org/XSLT/" rel="nofollow noreferrer">libxslt</a> provides no API for progress reporting. This means that for a long time our application will appear frozen while it imports and converts the customer XML. </p>
  
  <p>I do have the option to write every zone unconditionally, and upon the application bootstrapping from our (output) XML, remove or ignore any zones that have no devices placed in them.
  And it may turn out that this may be the only option I have.</p>
  
  <p>The downside to this is that my output XML then contains a lot of zones that are not referenced.
  That makes it a bit difficult to consolidate what we have in our configuration and what parts of it the application is actually using.</p>
  
  <p>My question to you is:</p>
  
  <p><strong>Have I got other options that ensure that the output XML only contains used zones?</strong></p>
  
  <p>I am not averse to performing a follow-up XSLT conversion.
  I was for instance thinking that it may be possible(?) to write an attribute <code>used="false"</code> to each <code>&lt;Zone&gt;</code> element in my output.
  Then as I go over the devices, I find the relevant zone in my output XML (providing it is assigned / zone is non-zero) and change this to <code>used="true"</code>.
  Then follow up with a quick second transformation to remove all zones which have <code>used="false"</code>.</p>
  
  <p>But, can I reference my own output elements during an XSLT transformation and change its contents?</p>