Mega Code Archive

 
Categories / XML Tutorial / XSLT StyleSheet
 

While XSLT does not define while and for loops, their behavior can be simulated

File: Data.xml <?xml version="1.0" encoding="utf-8"?> <data>     <AAA repeat="3"/>     <BBB repeat="2"/>     <CCC repeat="5"/> </data> File: Transform.xslt <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet       version="1.0"       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/data/*">       <paragraph>         <xsl:call-template name="for">           <xsl:with-param name="stop">             <xsl:value-of select="@repeat"/>           </xsl:with-param>         </xsl:call-template>       </p>     </xsl:template>     <xsl:template name="for">       <xsl:param name="start">1</xsl:param>       <xsl:param name="stop">1</xsl:param>       <xsl:param name="step">1</xsl:param>       <xsl:value-of select="name()"/>       <xsl:text/>       <xsl:if test="$start &lt; $stop">         <xsl:call-template name="for">           <xsl:with-param name="stop">             <xsl:value-of select="$stop"/>           </xsl:with-param>           <xsl:with-param name="start">             <xsl:value-of select="$start + $step"/>           </xsl:with-param>         </xsl:call-template>       </xsl:if>     </xsl:template> </xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>     <paragraph>AAAAAAAAA</p>     <paragraph>BBBBBB</p>     <paragraph>CCCCCCCCCCCCCCC</p>