title: how to re-number filtered elements in result set
body: <p><strong>XML:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;book&gt;
      &lt;chapter&gt;
          &lt;section name="a"&gt;...&lt;/section&gt;
          &lt;section name="b"&gt;...&lt;/section&gt;
          &lt;section name="c"&gt;...&lt;/section&gt;
          &lt;section name="d"&gt;...&lt;/section&gt;
          &lt;section name="e"&gt;...&lt;/section&gt;
          ...
      &lt;/chapter&gt;
      &lt;appendix&gt;
          &lt;section name="reference"&gt;...&lt;/section&gt;
      &lt;/appendix&gt;
  &lt;/book&gt;
  </code></pre>
  
  <p>Hi, I want to output all the <strong><code>sections</code></strong> under <code>chapter</code> and <code>appendix</code> nodes. The sections under appendix will be printed out definitely. But <strong>not all sections under chapter are allowed to print out</strong>, they depend on some external condition (if the section name is in allowed list which passed in from java application). </p>
  
  <p>Also <code>sections</code> under <code>chapter</code> should be output with correct <strong>sequence number before section name</strong> which looks like the following:</p>
  
  <p><strong>Desired result</strong></p>
  
  <ol>
  <li>b ... </li>
  <li>d ...</li>
  </ol>
  
  <p>(which means section a, c, e are filtered out)</p>
  
  <p>My question is how I can produce the above desired output for <code>//chapter/section</code>? Any hints or help is highly appreciated</p>
  
  <p><strong>My XSL:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsl:stylesheet version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ex="http://example.com/namespace" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
      &lt;xsl:output method="xml" indent="yes"/&gt;
  
      &lt;xsl:param name="validChapters" /&gt;
  
      &lt;xsl:function name="ex:isValidChapter" as="xs:boolean"&gt;
          &lt;xsl:param name="str-in" as="xs:string"/&gt;
          &lt;xsl:choose&gt;
              &lt;xsl:when test="contains($validChapters, $str-in)"&gt;
                  &lt;xsl:sequence select="true()" /&gt;
              &lt;/xsl:when&gt;
              &lt;xsl:otherwise&gt;
                  &lt;xsl:sequence select="false()" /&gt;
              &lt;/xsl:otherwise&gt;
          &lt;/xsl:choose&gt;
      &lt;/xsl:function&gt;
  
      &lt;xsl:template match="/"&gt;
          &lt;xsl:apply-templates select="chapter" /&gt;
          &lt;xsl:apply-templates select="appendix" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="chapter"&gt;
          ...
          &lt;xsl:apply-templates select="section" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="appendix"&gt;
          ...
          &lt;xsl:apply-templates select="section" /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="section"&gt;
          ...
          &lt;xsl:apply-templates /&gt;
      &lt;/xsl:template&gt;
  
      &lt;xsl:template match="//chapter/section"&gt;
          &lt;xsl:if test="ex:isValidChapter(@name)"&gt;
              &lt;fo:block&gt;
                  &lt;xsl:number format="1."/&gt;
                  &lt;xsl:value-of select="@name" /&gt;
              &lt;/fo:block&gt;
              &lt;xsl:apply-templates /&gt;
          &lt;/xsl:if&gt;
      &lt;/xsl:template&gt;
  
      ...
  &lt;/xsl:stylesheet&gt;
  </code></pre>