2016-07-28 110 views
0

我想使用应该垂直排序的xslt 1.0将一长串列项分解为列。我已经看到了如何使用水平排序来解决这个问题的解决方案,但无法弄清楚如何垂直排列。将列表划分为使用xslt垂直排列的列

下面是示例输入:

<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> 

下面是所需的输出(3列):

<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> 
+0

的例子是不明确的。有14个项目时请显示预期结果。 –

回答

1

我相信这应该为你工作。它将项目分成(任意)给定数量的列,并使用“先向下”方法填充这些列。

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> 

应用于以下输入例如:

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> 

结果将是:

<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> 

呈现为:

enter image description here

+0

谢谢!这正是我需要的。在我能够澄清这个例子之前,你猜对了预期的行为;)我没有想到模特技巧 – user3124206