Muenchian使用XSL-FO表进行分组

时间:2014-06-25 14:18:37

标签: xml xslt xpath xsl-fo muenchian-grouping

我一直在使用RenderX XEP中的XSL-FO生成简单的PDF,但我对XPath中涉及的功能相对较新。

我有一些看起来像这样的XML:

<?xml version="1.0" encoding="UTF-8"?>
<report>
    <ticket>
        <size>A</size>
        <department>Dept 1</department>
        <category>Cat 1</category>
        <product>Product 1</product>
    </ticket>
    <ticket>
        <size>A</size>
        <department>Dept 1</department>
        <category>Cat 2</category>
        <product>Product 2</product>
    </ticket>
    <ticket>
        <size>B</size>
        <department>Dept 1</department>
        <category>Cat 2</category>
        <product>Product 3</product>
    </ticket>
    <ticket>
        <size>B</size>
        <department>Dept 1</department>
        <category>Cat 1</category>
        <product>Product 1</product>
    </ticket>
    <ticket>
        <size>C</size>
        <department>Dept 1</department>
        <category>Cat 2</category>
        <product>Product 2</product>
    </ticket>
    <ticket>
        <size>D</size>
        <department>Dept 3</department>
        <category>Cat 1</category>
        <product>Product 4</product>
    </ticket>
    <ticket>
        <size>D</size>
        <department>Dept 1</department>
        <category>Cat 1</category>
        <product>Product 1</product>
    </ticket>
</report>

我需要能够使用XSLT1.0格式化对象将这些数据排序到PDF中。所需的格式如下:

所有相同尺寸的门票都在单独的表格中(按字母顺序排列在页面上),每张门票都列出了相关类别和部门。 例如:

Desired Output

我已经开始实施Muenchian分组,但是当使用大型XML提取时,错误的类别属于部门之下,而且我还没有能够列出每个类别下面的相关产品。

我可能比较不整洁的XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:key name="keySize" match="ticket" use="size" />
    <xsl:key name="keyDepartment" match="ticket" use="concat(size, ' ', department)" />
    <xsl:key name="keyCategory" match="ticket" use="concat(size, ' ', department, ' ', category)" />

    <xsl:template match="/report">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>
                <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="2cm" margin-bottom="2cm" margin-left="2cm" margin-right="2cm">
                    <fo:region-body />
                </fo:simple-page-master>
            </fo:layout-master-set>
            <fo:page-sequence master-reference="simple">
                <fo:flow flow-name="xsl-region-body">

                    <xsl:for-each select="ticket[count(. | key('keySize', size)[1]) = 1]">
                        <xsl:sort select="size" order="ascending" />
                        <fo:block>
                            Size <xsl:value-of select="size" />
                        </fo:block>
                        <fo:block>
                            <xsl:variable name="sizes" select="key('keySize', size)" />
                            <xsl:for-each select="$sizes[generate-id() = generate-id(key('keyDepartment', concat(size, ' ', department))[1])]">
                                <xsl:sort select="department" order="ascending" />

                                <!--TABLES START-->
                                <fo:table-and-caption>
                                    <fo:table border="0.2mm solid black" width="100%">
                                        <fo:table-header>
                                            <fo:table-row>
                                                <fo:table-cell>
                                                    <fo:block font-weight="bold" margin="1mm 0mm 0mm 1mm">
                                                        <xsl:text>Department</xsl:text>
                                                    </fo:block>
                                                </fo:table-cell>
                                                <fo:table-cell>
                                                    <fo:block font-weight="bold" margin="1mm 0mm 0mm 1mm">
                                                        <xsl:value-of select="department" />
                                                    </fo:block>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-header>
                                        <fo:table-body>
                                            <fo:table-cell>
                                                <fo:block margin="1mm 0mm 0mm 1mm" />
                                            </fo:table-cell>
                                        </fo:table-body>
                                    </fo:table>
                                </fo:table-and-caption>

                                <!--BRINGS IN WRONG CATEGORIES-->
                                <xsl:for-each select="$sizes[generate-id() = generate-id(key('keyCategory', concat(size, ' ', department, ' ', category))[1])]">
                                    <xsl:sort select="category" order="ascending" />
                                    <fo:table-and-caption>
                                        <fo:table border="0.2mm solid black" width="100%">
                                            <fo:table-header>
                                                <fo:table-row>
                                                    <fo:table-cell>
                                                        <fo:block font-weight="bold" margin="1mm 0mm 0mm 1mm">
                                                            <xsl:text>Category</xsl:text>
                                                        </fo:block>
                                                    </fo:table-cell>
                                                    <fo:table-cell>
                                                        <fo:block font-weight="bold" margin="1mm 0mm 0mm 1mm">
                                                            <xsl:value-of select="category" />
                                                        </fo:block>
                                                    </fo:table-cell>
                                                </fo:table-row>
                                            </fo:table-header>
                                            <fo:table-body>
                                                <fo:table-row>
                                                    <fo:table-cell>
                                                        <fo:block margin="1mm 0mm 0mm 1mm"><xsl:value-of select="description" /></fo:block>
                                                    </fo:table-cell>
                                                </fo:table-row>

                                                <!--

                                                DESCRIPTIONS GO HERE?

                                                -->
                                            </fo:table-body>
                                        </fo:table>
                                    </fo:table-and-caption>
                                </xsl:for-each>                         
                            </xsl:for-each>                         
                        </fo:block>
                    </xsl:for-each>
                </fo:flow>
            </fo:page-sequence>
        </fo:root>
    </xsl:template>
</xsl:stylesheet>

任何有关引入产品的帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为问题在于这一行,您尝试迭代某个部门的所有类别

<xsl:for-each select="$sizes[generate-id() = generate-id(key('keyCategory', concat(size, ' ', department, ' ', category))[1])]">

问题是变量尺寸包含不同部门的详细信息,而不仅仅是您感兴趣的部门,导致重复。

请尝试此操作,将其限制为当前部门

<xsl:variable name="depts" select="key('keyDepartment', concat(size, ' ', department))" />
<xsl:for-each select="$depts[generate-id() = generate-id(key('keyCategory', concat(size, ' ', department, ' ', category))[1])]">