对齐表格标题和表格单元格

时间:2013-06-11 07:20:55

标签: xml xslt

我有以下示例XML:

<javancss>
    <packages>
        <package id="1">
            <name>Package 1</name>
            <File ID="1">
                <classes>3</classes>
                <functions>21</functions>
                <ncss>283</ncss>
                <javadocs>20</javadocs>
                <javadoc_lines>111</javadoc_lines>
                <single_comment_lines>11</single_comment_lines>
                <multi_comment_lines>221</multi_comment_lines>
            </File>
            <File ID="2">
                <classes>3</classes>
                <functions>21</functions>
                <ncss>285</ncss>
                <javadocs>20</javadocs>
                <javadoc_lines>111</javadoc_lines>
                <single_comment_lines>11</single_comment_lines>
                <multi_comment_lines>222</multi_comment_lines>
            </File>
        </package>
</packages>

 <objects>
        <object id="1">
            <name>Object 1</name>
            <File ID="1">
                <ncss>253</ncss>
                <functions>17</functions>
                <classes>2</classes>
                <javadocs>20</javadocs>
            </File>
            <File ID="2">
                <ncss>255</ncss>
                <functions>17</functions>
                <classes>2</classes>
                <javadocs>20</javadocs>
            </File>
        </object>
<objects>

<functions>
 <function id="10">
            <name>Function 1</name>
            <File ID="1">
                <ncss>61</ncss>
                <ccn>29</ccn>
                <javadocs>1</javadocs>
            </File>
            <File ID="2">
                <ncss>61</ncss>
                <ccn>29</ccn>
                <javadocs>1</javadocs>
            </File>
        </function>
</functions>
</javancss>

以下XSLT将其翻译为HTML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>

<head>
</head>

<body>

<center>  <h2>Package Summary</h2> </center>

<table border="1" >

  <tr>
    <th  > ID</th>
    <th  > Name</th>
    <th width="20"  >File</th>
    <th width="46"  >Classes</th>
    <th width="58"  >Functions</th>
    <th width="60"  >NCSS</th>
    <th width="82"  >Javadocs</th>
    <th width="130"  >Javadoc Lines </th>
    <th width="199"  >Single Comment Lines </th>
    <th width="126"  >Multi Comment Lines </th>
  </tr>

  <xsl:for-each select="/javancss/packages/package">

<tr>
    <td  > <xsl:value-of select="@id" /> </td>
    <td align="left" > <xsl:value-of select="name" /></td>
    <td  colspan="8"><td />
<table border="1">

    <xsl:for-each select="File">
<tr >
            <td width="20"  > <a href="#FileList"><xsl:value-of select="@ID" /> </a></td>
        <td width="46"  > <xsl:value-of select="classes" /></td>
            <td width="58"  > <xsl:value-of select="functions" /> </td>
            <td width="60"  ><xsl:value-of select="ncss" />      </td>
            <td width="82"  ><xsl:value-of select="javadocs" /></td>
            <td width="130"  ><xsl:value-of select="javadoc_lines" /> </td>
            <td width="199"  ><xsl:value-of select="single_comment_lines" /> </td>
            <td width="126"  ><xsl:value-of select="multi_comment_lines" /> </td>
    </tr></xsl:for-each>
    </table></td>
  </tr>
</xsl:for-each>
</table>

<center>  <h2>Object Summary</h2> </center>

<table border="1" >

  <tr>
    <th  > ID</th>
    <th  > Name</th>
    <th width="20"  >File</th>
    <th width="46"  >Classes</th>
    <th width="58"  >Functions</th>
    <th width="60"  >NCSS</th>
    <th width="82"  >Javadocs</th>
  </tr>

  <xsl:for-each select="/javancss/objects/object">

<tr>
    <td  > <xsl:value-of select="@id" /> </td>
    <td align="left" > <xsl:value-of select="name" /></td>
    <td  colspan="8"><table border="1">
    <xsl:for-each select="File">
<tr>
            <td width="20"  ><a href="#FileList"><xsl:value-of select="@ID" /> </a></td>
        <td width="46"  > <xsl:value-of select="classes" /></td>
            <td width="58"  > <xsl:value-of select="functions" /> </td>
            <td width="60"  ><xsl:value-of select="ncss" />      </td>
            <td width="82"  ><xsl:value-of select="javadocs" /></td>
    </tr></xsl:for-each>
    </table></td>
  </tr>
</xsl:for-each>
</table>

<center>  <h2>Functions Summary</h2> </center>

<table border="1">

  <tr>
    <th  > ID</th>
    <th  > Name</th>
    <th width="20"  >File</th>
    <th width="46"  >CCN</th>
    <th width="60"  >NCSS</th>
    <th width="82"  >Javadocs</th>
  </tr>

  <xsl:for-each select="/javancss/functions/function">

<tr>
    <td  > <xsl:value-of select="@id" /> </td>
    <td align="left" > <xsl:value-of select="name" /></td>
    <td  colspan="8"><table border="1">
    <xsl:for-each select="File"><tr>
            <td width="20"  ><a href="#FileList"><xsl:value-of select="@ID" /> </a></td>
        <td width="46"  > <xsl:value-of select="ccn" /></td>
            <td width="60"  ><xsl:value-of select="ncss" />      </td>
            <td width="82"  ><xsl:value-of select="javadocs" /></td>
    </tr></xsl:for-each>
    </table></td>
  </tr>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>
</xsl:stylesheet>

我无法将表格标题和单元格对齐在一起。我想知道如何去做,如果可能的话,如何使这种行为与所有浏览器交叉兼容。

所有帮助将不胜感激

1 个答案:

答案 0 :(得分:1)

您不应该在表格中使用表格。尝试使用 rowspan
例如,对于您的第一个表(包汇总),尝试这样的事情:

<center>
    <h2>Package Summary</h2>
</center>

<table border="1" >

    <tr>
        <th  > ID</th>
        <th  > Name</th>
        <th width="20"  >File</th>
        <th width="46"  >Classes</th>
        <th width="58"  >Functions</th>
        <th width="60"  >NCSS</th>
        <th width="82"  >Javadocs</th>
        <th width="130"  >Javadoc Lines </th>
        <th width="199"  >Single Comment Lines </th>
        <th width="126"  >Multi Comment Lines </th>
    </tr>

    <xsl:for-each select="/javancss/packages/package">
        <xsl:variable name="filescnt" select="count(File)" />
        <xsl:for-each select="File">
            <tr>
                <xsl:if test="position() =1" >
                    <td  rowspan="{$filescnt}">
                        <xsl:value-of select="../@id" />
                    </td>

                    <td align="left"  rowspan="{$filescnt}">
                        <xsl:value-of select="../name" />
                    </td>
                </xsl:if>
                <td width="20"  >
                    <a href="#FileList">
                        <xsl:value-of select="@ID" />
                    </a>
                </td>
                <td width="46"  >
                    <xsl:value-of select="classes" />
                </td>
                <td width="58"  >
                    <xsl:value-of select="functions" />
                </td>
                <td width="60"  >
                    <xsl:value-of select="ncss" />
                </td>
                <td width="82"  >
                    <xsl:value-of select="javadocs" />
                </td>
                <td width="130"  >
                    <xsl:value-of select="javadoc_lines" />
                </td>
                <td width="199"  >
                    <xsl:value-of select="single_comment_lines" />
                </td>
                <td width="126"  >
                    <xsl:value-of select="multi_comment_lines" />
                </td>

            </tr>
        </xsl:for-each>
    </xsl:for-each>
</table>