+ -
当前位置:首页 → 问答吧 → 表头自动合并含有相同内容的相邻单元格

表头自动合并含有相同内容的相邻单元格

时间:2003-09-10

来源:互联网

xml+xsl

[ 本贴由 宝玉 于 2003-9-10 17:48 最后编辑 ]

作者: 宝玉   发布时间: 2003-09-10

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type='text/xsl' href='print.xsl'?>
<table font-size="9pt" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <tr>
                <th>表头1        </th>
                <th>表头2</th>
                <th>表头2</th>
                <th>表头2        </th>
                <th>表头3</th>
                <th>表头4        </th>
        </tr>
        <tr>
                <th>表头1        </th>
                <th>表头21        </th>
                <th>表头22        </th>
                <th>表头23        </th>
                <th>表头3        </th>
                <th>表头4</th>
        </tr>
        <tr>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
        </tr>
        <tr>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
        </tr>
        <tr>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
        </tr>
        <tr>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
        </tr>
        <tr>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
                <td>数据</td>
        </tr>
</table>

[ 本贴由 宝玉 于 2003-9-10 17:38 最后编辑 ]

作者: 宝玉   发布时间: 2003-09-10

<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:Grandsoft.GPM.Script="http://www.grandsoft.com" version="1.0">
  <msxsl:script language="JScript" implements-prefix="Grandsoft.GPM.Script">
        <![CDATA[

                         function getColSpan(node)
                         {
                                        return CountColSpan(node(0));
       }  

                         function getRowSpan(node)
                         {
                                        return CountRowSpan(node(0));
       }  

                         function getTD(node)
                         {
                                return HaveRowSpan(node)
                         }

                         function CountColSpan(node)
                         {
                                        if(node == node.parentNode.lastChild)
                                                return 1;
                                        if(node.text==node.nextSibling.text)
                                                return CountColSpan(node.nextSibling)+1;
                                        return 1;
                         }

                         function CountRowSpan(node)
                         {
                                        var index = getIndex(node);
                                        if(node.parentNode == node.parentNode.parentNode.lastChild)
                                                return 1;
                                        var oNode = node.parentNode.nextSibling.childNodes(index);
                                        if(node.text==oNode.text)
                                                return CountColSpan(oNode)+1;
                                        return 1;
                         }

                         function HaveColSpan(node)
                         {
                                        if(node(0) == node(0).parentNode.firstChild)
                                                return 0;
                                        if(node(0).text==node(0).previousSibling.text)
                                                return 1;
                                        return 0;
                         }

                         function HaveRowSpan(node)
                         {
                                        if(node(0).parentNode == node(0).parentNode.parentNode.firstChild)
                                                return 0;
                                        var index = getIndex(node(0));

                                        var oNode = node(0).parentNode.previousSibling.childNodes(index);

                                        if(node(0).text==oNode.text)
                                                return 1;
                                        return 0;
                         }

                         function IfJump(node)
                         {
                                        if(HaveColSpan(node)==1 || HaveRowSpan(node)==1)
                                                return 1;
                                        return HaveColSpan(node);
                         }

                         function getIndex(node)
                         {
                                if(IsRoot(node))
                                        return 1;                               
                                var oNodeList = node.parentNode.childNodes;
                                var Item;
                                for (var i=0; i<oNodeList.length; i++) {
                                        Item = oNodeList.item(i);
                                        if(Item==node){
                                                return i;
                                        }
                                }
                                return 0;
                         }

                         function IsRoot(node)
                         {
                                        if(node.ownerDocument==node)
                                                return true;
                                        return false;
                         }

                ]]>
  </msxsl:script>
        <xsl:template match="/">
        <xsl:apply-templates select="table"/>
                <TABLE border="0" cellspacing="1" cellpadding="2" bgcolor="gray">

                                <xsl:for-each select="//tr">

                                        <tr height="18">

                                                <xsl:attribute name="class">
                                                        <xsl:if test="th">tablehead</xsl:if>
                                                        <xsl:if test="td">tabledata</xsl:if>
                                                </xsl:attribute>

                                                <xsl:apply-templates select="th"/>
                                                <xsl:apply-templates select="td"/>

                                        </tr>

                                </xsl:for-each>

                </TABLE>

        </xsl:template>

        <xsl:template match="th">
                <xsl:for-each select=".">
                        <xsl:if test="Grandsoft.GPM.Script:IfJump(.)=0">
                                <td>
                                                <xsl:if test="Grandsoft.GPM.Script:getColSpan(.)>1">
                                                        <xsl:attribute name="colspan">
                                                                <xsl:value-of select="Grandsoft.GPM.Script:getColSpan(.)"/>
                                                        </xsl:attribute>
                                                        <xsl:attribute name="align">center</xsl:attribute>
                                                </xsl:if>
                                                <xsl:if test="Grandsoft.GPM.Script:getRowSpan(.)>1">
                                                        <xsl:attribute name="rowspan">
                                                                <xsl:value-of select="Grandsoft.GPM.Script:getRowSpan(.)"/>
                                                        </xsl:attribute>
                                                </xsl:if>
                                                <xsl:value-of select="."/>
                                </td>
                        </xsl:if>
                </xsl:for-each>
        </xsl:template>

       
        <xsl:template match="td">
                <xsl:for-each select=".">
                        <td><xsl:value-of select="."/></td>
                </xsl:for-each>
        </xsl:template>

        <xsl:template match="table">
                <style>
                        .tablehead{
                                background-color: menu;
                        }
                        .tabledata{
                                background-color: #FFFFFF;
                        }
                        td{
                                font-size:<xsl:value-of select="@font-size"/>
                        }
                </style>
        </xsl:template>

</xsl:stylesheet>

[ 本贴由 宝玉 于 2003-9-10 17:36 最后编辑 ]

作者: 宝玉   发布时间: 2003-09-10

看来真的好冷清.

回一个.....支持鲍鱼.

作者: ※潇洒※   发布时间: 2003-09-12

说说原理,让大家更能理解你的思路

作者: 车仔   发布时间: 2003-09-12

是啊. 俺看得一头雾水...

  Grandsoft.GPM.Script  这些都是啥东东来的?

作者: ※潇洒※   发布时间: 2003-09-13

这么长的代码是怎么写出来的啊?????

作者: 飘逸书生   发布时间: 2003-10-18

有点意思,希望出更多的更好的作品

作者: KAI   发布时间: 2003-10-19



QUOTE:
[i]※潇洒※[/i] 于 2003-9-13 10:57 PM 写道:
是啊. 俺看得一头雾水...

  Grandsoft.GPM.Script  这些都是啥东东来的?

这个是命名空间,可以定义成你想要的,呵呵
grandsoft是我公司的名字
gpm是我所在部门
script就表示是脚本

作者: 宝玉   发布时间: 2003-10-19