Logo Questions Linux Laravel Mysql Ubuntu Git Menu

XSLT 1.0 and string counting

So I'm trying to solve a problem in xslt which I would normally know how to do in an imperative language. I'm adding cells to a table from a list of xml elements, standard stuff. So:


However, I want to cut off one row and start a new one after a certain character maximum has been reached. So say I allow at the most, 20 characters per row. I'd end up with this:


In an imperative language, I'd append the elements to a row while adding each elements string-count to some mutable variable. When that variable exceeded 20, I'd stop, build a new row, and rerun the whole process (starting at the stopped element) on that row after returning the string-count to zero. However, I can't change variable values in XSLT. This whole stateless, function evaluation thing is throwing me for a loop.

like image 232
Colin Brogan Avatar asked Jan 17 '23 06:01

Colin Brogan

1 Answers

Coming to this forum from xsl-list is like going back 10 years, why does everyone use xslt 1:-)

<xsl:stylesheet version="1.0"

<xsl:output indent="yes"/>

<xsl:template match="some-elements">
  <xsl:apply-templates select="element[1]"/>

<xsl:template match="element">
 <xsl:param name="row"/>
  <xsl:when test="(string-length($row)+string-length(.))>20
    <xsl:copy-of select="$row"/>
    <xsl:copy-of select="."/>
   <xsl:apply-templates select="following-sibling::element[1]"/>
   <xsl:apply-templates select="following-sibling::element[1]">
    <xsl:with-param name="row">
     <xsl:copy-of select="$row"/>
     <xsl:copy-of select="."/>
like image 157
David Carlisle Avatar answered Jan 24 '23 16:01

David Carlisle