CALS表未正确创建

时间:2014-01-15 09:54:36

标签: html xml xslt

我有以下XML代码(表格)

<root>
  <table frame="all">
    <tgroup cols="9">
      <colspec colname="col1" colnum="1" colwidth="9*"/>
      <colspec colname="col2" colnum="2" colwidth="12*"/>
      <colspec colname="col3" colnum="3" colwidth="14*"/>
      <colspec colname="col4" colnum="4" colwidth="10*"/>
      <colspec colname="col5" colnum="5" colwidth="11*"/>
      <colspec colname="col6" colnum="6" colwidth="11*"/>
      <colspec colname="col7" colnum="7" colwidth="11*"/>
      <colspec colname="col8" colnum="8" colwidth="11*"/>
      <colspec colname="col9" colnum="9" colwidth="11*"/>
    <tbody>
      <row rowsep="1">
        <entry colname="col1" align="center" valign="top" colsep="1"><para>BIL</para></entry>
        <entry colname="col2" align="center" valign="top" colsep="1"><para>NO KES</para></entry>
        <entry colname="col3" align="center" valign="top" colsep="1"><para>NAMA PEMBELI</para></entry>
        <entry colname="col4" align="center" valign="top" colsep="1"><para>NO. KP</para></entry>
        <entry colname="col5" align="center" valign="top" colsep="1"><para>ALAMAT PEMBELI</para></entry>
        <entry colname="col6" align="center" valign="top" colsep="1"><para>TARIKH LELONG</para></entry>
        <entry colname="col7" align="center" valign="top" colsep="1"><para>TARIKH LUPUT</para></entry>
        <entry colname="col8" align="center" valign="top" colsep="1"><para>HARGA RIZAB</para></entry>
        <entry colname="col9" align="center" valign="top"><para>HARGA JUALAN</para></entry>
      </row>
      <row rowsep="1">
        <entry colname="col1" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col2" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col3" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col4" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col5" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col6" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col7" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col8" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col9" align="left" valign="top"><para></para></entry>
      </row>
      <row>
        <entry colname="col1" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col2" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col3" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col4" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col5" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col6" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col7" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col8" align="left" valign="top" colsep="1"><para></para></entry>
        <entry colname="col9" align="left" valign="top"><para></para></entry>
      </row>
    </tbody>
  </tgroup>
</table>

<table frame="all">
  <tgroup cols="9">
    <colspec colname="col1" colnum="1" colwidth="6*"/>
    <colspec colname="col2" colnum="2" colwidth="6*"/>
    <colspec colname="col3" colnum="3" colwidth="6*"/>
    <colspec colname="col4" colnum="4" colwidth="6*"/>
    <colspec colname="col5" colnum="5" colwidth="7*"/>
    <colspec colname="col6" colnum="6" colwidth="7*"/>
    <colspec colname="col7" colnum="7" colwidth="25*"/>
    <colspec colname="col8" colnum="8" colwidth="10*"/>
    <colspec colname="col9" colnum="9" colwidth="15*"/>
  <tbody>
    <row>
      <entry namest="col1" nameend="col3" align="center" valign="top" colsep="1"><para>DEPOSIT 10% HARGA RIZAB</para></entry>
      <entry namest="col4" nameend="col6" align="center" valign="top" colsep="1"><para>BAKI 90% HARGA BELIAN</para></entry>
      <entry colname="col7" align="left" valign="top" colsep="1" rowsep="1" morerows="1"><para>TARIKH PEMYATA BAILIF DAN BAUCARDISEDIAKAN</para></entry>
      <entry colname="col8" align="left" valign="top" colsep="1" rowsep="1" morerows="1"><para>TARIKH PENYATA DAN TARIKH BAUCAR DIHANTAR KE BAHAGIAN KEWANGAN</para></entry>
      <entry colname="col9" align="left" valign="top" rowsep="1" morerows="1"><para>TARIK CEK BAYARAN DIKELUARKAN/DIHANTAR KEPADA PENAWAR</para></entry>
    </row>
    <row rowsep="1">
      <entry colname="col1" align="center" valign="top"><para>Tarikh</para></entry>
      <entry colname="col2" align="center" valign="top"><para>No. Resit</para></entry>
      <entry colname="col3" align="center" valign="top" colsep="1"><para>Jumlah</para></entry>
      <entry colname="col4" align="left" valign="top"><para>Tarikh</para></entry>
      <entry colname="col5" align="center" valign="top"><para>No. Resit</para></entry>
      <entry colname="col6" align="center" valign="top" colsep="1"><para>Jumlah</para></entry>
      <entry colname="col7" align="left" valign="top"><para></para></entry>
      <entry colname="col8" align="left" valign="top"><para></para></entry>
      <entry colname="col9" align="left" valign="top"><para></para></entry>
    </row>
    <row>
      <entry colname="col1" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col2" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col3" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col4" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col5" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col6" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col7" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col8" align="left" valign="top" colsep="1"><para></para></entry>
      <entry colname="col9" align="left" valign="top"><para></para></entry>
    </row>
  </tbody>
