Divide a list into columns ordered vertically using xslt -


i break-down long list of items columns using xslt 1.0 should ordered vertically. have seen solutions on how horizontal ordering cannot figure out how vertically.

here sample input:

<list>  <item>1</item>  <item>2</item>  <item>3</item>  <item>4</item>  <item>5</item>  <item>6</item>  <item>7</item>  <item>8</item>  <item>9</item>  <item>10</item>  <item>11</item>  <item>12</item>  <item>13</item>  <item>14</item> </list> 

here desired output (3 columns):

<table> <tr>  <td>1</td>  <td>6</td>  <td>11</td> </tr> <tr>  <td>2</td>  <td>7</td>  <td>12</td> </tr> <tr>  <td>3</td>  <td>8</td>  <td>13</td> </tr> <tr>  <td>4</td>  <td>9</td>  <td>14</td> </tr> <tr>  <td>5</td>  <td>10</td>  <td></td> </tr> </table> 

i believe should work you. divides items (any) given number of columns , populates these columns using "down first" method.

xslt 1.0

<xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>  <xsl:param name="columns" select="3" />  <xsl:template match="/list">     <xsl:variable name="rows" select="ceiling(count(item) div $columns)" />     <table border="1">         <xsl:for-each select="item[position() &lt;= $rows]">             <xsl:variable name="row" select="position() mod $rows" />             <tr>                 <xsl:apply-templates select="../item[position() mod $rows = $row]"/>             </tr>         </xsl:for-each>     </table> </xsl:template>  <xsl:template match="item">     <td>         <xsl:value-of select="."/>     </td> </xsl:template>  </xsl:stylesheet> 

applied following input example:

xml

<list>     <item>1</item>     <item>2</item>     <item>3</item>     <item>4</item>     <item>5</item>     <item>6</item>     <item>7</item>     <item>8</item>     <item>9</item>     <item>10</item>     <item>11</item>     <item>12</item>     <item>13</item>     <item>14</item> </list> 

the result be:

<table border="1">    <tr>       <td>1</td>       <td>6</td>       <td>11</td>    </tr>    <tr>       <td>2</td>       <td>7</td>       <td>12</td>    </tr>    <tr>       <td>3</td>       <td>8</td>       <td>13</td>    </tr>    <tr>       <td>4</td>       <td>9</td>       <td>14</td>    </tr>    <tr>       <td>5</td>       <td>10</td>    </tr> </table> 

rendered as:

enter image description here


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -