Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are two fairly simple ways that come quickly to mind.</p> <p>(1) In your template for <code>test-message</code>, include two <code>test-message</code> output elements, each containing an <code>apply-templates</code> instruction. Give <code>apply-templates</code> a parameter to distinguish the first and second calls.</p> <p>Say, something like (not tested):</p> <pre><code>&lt;xsl:template match="test-message"&gt; &lt;test-message&gt; &lt;xsl:apply-templates&gt; &lt;xsl:with-param name="flag" select="1"/&gt; &lt;/xsl:apply-templates&gt; &lt;/test-message&gt; &lt;test-message&gt; &lt;xsl:apply-templates&gt; &lt;xsl:with-param name="flag" select="2"/&gt; &lt;/xsl:apply-templates&gt; &lt;/test-message&gt; &lt;/xsl:template&gt; </code></pre> <p>In the template for <code>segment</code>, write out a copy of the element if (a) $flag = 1 and neither this segment nor any preceding sibling segment has an id of <code>OBR</code> or <code>ORC</code>, or else if (b) $flag = 2 and this segment or some preceding sibling segment has such an <code>id</code>. Something like</p> <pre><code>&lt;xsl:template match="segment"&gt; &lt;xsl:param name="flag"/&gt; &lt;xsl:if test="( $flag = 1 and not(@id = ('ORC', 'OBR')) and not(preceding-sibling::segment [@id=('ORC','OBR')]) ) or ( $flag = 2 and ((@id = ('ORC', 'OBR')) or preceding-sibling::segment [@id=('ORC','OBR')] )"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:if&gt; </code></pre> <p></p> <p>(2) Make the <code>test-message</code> template as above, but add <code>select="./segment[1]"</code> to the two calls to <code>xsl:apply-templates</code>.</p> <p>Then make the template for <code>segment</code> do its work and then recur on its immediately following sibling. To keep the logic simple, we distinguish several cases: first, <code>$flag=1</code> and we haven't seen OBR or ORC yet: copy the current element and keep going.</p> <pre><code>&lt;xsl:template match="segment"&gt; &lt;xsl:param name="flag"/&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$flag=1 and not(@id=('OBR', 'ORC'))"&gt; &lt;xsl:copy-of select="."/&gt; &lt;xsl:if test="not(@id=('OBR','ORC'))"&gt; &lt;xsl:apply-templates select="following-sibling::*[1]"&gt; &lt;xsl:with-param name="flag" select="$flag"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:if&gt; &lt;/xsl:when&gt; </code></pre> <p>Second, <code>$flag = 1</code> and we are now encountering OBR or ORC. Don't copy the current element and don't keep going; the first <code>test-message</code> element is now completed.</p> <pre><code> &lt;xsl:when test="$flag=1 and @id=('OBR', 'ORC')"&gt; &lt;!--* do nothing, stop recursion *--&gt; &lt;/xsl:when&gt; </code></pre> <p>Third, <code>$flag = 2</code> and we haven't yet encountered OBR or ORC. Keep going.</p> <pre><code> &lt;xsl:when test="$flag=2 and not(@id=('OBR', 'ORC'))"&gt; &lt;!--* don't copy yet, keep looking for OBR/ORC *--&gt; &lt;xsl:apply-templates select="following-sibling::*[1]"&gt; &lt;xsl:with-param name="flag" select="$flag"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; </code></pre> <p>Fourth, <code>$flag = 2</code> and we are now encountering OBR or ORC. Copy the current element and keep going; switch the flag to a third value which means we are in the second <code>test-message</code> element and we have seen OBR or ORC:</p> <pre><code> &lt;xsl:when test="$flag=2 and @id=('OBR', 'ORC')"&gt; &lt;xsl:copy-of select="."/&gt; &lt;xsl:apply-templates select="following-sibling::*[1]"&gt; &lt;xsl:with-param name="flag" select="3"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; </code></pre> <p>Finally, if <code>$flag = 3</code> then we just copy the current element and keep going.</p> <pre><code> &lt;xsl:when test="$flag=3"&gt; &lt;xsl:copy-of select="."/&gt; &lt;xsl:apply-templates select="following-sibling::*[1]"&gt; &lt;xsl:with-param name="flag" select="3"/&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload