代码之家  ›  专栏  ›  技术社区  ›  Amal Alfin Paul

通过XSL添加colspan

  •  0
  • Amal Alfin Paul  · 技术社区  · 5 年前

    <td> 不知怎么的。例如,如果 size-for-desktop=8 科尔斯潘是 8 td 接下来呢 aga-block ,如果 size-for-desktop=4 控制关闭 </td> 8 (桌面大小=4+桌面大小=4)和下一个 size-for-desktop=2 控制关闭 < 只有当总和达到 (桌面大小=2+桌面大小=6)

    <aga-root>
        <aga-section type="default">
            <aga-block size-for-desktop="8" >
                <aga-text>1</aga-text>
            </aga-block>
            <aga-block size-for-desktop="4" >
                <aga-text>2</aga-text>
            </aga-block>
            <aga-block size-for-desktop="4" >
                <aga-text>3</aga-text>
            </aga-block>
            <aga-block size-for-desktop="2" >
                <aga-text>4</aga-text>
            </aga-block>
            <aga-block size-for-desktop="6" >
                <aga-text>5</aga-text>
            </aga-block>
            <aga-block size-for-desktop="8" >
                <aga-text>6</aga-text>
            </aga-block>
        </aga-section>
    </aga-root>
    

    XSL文件:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
    <xsl:output method="html" encoding="UTF-8"/>
    <xsl:param name="AccessKey" />
    <xsl:template match="/">
    <html>
    <head>
    <meta content="text/html charset=utf-8" http-equiv="Content-Type" />
    <meta content="width=device-width, initial-scale=1.0" name="viewport" />
    <title>Test</title>
    </head>
    <body>
    <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
    <tbody>
    <xsl:for-each select="//aga-section">
    <xsl:call-template name="ExploreAgaSection">
    <xsl:with-param name="Node" select="." />
    </xsl:call-template>
    </xsl:for-each>
    </tbody>
    </table>
    </body>
    </html>
    </xsl:template>
    <xsl:template name="ExploreAgaSection">
    <xsl:param name="Node" />
    <tr>
    <table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
    <xsl:choose>
    <xsl:when test="@size-for-desktop = 8">
        <xsl:for-each select="aga-block[@size-for-desktop]">
        <tr>
            <td>
            <xsl:attribute name="colspan"><xsl:value-of select="@size-for-desktop"/></xsl:attribute>
            <xsl:call-template name="ExploreAgaText">
            <xsl:with-param name="Node" select="." />
            </xsl:call-template>
            </td>
        </tr>
        </xsl:for-each>
    </xsl:when>
    <xsl:otherwise>
        <tr>
        <xsl:for-each select="aga-block[@size-for-desktop!=8]">
            <td>
            <xsl:attribute name="colspan"><xsl:value-of select="@size-for-desktop"/></xsl:attribute>
            <xsl:call-template name="ExploreAgaText">
            <xsl:with-param name="Node" select="." />
            </xsl:call-template>
            </td>
        </xsl:for-each>
        </tr>
    </xsl:otherwise>
    </xsl:choose>
    </table>
    </tr>
    </xsl:template>
    <xsl:template name="ExploreAgaText">
    <xsl:param name="Node" />
    <xsl:copy-of select="$Node/aga-text/node()" />
    </xsl:template>
    </xsl:stylesheet>
    

    电流输出:

        <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
         <tbody>
            <tr>
               <table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
                  <tr>
                     <td colspan="4">2</td>
                     <td colspan="4">3</td>
                     <td colspan="2">4</td>
                     <td colspan="6">5</td>
                  </tr>
               </table>
            </tr>
         </tbody>
      </table>
    

    <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
         <tbody>
            <tr>
               <table width="600" cellpadding="0" cellspacing="0" align="center" border="0" style="background-color:#fff; border-collapse:collapse; ">
                  <tr>
                     <td colspan="8">1</td>
                  </tr>
                  <tr>
                     <td colspan="4">2</td>
                     <td colspan="4">3</td>
                  </tr>
                  <tr>
                     <td colspan="2">4</td>
                     <td colspan="6">5</td>
                  </tr>
                     <td colspan="8">6</td>
                  </tr>
               </table>
            </tr>
         </tbody>
      </table>
    

    xsl transform

    0 回复  |  直到 5 年前
        1
  •  2
  •   Ilya Kharlamov    5 年前

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.1">
        <xsl:output method="html" encoding="UTF-8"/>
        <xsl:param name="AccessKey"/>
    
        <xsl:template name="split">
            <xsl:param name="from_including"  select="1"/>
            <xsl:param name="to_excluding" select="2"/>
            <xsl:param name="max_size"/>
            <xsl:variable name="sum_so_far"
                select="sum(aga-block[$from_including &lt;= position() and position() &lt; $to_excluding]/@size-for-desktop)"/>
            <xsl:if test="$sum_so_far > 0">
                <xsl:choose>
                    <xsl:when test="$sum_so_far >= $max_size or count(aga-block) &lt; $to_excluding">
                        <xsl:text>&#x0A;</xsl:text>
                        <tr>
                            <xsl:for-each
                                select="aga-block[$from_including &lt;= position() and position() &lt; $to_excluding]">
                                <xsl:text>&#x0A;    </xsl:text>
                                <td>
                                    <xsl:attribute name="colspan">
                                        <xsl:value-of select="@size-for-desktop"/>
                                    </xsl:attribute>
                                    <xsl:call-template name="ExploreAgaText"/>
                                </td>
                            </xsl:for-each>
                        </tr>
                        <xsl:call-template name="split">
                            <xsl:with-param name="from_including" select="$to_excluding"/>
                            <xsl:with-param name="to_excluding" select="$to_excluding + 1"/>
                            <xsl:with-param name="max_size" select="$max_size"/>
                        </xsl:call-template>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:call-template name="split">
                            <xsl:with-param name="from_including" select="$from_including"/>
                            <xsl:with-param name="to_excluding" select="$to_excluding + 1"/>
                            <xsl:with-param name="max_size" select="$max_size"/>
                        </xsl:call-template>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:if>
            
        </xsl:template>
    
        <xsl:template match="/">
            <html>
                <head>
                    <meta content="text/html charset=utf-8" http-equiv="Content-Type"/>
                    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
                    <title>Test</title>
                </head>
                <body>
                    <table width="100%" cellpadding="0" cellspacing="0" border="0" bgcolor="#ffffff">
                        <tbody>
                            <xsl:for-each select="//aga-section">
                                <xsl:call-template name="ExploreAgaSection">
                                    <xsl:with-param name="Node" select="."/>
                                </xsl:call-template>
                            </xsl:for-each>
                        </tbody>
                    </table>
                </body>
            </html>
        </xsl:template>
        <xsl:template name="ExploreAgaSection">
            <xsl:param name="Node"/>
            <tr>
                <table width="600" cellpadding="0" cellspacing="0" align="center" border="0"
                    style="background-color:#fff; border-collapse:collapse; ">
                    <xsl:call-template name="split">
                        <xsl:with-param name="max_size" select="8"/>
                    </xsl:call-template>
                </table>
            </tr>
        </xsl:template>
        <xsl:template name="ExploreAgaText">
            <xsl:param name="Node" select="."/>
            <xsl:copy-of select="$Node/aga-text/node()"/>
        </xsl:template>
    </xsl:stylesheet>