xslt-3.0xpath-3.0

Split elements into sections


I'd like to split a text into sections:

The source XML:

<data>
  <p>hello</p>
  <p>nice</p>
  <p>BREAK</p>
  <p>world</p>
</data>

My half successful attempt:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
  <xsl:output indent="yes"/>

  <xsl:template match="data">
    <xsl:for-each-group select="p" group-starting-with=".[. = 'BREAK']">
      <chapter>
        <xsl:copy-of select="current-group()"/>
      </chapter>
    </xsl:for-each-group>
  </xsl:template>
  
</xsl:stylesheet>

The result (fragment) I'd like to get:

<chapter>
  <p>hello</p>
  <p>nice</p>
</chapter>
<chapter>
  <p>world</p>
</chapter>

Instead I get

<chapter>
   <p>hello</p>
   <p>nice</p>
</chapter>
<chapter>
   <p>BREAK</p>
   <p>world</p>
</chapter>

(it still contains <p>BREAK</p>)

My question is: is there a straightforward way to get rid of the <p>BREAK</p> element?


Solution

  • You can easily select <xsl:copy-of select="current-group()[not(. = 'BREAK')]"/>.