按列生成表

时间:2014-11-30 12:49:03

标签: xslt xslt-1.0

我会创建一个包含10列的表格,显示以5组50个值递增的值,每个值两个。

这是我的XML:

<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4555</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.5</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT> 
<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4556</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.6</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT> 
<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4557</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.7</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT> 
<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4558</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.8</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT> 
<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4559</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.9</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT> 
<NTC_LIGHTLISTPRODUCT>
    <ITA_LIGHT_NUMBER>4560</ITA_LIGHT_NUMBER>
    <INTERNATIONAL_LIGHT_NUMBER>E2686.10</INTERNATIONAL_LIGHT_NUMBER>
</NTC_LIGHTLISTPRODUCT>

使用colomn创建一个特定的表格。 这是输出:


国际|意大利|国际|意大利|国际|意大利|


E2686.5 | 4555 | E2686.7 | 4557 | E2686.9 | 4559 |
E2686.6 | 4556 | E2686.8 | 4558 | E2686.10 | 4560 |

直到页面结束,当页面完成时,从下一列开始。

使用这个XSLT我创建了它,但是我无法到达页面末尾并继续到下一个表列,我只在一列中的另一个页面中继续。

这是我的XSLT:

<body style="tab-interval:35.4pt">
    <div>
        <table  style="font-size:9pt; border-style:none; border-collapse:collapse; vertical-align:top; font-family:Univers Condensed;" border="1" cellspacing="0" cellpadding="0">
        <tr>
            <td> <p>Internazionale</p></td>
            <td> <p>Italiano</p></td>
        </tr>
        <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER">
        <xsl:sort select="//INTERNATIONAL_LIGHT_NUMBER" data-type="number" order="ascending"/>
            <tr>
                <td>
                    <xsl:value-of select="."/>
                    <td>
                        <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
                    </td>
                </td>
            </tr>   
        </xsl:for-each>
        </table>
    </div>
</body>

1 个答案:

答案 0 :(得分:0)

我不确定我是否误解了这个问题,但是对于您当前的模板,您正在第一个<td>内的xsl:for-each循环中写下第二个<td>,我想你不想要并且是无效的标记。只需改为

<xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER">
<xsl:sort select="." data-type="number" order="ascending"/>
   <tr>
      <td>
          <xsl:value-of select="."/>
      </td>
      <td>
          <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
      </td>
   </tr>
</xsl:for-each>

生成两列。

更新:要求打印10列表以打印前两列中升序的前50个值,然后在第3列和第4列中继续执行50到100之间的值等。 - 一个例子,说明如何实现12个值(为了便于阅读) 对于示例输入,我刚刚将4个产品项添加到示例XML中。遵循XSLT

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" doctype-public="XSLT-compat"
omit-xml-declaration="yes" encoding="UTF-8" indent="no" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
   <div>
      <table style="font-size:9pt; border-style:none; border-collapse:collapse;
       vertical-align:top; font-family:Univers Condensed;" border="1" 
       cellspacing="0" cellpadding="0">
        <tr>
          <td> <p>Internazionale</p>
          </td>
          <td> <p>Italiano</p>
          </td>
          <td> <p>Internazionale</p>
          </td>
          <td> <p>Italiano</p>
          </td>
          <td> <p>Internazionale</p>
          </td>
          <td> <p>Italiano</p>
          </td>
          <td> <p>Internazionale</p>
          </td>
          <td> <p>Italiano</p>
          </td>
          <td> <p>Internazionale</p>
          </td>
          <td> <p>Italiano</p>
          </td>
        </tr>
        <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER ">
        <xsl:sort select="." data-type="number" order="ascending"/>
        <xsl:variable name="pos" select="position()"/>
          <xsl:if test="not(position() >2)">
            <tr>
              <td>
                <xsl:value-of select="."/>
              </td>
              <td>
                <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
              </td>
              <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER ">
              <xsl:sort select="." data-type="number" order="ascending"/>
                <xsl:if test="position()=$pos+2">
                  <td>
                    <xsl:value-of select="."/>
                  </td>
                  <td>
                    <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
                  </td>
                </xsl:if>
              </xsl:for-each>
              <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER ">
              <xsl:sort select="." data-type="number" order="ascending"/>
                <xsl:if test="position()=$pos+4">
                  <td>
                    <xsl:value-of select="."/>
                  </td>
                  <td>
                    <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
                  </td>
                </xsl:if>
              </xsl:for-each>
              <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER ">
              <xsl:sort select="." data-type="number" order="ascending"/>
                <xsl:if test="position()=$pos+6">
                  <td>
                    <xsl:value-of select="."/>
                  </td>
                  <td>
                    <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
                  </td>
                </xsl:if>
              </xsl:for-each>
              <xsl:for-each select="//INTERNATIONAL_LIGHT_NUMBER ">
              <xsl:sort select="." data-type="number" order="ascending"/>
                <xsl:if test="position()=$pos+8">
                  <td>
                    <xsl:value-of select="."/>
                  </td>
                  <td>
                    <xsl:value-of select="preceding-sibling::ITA_LIGHT_NUMBER"/>
                  </td>
                </xsl:if>
              </xsl:for-each>
            </tr>
          </xsl:if>
        </xsl:for-each>
      </table>
    </div>
  </xsl:template>
</xsl:stylesheet>

结果HTML:

<div>
 <table style="font-size:9pt; border-style:none; border-collapse:collapse;
  vertical-align:top; font-family:Univers Condensed;" border="1" 
  cellspacing="0" cellpadding="0">
    <tr>
     <td>
        <p>Internazionale</p>
     </td>
     <td>
        <p>Italiano</p>
     </td>
     <td>
        <p>Internazionale</p>
     </td>
     <td>
        <p>Italiano</p>
     </td>
     <td>
        <p>Internazionale</p>
     </td>
     <td>
        <p>Italiano</p>
     </td>
     <td>
        <p>Internazionale</p>
     </td>
     <td>
        <p>Italiano</p>
     </td>
     <td>
        <p>Internazionale</p>
     </td>
     <td>
        <p>Italiano</p>
     </td>
    </tr>
    <tr>
     <td>E2686.5</td>
     <td>4555</td>
     <td>E2686.7</td>
     <td>4557</td>
     <td>E2686.9</td>
     <td>4559</td>
     <td>E2686.11</td>
     <td>4561</td>
     <td>E2686.13</td>
     <td>4563</td>
    </tr>
    <tr>
     <td>E2686.6</td>
     <td>4556</td>
     <td>E2686.8</td>
     <td>4558</td>
     <td>E2686.10</td>
     <td>4560</td>
     <td>E2686.12</td>
     <td>4562</td>
     <td>E2686.14</td>
     <td>4564</td>
    </tr>
  </table>
</div> 

要处理50个值的组,您只需调整列组的值和初始值<xsl:for-each> - 必须将<xsl:if test="not(position() >2)">调整为<xsl:if test="not(position() >50)">。对于接下来的50个值,<xsl:if test="position()=$pos+2">必须调整为<xsl:if test="position()=$pos+50">,依此类推。