带有空白单元格的XSL和XML动态行

问题描述 投票:0回答:1

我是XSL的新手,我正在尝试使用以下结构构建一个表:enter image description here

我正在使用这个XML:

<CARS>
    <CAR>
        <CAR_NUM>65</CAR_NUM>
        <DRIVERS>
            <DRIVER>
                <DRIVER_NUM>123</DRIVER_NUM>
                <DRIVER_NAME>STEVE RODGERS</DRIVER_NAME>
            </DRIVER>
        </DRIVERS>
        <STATS>
            <STAT>
                <LAP>1</LAP>
                <TIME>3:21:10</TIME>
            </STAT>
            <STAT>
                <LAP>2</LAP>
                <TIME>3:21:07</TIME>
            </STAT>
        </STATS>
    </CAR>
    <CAR>
        <CAR_NUM>22</CAR_NUM>

        <DRIVERS>
            <DRIVER>
                <DRIVER_NUM>143</DRIVER_NUM>
                <DRIVER_NAME>TONY STARK</DRIVER_NAME>
            </DRIVER>
            <DRIVER>
                <DRIVER_NUM>155</DRIVER_NUM>
                <DRIVER_NAME>JAMES RHODES</DRIVER_NAME>
            </DRIVER>
        </DRIVERS>

        <STATS>
            <STAT>
                <LAP>1</LAP>
                <TIME>3:22:39</TIME>
            </STAT>
            <STAT>
                <LAP>2</LAP>
                <TIME>3:19:17</TIME>
            </STAT>
            <STAT>
                <LAP>3</LAP>
                <TIME>3:15:46</TIME>
            </STAT>
            <STAT>
                <LAP>4</LAP>
                <TIME>3:17:22</TIME>
            </STAT>
        </STATS>
    </CAR>
</CARS>

没有比圈数更多的司机。 CAR NUMBER对表格进行嵌套是为了解决所有问题,因为我不知道如何使用XSL递归地生成空白字段(我猜)。

我知道我的第一次尝试已经开始,但在这里......

<table>
    <xsl:for-each select="CARS/CAR">
    <tr>
        <td><xsl:value-of select="CAR_NUM"/>
        </td>
        <xsl:for-each select="DRIVERS/DRIVER">
        <td><xsl:value-of select="DRIVER_NUM"/>
        </td>
        <td><xsl:value-of select="DRIVER_NAME"/>
        </td>
        </xsl:for-each>
        <xsl:for-each select="STATS/STAT">
        <td><xsl:value-of select="LAP"/>
        </td>
        <td><xsl:value-of select="TIME"/>
        </td>
        </xsl:for-each>
        </tr>
        </xsl:for-each>
</table>

这种尝试导致:

65 123 STEVE RODGERS 1 3:21:10 2 3:21:07

22 143 TONY STARK 155 JAMES RHODES 1 3:22:39 2 3:19:17 3 3:15:46 4 3:17:22

我无法解释在循环处理过程中如何创建空单元格和行。

编辑:研究它我相信我需要将XML分析为层次结构层:

Tier 1: CAR_NUM
Tier 2: DRIVER_NUM, DRIVER_NAME
Tier 3: LAP, TIME

我需要在运行第一条记录后嵌套一对if:

T1->T2->T3 - End the row, then test for next record in T2 (if found go to T2)
T2->T3 - End the row, then test for next record in T2 (if not found, go T3)
T3
xml xslt xslt-1.0
1个回答
0
投票

正如评论中指出的那样,我还没有完全理解如何将驾驶员与圈数联系起来,如果关系仅仅是所有驾驶员,但是最后一个只做一圈而最后一个驾驶员完成所有剩余的圈数,那么你可以按如下方式实现:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

  <xsl:output method="html" indent="yes" version="5" doctype-system="about:legacy-doctype"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>.NET XSLT Fiddle Example</title>
        <style>
            th, td { vertical-align: top }
        </style>
      </head>
      <body>
          <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="CARS">
      <table>
          <thead>
              <tr>
                  <th>CAR NUMBER</th>
                  <th>DRIVER NUMBER</th>
                  <th>DRIVER NAME</th>
                  <th>LAP</th>
                  <th>TIME</th>
              </tr>
          </thead>
          <xsl:apply-templates select="CAR"/>
      </table>
  </xsl:template>

  <xsl:template match="CAR">
      <tbody>
          <xsl:apply-templates select="STATS/STAT"/>
      </tbody>
  </xsl:template>

  <xsl:template match="STAT">
      <tr>
          <xsl:call-template name="create-cells"/>
      </tr>
  </xsl:template>

  <xsl:template match="STAT[1]">
      <tr>
          <td rowspan="{count(../STAT)}">
              <xsl:value-of select="ancestor::CAR/CAR_NUM"/>
          </td>
          <xsl:call-template name="create-cells"/>
      </tr>
  </xsl:template>

  <xsl:template name="create-cells">
      <xsl:variable name="pos" select="position()"/>
      <xsl:apply-templates select="ancestor::CAR/DRIVERS/DRIVER[$pos]/*"/>
      <xsl:apply-templates/>      
  </xsl:template>

  <xsl:template match="DRIVER/*">
      <td>
          <xsl:apply-templates/>
      </td>
  </xsl:template>

  <xsl:template match="DRIVER[last()]/*">
      <td rowspan="{count(ancestor::CAR/STATS/STAT) - count(../DRIVER)}">
          <xsl:apply-templates/>
      </td>
  </xsl:template>

  <xsl:template match="LAP | TIME">
      <td>
          <xsl:value-of select="."/>
      </td>
  </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/6qVRKwy在线

创建的HTML表似乎符合要求。

© www.soinside.com 2019 - 2024. All rights reserved.