</tgroup>
</table>
</root>

我正在使用下面的XSLT:

      <xsl:template name="table" match="table">
    <table class="frame-{current()/@frame}">
      <xsl:apply-templates/>
    </table>
  </xsl:template>


  <xsl:template match="tgroup">
  <xsl:if test="not(preceding-sibling::tgroup)">
  <xsl:if test="@colsep|@rowsep|@align|cols">

</xsl:if>
</xsl:if>


    <colgroup>
      <xsl:apply-templates select=".//colspec" />
    </colgroup>
    <xsl:apply-templates select="child::node()[not(self::colspec)]" />
  </xsl:template>
  <xsl:template name="tbody" match="tgroup/tbody">
    <tbody>
      <xsl:for-each select="current()/row">
        <xsl:call-template name="row" />
      </xsl:for-each>
    </tbody>
  </xsl:template>
  <xsl:template name="thead" match="tgroup/thead">

    <xsl:for-each select="current()/row">
      <thead>
        <tr>
          <xsl:for-each select="current()/entry">
            <xsl:call-template name="headentry" />
          </xsl:for-each>
        </tr>
      </thead>
    </xsl:for-each>
  </xsl:template>





  <xsl:template name="colspec" match="colspec">


    <xsl:variable name="b">
      <xsl:value-of select="sum(../colspec/number(substring-before(@colwidth,'in')))"/>
    </xsl:variable>

    <xsl:choose>
      <xsl:when test="@colnum">
        <col class="colnum-{current()/@colnum} colname-{current()/@colname} colwidth-{translate(@colwidth,'*','')}" />
      </xsl:when>
      <xsl:otherwise>
        <col class="colname-{current()/@colname} colwidth-{translate(@colwidth,'*','')}" />
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text disable-output-escaping="yes"><![CDATA[</col>]]></xsl:text>
  </xsl:template>
  <xsl:template name="row" match="tbody/row">
    <tr>
      <xsl:for-each select="current()/entry">
        <xsl:call-template name="entry" />
      </xsl:for-each>
    </tr>
  </xsl:template>


  <xsl:template match="entry" name="entry">

  <xsl:choose>

  <xsl:when test="./@namest">
            <xsl:variable name="namest" select="@namest"/>
    <xsl:variable name="nameend" select="@nameend"/>
    <xsl:variable name="namestPos" select="count(ancestor::tgroup/colspec[@colname=$namest]/preceding-sibling::colspec)"/>
    <xsl:variable name="nameendPos" select="count(ancestor::tgroup/colspec[@colname=$nameend]/preceding-sibling::colspec)"/>

    <td colspan="{$nameendPos - $namestPos + 1}" align="{@align}" valign="{@valign}">

      <xsl:apply-templates/>
    </td>
        </xsl:when>
        <xsl:when test="@align and ./@morerows">
        <td class="align-{@align}" rowspan="{number(./@morerows)+1}"  valign="{@valign}">
        <xsl:for-each select="para">
<xsl:choose>
    <xsl:when test="../@colname='col4' and contains(./text(),'/')">
        <xsl:value-of select="concat('er:#MCP_PD_',substring-before(.,'/'),'/','P',translate(./text(),'/','-'))"/>
    </xsl:when>
    <xsl:otherwise>
          <div class="para">
            <xsl:apply-templates/>
          </div>
    </xsl:otherwise>
</xsl:choose>

          </xsl:for-each>
        </td>
      </xsl:when>
      <xsl:when test="./@morerows and not(./@align)">
                          <td rowspan="{number(./@morerows)+1}"  valign="{@valign}">
<xsl:for-each select="para">         
         <xsl:choose>
    <xsl:when test="../@colname='col4' and contains(./text(),'/')">
    <xsl:value-of select="concat('er:#MCP_PD_',substring-before(.,'/'),'/','P',translate(./text(),'/','-'))"/>
    </xsl:when>
    <xsl:otherwise>
          <div class="para">
            <xsl:apply-templates/>
          </div>
    </xsl:otherwise>
</xsl:choose></xsl:for-each>
        </td>
        </xsl:when>

      <xsl:otherwise>
        <td class="align-{@align}" valign="{@valign}">
        <xsl:for-each select="para">

       <xsl:choose>
    <xsl:when test="../@colname='col4' and contains(./text(),'/')">
        <a href="{concat('er:#MCP_PD_',substring-before(.,'/'),'/','P',translate(./text(),'/','-'))}">
<xsl:value-of select="./text()"/>       
        </a>
    </xsl:when>
    <xsl:otherwise>
          <div class="para">
            <xsl:apply-templates/>
          </div>
    </xsl:otherwise>
</xsl:choose>
          </xsl:for-each>
        </td>
      </xsl:otherwise>
    </xsl:choose>

  </xsl:template>



  <xsl:template name="headentry">
    <xsl:choose>
      <xsl:when test="@align">
        <th class="align-{@align}"  valign="{@valign}
">
          <xsl:if test="translate(current()/@namest,'col','') != translate(current()/@nameend,'col','')">
            <xsl:variable name="colspan">
              <xsl:value-of select="number(translate(@nameend,'col','')) - number(translate(@namest,'col','')) + 1" />
            </xsl:variable>
            <xsl:attribute name="colspan">
              <xsl:value-of select="$colspan">
              </xsl:value-of>
            </xsl:attribute>
          </xsl:if>
          <xsl:for-each select="para">
            <div class="para">
              <xsl:apply-templates></xsl:apply-templates>
            </div>
          </xsl:for-each>
        </th>
      </xsl:when>
      <xsl:otherwise>
        <th>
          <xsl:if test="translate(current()/@namest,'col','') != translate(current()/@nameend,'col','')">
            <xsl:variable name="colspan">
           <!--   <xsl:value-of select="translate(current()/@nameend,'col','') - translate(current()/@namest,'col','') + 1" />-->
            </xsl:variable>
            <xsl:attribute name="colspan">
              <xsl:value-of select="$colspan">
              </xsl:value-of>
            </xsl:attribute>
          </xsl:if>
          <xsl:for-each select="para">
            <div class="para">
              <xsl:apply-templates></xsl:apply-templates>
            </div>
          </xsl:for-each>
        </th>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

输出的html代码如下。

<table class="frame-all">
                        <colgroup>
                            <col class="colnum-1 colname-col1 colwidth-9"></col><col class="colnum-2 colname-col2 colwidth-12"></col><col class="colnum-3 colname-col3 colwidth-14"></col><col class="colnum-4 colname-col4 colwidth-10"></col><col class="colnum-5 colname-col5 colwidth-11"></col><col class="colnum-6 colname-col6 colwidth-11"></col><col class="colnum-7 colname-col7 colwidth-11"></col><col class="colnum-8 colname-col8 colwidth-11"></col><col class="colnum-9 colname-col9 colwidth-11"></col></colgroup>
                        <tbody>
                            <tr>
                                <td class="align-center" valign="top">
                                    <div class="para">BIL</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">NO KES</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">NAMA PEMBELI</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">NO. KP</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">ALAMAT PEMBELI</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">TARIKH LELONG</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">TARIKH LUPUT</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">HARGA RIZAB</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">HARGA JUALAN</div>
                                </td>
                            </tr>
                            <tr>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                            </tr>
                            <tr>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <table class="frame-all">
                        <colgroup>
                            <col class="colnum-1 colname-col1 colwidth-6"></col><col class="colnum-2 colname-col2 colwidth-6"></col><col class="colnum-3 colname-col3 colwidth-6"></col><col class="colnum-4 colname-col4 colwidth-6"></col><col class="colnum-5 colname-col5 colwidth-7"></col><col class="colnum-6 colname-col6 colwidth-7"></col><col class="colnum-7 colname-col7 colwidth-25"></col><col class="colnum-8 colname-col8 colwidth-10"></col><col class="colnum-9 colname-col9 colwidth-15"></col></colgroup>
                        <tbody>
                            <tr>
                                <td colspan="3" align="center" valign="top">
                                    <div class="para">DEPOSIT 10% HARGA RIZAB</div>
                                </td>
                                <td colspan="3" align="center" valign="top">
                                    <div class="para">BAKI 90% HARGA BELIAN</div>
                                </td>
                                <td class="align-left" rowspan="2" valign="top">
                                    <div class="para">TARIKH PEMYATA BAILIF DAN BAUCARDISEDIAKAN</div>
                                </td>
                                <td class="align-left" rowspan="2" valign="top">
                                    <div class="para">TARIKH PENYATA DAN TARIKH BAUCAR DIHANTAR KE BAHAGIAN KEWANGAN</div>
                                </td>
                                <td class="align-left" rowspan="2" valign="top">
                                    <div class="para">TARIK CEK BAYARAN DIKELUARKAN/DIHANTAR KEPADA PENAWAR</div>
                                </td>
                            </tr>
                            <tr>
                                <td class="align-center" valign="top">
                                    <div class="para">Tarikh</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">No. Resit</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">Jumlah</div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para">Tarikh</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">No. Resit</div>
                                </td>
                                <td class="align-center" valign="top">
                                    <div class="para">Jumlah</div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                            </tr>
                            <tr>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                                <td class="align-left" valign="top">
                                    <div class="para"></div>
                                </td>
                            </tr>
                        </tbody>
                    </table>

我得到了一些错误的输出格式,下面是原始输出和我得到的。 原始输出: Original Output

当前输出: Current output

1 个答案:

答案 0 :(得分:0)

我相信您的输入XML是错误的。在这一行

<row>
    <entry namest="col1" nameend="col3" align="center" valign="top" colsep="1"><para>DEPOSIT 10% HARGA RIZAB</para></entry>
    <entry namest="col4" nameend="col6" align="center" valign="top" colsep="1"><para>BAKI 90% HARGA BELIAN</para></entry>
    <entry colname="col7" align="left" valign="top" colsep="1" rowsep="1" morerows="1"><para>TARIKH PEMYATA BAILIF DAN BAUCARDISEDIAKAN</para></entry>
    <entry colname="col8" align="left" valign="top" colsep="1" rowsep="1" morerows="1"><para>TARIKH PENYATA DAN TARIKH BAUCAR DIHANTAR KE BAHAGIAN KEWANGAN</para></entry>
    <entry colname="col9" align="left" valign="top" rowsep="1" morerows="1"><para>TARIK CEK BAYARAN DIKELUARKAN/DIHANTAR KEPADA PENAWAR</para></entry>
</row>

列col7,col8和col9声明为morerows="1",意味着列col7,col8和col9在下面进一步延伸1行。第二个<row>必须包含

<row rowsep="1">
    <entry colname="col1" align="center" valign="top"><para>Tarikh</para></entry>
    <entry colname="col2" align="center" valign="top"><para>No. Resit</para></entry>
    <entry colname="col3" align="center" valign="top" colsep="1"><para>Jumlah</para></entry>
    <entry colname="col4" align="left" valign="top"><para>Tarikh</para></entry>
    <entry colname="col5" align="center" valign="top"><para>No. Resit</para></entry>
    <entry colname="col6" align="center" valign="top" colsep="1"><para>Jumlah</para></entry>
</row>

我尝试用你的XSLT再次运行它,我不禁注意到这个

<col class="colnum-1 colname-col1 colwidth-9"/></col>

我相信这应该是

<col class="colnum-1 colname-col1 colwidth-9"/>

<col class="colnum-1 colname-col1 colwidth-9"></col>

但我现在看到额外的细胞现在消失了。