XSLT 1.0 and row counting

So, I am trying to solve a problem in xslt, which I usually know how to do in an imperative language. I add cells to the table from the list of xml elements, standard files. So:

<some-elements> <element>"the"</element> <element>"minds"</element> <element>"of"</element> <element>"Douglas"</element> <element>"Hofstadter"</element> <element>"and"</element> <element>"Luciano"</element> <element>"Berio"</element> </some-elements> 

However, I want to cut one line and start a new one after reaching a certain character. Say I allow a maximum of 20 characters per line. I would end up with this:

 <table> <tr> <td>"the"</td> <td>"minds"</td> <td>"of"</td> <td>"Douglas"</td> </tr> <tr> <td>"Hofstadter"</td> <td>"and"</td> <td>"Luciano"</td> </tr> <tr> <td>"Berio"</td> </tr> </table> 

In an imperative language, I add elements to a string by adding each string-count element to some mutable variable. When this variable exceeded 20, I would stop, build a new line and restart the whole process (starting with the stopped element) in this line after returning the number of lines to zero. However, I cannot change the values โ€‹โ€‹of variables in XSLT. All this inactivity function, function evaluation function throws me into a cycle.

+4
source share
1 answer

Arriving at this forum from xsl-list is like returning 10 years, why everyone uses xslt 1 :-)

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes"/> <xsl:template match="some-elements"> <table> <xsl:apply-templates select="element[1]"/> </table> </xsl:template> <xsl:template match="element"> <xsl:param name="row"/> <xsl:choose> <xsl:when test="(string-length($row)+string-length(.))>20 or not(following-sibling::element[1])"> <tr> <xsl:copy-of select="$row"/> <xsl:copy-of select="."/> </tr> <xsl:apply-templates select="following-sibling::element[1]"/> </xsl:when> <xsl:otherwise> <xsl:apply-templates select="following-sibling::element[1]"> <xsl:with-param name="row"> <xsl:copy-of select="$row"/> <xsl:copy-of select="."/> </xsl:with-param> </xsl:apply-templates> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> 
+9
source

Source: https://habr.com/ru/post/1400794/


All Articles