首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSL for-每个具有获取动态列号的位置的

XSL for-每个具有获取动态列号的位置的
EN

Stack Overflow用户
提问于 2015-03-21 02:10:04
回答 1查看 2.2K关注 0票数 0

这对我没用。根据XML中的节点数,我试图为我的表获得正确的额外列数。在这个例子中,我有两个“实质性”节点,但是我的XML数据可以有一个到四个,因此我的表可以有一个和四个额外的列。第一列将永远在那里。

以下是我的XML:

代码语言:javascript
复制
<stuff>
    <thing>house</thing>
    <color>red</color>
</stuff>
<stuff>
    <thing>hat</thing>
    <color>brown</color>
</stuff>

这是我的XSL:

代码语言:javascript
复制
<fo:table width="100%" table-layout="fixed">
    <fo:table-column column-width="27%" column-number="1"/>
    <xsl:for-each select="stuff">
        <xsl:if test="position()=1">
            <fo:table-column column-width="73%" column-number="2"/>
        </xsl:if>
        <xsl:if test="position()=2">
            <fo:table-column column-width="36.5%" column-number="2"/>
            <fo:table-column column-width="36.5%" column-number="3"/>
        </xsl:if>
        <xsl:if test="position()=3">
            <fo:table-column column-width="24%" column-number="2"/>
            <fo:table-column column-width="24%" column-number="3"/>
            <fo:table-column column-width="25%" column-number="4"/>
        </xsl:if>
        <xsl:if test="position()=4">
            <fo:table-column column-width="18.25%" column-number="2"/>
            <fo:table-column column-width="18.25%" column-number="3"/>
            <fo:table-column column-width="18.25%" column-number="4"/>
            <fo:table-column column-width="18.25%" column-number="5"/>
        </xsl:if>                                   
    </xsl:for-each>
    <fo:table-body>
        <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <fo:block>First Row</fo:block>
                </fo:block>
            </fo:table-cell>
            <xsl:for-each select="stuff">
                <fo:table-cell>
                    <fo:block>
                        <xsl:value-of select="thing"/>
                    </fo:block>
                </fo:table-cell>
            </xsl:for-each>
        </fo:table-row> 
        <fo:table-row>
            <fo:table-cell>
                <fo:block>
                    <fo:block>Second Row</fo:block>
                </fo:block>
            </fo:table-cell>
            <xsl:for-each select="stuff">
                <fo:table-cell>
                    <fo:block>
                        <xsl:value-of select="color"/>
                    </fo:block>
                </fo:table-cell>
            </xsl:for-each>
        </fo:table-row>         
    </fo:table-body>
</fo:table>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-21 02:40:00

我觉得你想做这样的事:

代码语言:javascript
复制
<xsl:template match="parent-of-stuff">
    <fo:table width="100%" table-layout="fixed">
        <fo:table-column column-width="27%" column-number="1"/>
        <xsl:variable name="n" select="count(stuff)" />
        <xsl:for-each select="stuff">
            <fo:table-column column-width="{73 div $n}%" column-number="{position() + 1}"/>
        </xsl:for-each>

        <!-- the rest of the table -->

  </fo:table>  
</xsl:template>

如果3列场景的column-width="24.3333333333333%"结果不可接受,请执行以下操作:

代码语言:javascript
复制
<xsl:template match="parent-of-stuff">
    <fo:table width="100%" table-layout="fixed">
        <fo:table-column column-width="27%" column-number="1"/>
        <xsl:variable name="n" select="count(stuff)" />
        <xsl:for-each select="stuff">
            <fo:table-column column-number="{position() + 1}">
                <xsl:attribute name="column-width">
                    <xsl:choose>
                        <xsl:when test="$n=3 and position()=3">25%</xsl:when>
                        <xsl:when test="$n=3">24%</xsl:when>
                        <xsl:otherwise>
                            <xsl:value-of select="73 div $n" />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:attribute>
            </fo:table-column>
        </xsl:for-each>

    <!-- the rest -->

  </fo:table>  
</xsl:template>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29178486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档