XSLT &XSL-FO:创建多行表? [英] XSLT & XSL-FO: Creating a table with multiple rows?

查看:30
本文介绍了XSLT &XSL-FO:创建多行表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 XSLT 还很陌生,我遇到了一个问题,我有一个包含未知数量的子元素的 Element,我需要在表格中显示这些子元素,以便有 5-6 列可供显示信息.

I'm fairly new to XSLT and I am stuck with a problem where I have an Element with an unknown amount of children, and I need to display these children in a table such that there are 5-6 columns available to display the information.

如果我得到一个像这样的 XML 文件:

If I'm given an XML file that looks like this:

<books>
    <book>
        <author>Ralls, Kim</author>
        <title>Midnight Rain</title>
    </book>
    <book>
        <author>Corets, Eva</author>
        <title>Maeve Ascendant</title>
    </book>
    <book>
        <author>Corets, Eva</author>
        <title>Oberon's Legacy</title>
    </book>
    <book>
        <author>Randall, Cynthia</author>
        <title>Lover Birds</title>
    </book>
    <book>
        <author>Thurman, Paula</author>
        <title>Splish Splash</title>
    </book>
    <book>
        <author>Knorr, Stefan</author>
        <title>Creepy Crawlies</title>
    </book>
    <book>
        <author>Kress, Peter</author>
        <title>Paradox Lost</title>
    </book>
    <book>
        <author>Crichton, Michael</author>
        <title>Jurassic Park</title>
    </book>
    <book>
        <author>Orwell, George</author>
        <title>1984</title>
    </book>
    <book>
        <author>Martin, George</author>
        <title>A Song of Ice And Fire</title>
    </book>
</books>

我想在一个由两行五列组成的表格中显示这 10 本书.

I would like to display these 10 books in a table consisting of two rows and five columns.

我已经走到这一步了:

<xsl:template match="books" mode="table">
    <fo:table margin-left="auto" margin-right="auto">
        <fo:table-body>
            <fo:table-row table-layout="fixed">
                <xsl:for-each select="skill">
                    <fo:table-cell border="1">
                        <fo:block font-weight="bold">
                            <xsl:value-of select="name"/>
                        </fo:block>
                    </fo:table-cell>    
                </xsl:for-each>
            </fo:table-row>
        </fo:table-body>
    </fo:table>
</xsl:template>

但是所有这一切都只是将每个单元格放在同一行上.我正在寻找一种方法来检查循环是否运行了一定次数(5 或 6 次),并在发生这种情况时插入新行,但我不知道这是否可以在 XSL 中执行.

But all this will do is put every cell on the same row. I'm looking for a way to check if the loop has run a certain amount of times (5 or 6), and inserting a new row when that happens, but I don't know if that's something I can do in XSL.

谁能指出我正确的方向?

Can anyone point me in the right direction?

推荐答案

之前的答案对于应该(并且现在)很简单的事情来说太复杂了.这么简单的事情不需要递归.

The previous answers are way to complicated for what should be (and is) easy. Recursion is not required for such a simple thing.

在 XSL FO 中,您不需要用行来构造表.您可以使用属性ends-row"来指定您正在结束一行并开始新的一行.您可以轻松地调整这个简单的示例,甚至可以传入列数"(请参阅​​ mod 5 ...这意味着每五分之一开始一个新行...更改为 4 或 8 或您希望的任何内容)... 您只需在此之外创建表的结构(fo:table 和 fo:table-body).在表格正文中,就像这个模板一样,将单元格作为子项放置:

In XSL FO you do not need to structure tables with rows. You can use the attribute "ends-row" to specify that you are ending a row and starting a new one. You can easily adapt this simple example and even pass in the "number of columns" (see the mod 5 ... which means after every fifth one start a new row ... change to 4 or 8 or whatever you wish) ... You would just create the structure for the table (fo:table and fo:table-body) outside of this. Inside the table body just put cells as children like this template does:

  <xsl:template match="book">
    <xsl:variable name="pos" select="position()"/>
    <fo:table-cell>
        <xsl:if test="not($pos mod 5)">
            <xsl:attribute name="ends-row">true</xsl:attribute>
        </xsl:if>
        <fo:block>
            <xsl:value-of select="author"/>
        </fo:block>
    </fo:table-cell>
</xsl:template>

所以把它放到一个简单的例子中,你的数据......见下文.将您的 XML 格式化为每行五列.

So putting this into a simple example with your data ... see below. Formats your XML into five columns per row.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="1.0">
<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master margin-top="1in" margin-left="1in" margin-bottom="1in"
                margin-right="1in" page-width="8in" page-height="11in" master-name="first">
                <fo:region-body margin-top="0pt"/>
                <fo:region-before extent="0pt"/>
                <fo:region-after extent="0pt"/>
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="first">
            <fo:flow flow-name="xsl-region-body" font-size="12pt" font-family="Helvetica">
    <xsl:apply-templates/>
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
    <xsl:template match="books">
        <fo:table>
            <fo:table-body>
                <xsl:apply-templates/>
            </fo:table-body>
        </fo:table>
    </xsl:template>
    <xsl:template match="book">
        <xsl:variable name="pos" select="position()"/>
        <fo:table-cell border="1pt solid black">
            <xsl:if test="not($pos mod 5)">
                <xsl:attribute name="ends-row">true</xsl:attribute>
            </xsl:if>
            <fo:block>
                <xsl:value-of select="author"/>
            </fo:block>
        </fo:table-cell>
    </xsl:template>

</xsl:stylesheet>

这篇关于XSLT &amp;XSL-FO:创建多行表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