Home > front end >  xsl:for-each-group the output of "fn:unparsed-text-lines()" (using the "group-startin
xsl:for-each-group the output of "fn:unparsed-text-lines()" (using the "group-startin

Time:10-24

I have a text file containing lines like this:

ABC
DEF
 GHI
JKL
 MNO
 PQR
 STU
VWX
YZA

So I read the lines of this file with XSLT-3.0's fn:unparsed-text-lines('fileName','UTF-8') and I want to group/merge the lines with the following lines that start with a space and wrap them in elements. For example, using xsl:for-each-group:

<line>ABC</line>
<line>DEF GHI</line>
<line>JKL MNO PQR STU</line>
<line>VWX</line>
<line>YZA</line>

But I couldn't figure out how to use xsl:for-each-group with a sequence of xs:strings with XSLT-3.0. Do I have to convert the strings to nodes first? Or is there a better way?

Here is the dysfunctional XSLT attempt (the predicate is merely a hint and does not work):

<xsl:for-each-group select="unparsed-text-lines('filename.txt','utf-8')" group-starting-with="text()[not(starts-with(.,' '))]">
  <line><xsl:value-of select="string-join(current-group(),'DELIMITER')"/></line>
</xsl:for-each-group>

CodePudding user response:

Strings are not text nodes. Try it this way:

<xsl:for-each-group select="$lines" group-starting-with=".[not(starts-with(.,' '))]">
    <line>
        <xsl:value-of select="current-group()" separator=""/>
    </line>
</xsl:for-each-group>
  • Related