如何根据节点属性动态更改xslt页面顺序?

发布于 2024-11-26 14:45:42 字数 4416 浏览 1 评论 0原文

我遇到的问题的淡化版本是这样的。对于像这样的 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item cols="1">Item 1</item>
    <item cols="1">Item 2</item>
    <item cols="1">Item 3</item>
    <item cols="1">Item 4</item>
    <item cols="1">Item 5</item>
    <item cols="1">Item 6</item>
    <item cols="1">Item 7</item>
    <item cols="1">Item 8</item>
    <item cols="1">Item 9</item>
    <item cols="2">Item 10</item>
    <item cols="1">Item 11</item>
    <item cols="1">Item 12</item>
    <item cols="1">Item 13</item>
    <item cols="1">Item 14</item>
    <item cols="1">Item 15</item>
    <item cols="1">Item 16</item>
    <item cols="1">Item 17</item>
    <item cols="1">Item 18</item>
</items>

我需要能够在单列页面布局中打印具有“cols=1”的“项目”,以及在双列页面布局中打印具有“cols=2”的“项目”。必须保留项目的顺序。所有具有相同 @cols 值的连续“项目”都需要显示为连续流。每当 @cols 值发生变化时,我都需要切换到新页面并根据需要更改布局。

我正在做这样的事情:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>

                <fo:simple-page-master master-name="one-column-page-master">
                    <fo:region-body margin-top="3cm" region-name="body" column-count="1"/>
                </fo:simple-page-master>

                <fo:simple-page-master master-name="two-column-page-master">
                    <fo:region-body margin-top="3cm" region-name="body" column-count="2"/>
                    <fo:region-before region-name="header" extent="2cm"/>
                </fo:simple-page-master>

                <fo:page-sequence-master master-name="one-column-page">
                    <fo:repeatable-page-master-reference master-reference="one-column-page-master"/>
                </fo:page-sequence-master>

                <fo:page-sequence-master master-name="two-column-page">
                    <fo:repeatable-page-master-reference master-reference="two-column-page-master"/>
                </fo:page-sequence-master>

            </fo:layout-master-set>

            <xsl:for-each select="//item">
                <xsl:choose>
                    <xsl:when test="@cols = preceding-sibling::item[1]/@cols">
                        <!--cols value hasn't changed, don't create a new page-sequence-->
                        <!--But we cannot directly add fo:flow as the child of fo:root! -->
                        <xsl:call-template name="itemtemplate"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="@cols = 1">
                                <fo:page-sequence master-reference="one-column-page">
                                    <xsl:call-template name="itemtemplate"/>
                                </fo:page-sequence>
                            </xsl:when>
                            <xsl:otherwise>
                                <fo:page-sequence master-reference="two-column-page">
                                    <xsl:call-template name="itemtemplate"/>
                                </fo:page-sequence>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </fo:root>

    </xsl:template>

    <xsl:template name="itemtemplate">
            <fo:flow flow-name="body">
                <fo:block margin-bottom="5cm">
                    <xsl:apply-templates/>
                </fo:block>
            </fo:flow>

    </xsl:template>

</xsl:stylesheet>

但是当然,问题是我要么必须包含无论是否在我的样式表中,我都无法“动态”决定根据注释属性放入样式表。 (除非我有一个元程序可以首先动态创建样式表,但我希望仅使用普通的静态样式表来完成此操作)。

The watered-down version of the problem I'm having is this. For an XML file like:

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item cols="1">Item 1</item>
    <item cols="1">Item 2</item>
    <item cols="1">Item 3</item>
    <item cols="1">Item 4</item>
    <item cols="1">Item 5</item>
    <item cols="1">Item 6</item>
    <item cols="1">Item 7</item>
    <item cols="1">Item 8</item>
    <item cols="1">Item 9</item>
    <item cols="2">Item 10</item>
    <item cols="1">Item 11</item>
    <item cols="1">Item 12</item>
    <item cols="1">Item 13</item>
    <item cols="1">Item 14</item>
    <item cols="1">Item 15</item>
    <item cols="1">Item 16</item>
    <item cols="1">Item 17</item>
    <item cols="1">Item 18</item>
</items>

I need to be able to print the 'item's that have 'cols=1' in a single column page layout, and the 'item's that have 'cols=2' in a double column page layout. The ordering of the items has to be preserved. All contiguous 'item's with the same value of @cols needs to appear as a continuous flow. Any time the @cols value changes, I need to break to a new page and change the layout as necessary.

I'm doing something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:strip-space elements="*"/>

    <xsl:template match="/">
        <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
            <fo:layout-master-set>

                <fo:simple-page-master master-name="one-column-page-master">
                    <fo:region-body margin-top="3cm" region-name="body" column-count="1"/>
                </fo:simple-page-master>

                <fo:simple-page-master master-name="two-column-page-master">
                    <fo:region-body margin-top="3cm" region-name="body" column-count="2"/>
                    <fo:region-before region-name="header" extent="2cm"/>
                </fo:simple-page-master>

                <fo:page-sequence-master master-name="one-column-page">
                    <fo:repeatable-page-master-reference master-reference="one-column-page-master"/>
                </fo:page-sequence-master>

                <fo:page-sequence-master master-name="two-column-page">
                    <fo:repeatable-page-master-reference master-reference="two-column-page-master"/>
                </fo:page-sequence-master>

            </fo:layout-master-set>

            <xsl:for-each select="//item">
                <xsl:choose>
                    <xsl:when test="@cols = preceding-sibling::item[1]/@cols">
                        <!--cols value hasn't changed, don't create a new page-sequence-->
                        <!--But we cannot directly add fo:flow as the child of fo:root! -->
                        <xsl:call-template name="itemtemplate"/>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:choose>
                            <xsl:when test="@cols = 1">
                                <fo:page-sequence master-reference="one-column-page">
                                    <xsl:call-template name="itemtemplate"/>
                                </fo:page-sequence>
                            </xsl:when>
                            <xsl:otherwise>
                                <fo:page-sequence master-reference="two-column-page">
                                    <xsl:call-template name="itemtemplate"/>
                                </fo:page-sequence>
                            </xsl:otherwise>
                        </xsl:choose>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </fo:root>

    </xsl:template>

    <xsl:template name="itemtemplate">
            <fo:flow flow-name="body">
                <fo:block margin-bottom="5cm">
                    <xsl:apply-templates/>
                </fo:block>
            </fo:flow>

    </xsl:template>

</xsl:stylesheet>

But of course, the problem is that I either have to include a <fo:page-sequence..> in my stylesheet, or not, I cannot 'dynamically' decide to put in one based on note attributes. (Unless I have a meta program that creates the stylesheet dynamically in the first place, but I was hoping to accomplish this using just plain static stylesheets).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

咽泪装欢 2024-12-03 14:45:42

这是一个使用 xsl:for-each 的 XSLT 2.0 解决方案-groupgroup-adjacent

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="/">
      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>

          <fo:simple-page-master master-name="one-column-page-master">
            <fo:region-body margin-top="3cm" region-name="body" 
                            column-count="1"/>
          </fo:simple-page-master>

          <fo:simple-page-master master-name="two-column-page-master">
            <fo:region-body margin-top="3cm" region-name="body" 
                            column-count="2"/>
            <fo:region-before region-name="header" extent="2cm"/>
          </fo:simple-page-master>

          <fo:page-sequence-master master-name="one-column-page">
            <fo:repeatable-page-master-reference 
                master-reference="one-column-page-master"/>
          </fo:page-sequence-master>

          <fo:page-sequence-master master-name="two-column-page">
            <fo:repeatable-page-master-reference 
                master-reference="two-column-page-master"/>
          </fo:page-sequence-master>

        </fo:layout-master-set>
        <xsl:apply-templates/>
      </fo:root>
    </xsl:template>

    <xsl:template match="items">
      <xsl:for-each-group select="item" 
                          group-adjacent="@cols">

        <xsl:choose>
          <xsl:when test="@cols = 1">
            <fo:page-sequence master-reference="one-column-page">
              <fo:flow flow-name="body">
                <xsl:for-each select="current-group()">
                  <xsl:apply-templates select="."/>
                </xsl:for-each>
              </fo:flow>
            </fo:page-sequence>
          </xsl:when>

          <xsl:otherwise>
            <fo:page-sequence master-reference="two-column-page">
              <fo:flow flow-name="body">
                <xsl:for-each select="current-group()">
                  <xsl:apply-templates select="."/>
                </xsl:for-each>
              </fo:flow>
            </fo:page-sequence>
          </xsl:otherwise>

        </xsl:choose>
      </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="item">
      <fo:block margin-bottom="5cm">
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="one-column-page-master">
         <fo:region-body margin-top="3cm" region-name="body" column-count="1"/>
      </fo:simple-page-master>
      <fo:simple-page-master master-name="two-column-page-master">
         <fo:region-body margin-top="3cm" region-name="body" column-count="2"/>
         <fo:region-before region-name="header" extent="2cm"/>
      </fo:simple-page-master>
      <fo:page-sequence-master master-name="one-column-page">
         <fo:repeatable-page-master-reference master-reference="one-column-page-master"/>
      </fo:page-sequence-master>
      <fo:page-sequence-master master-name="two-column-page">
         <fo:repeatable-page-master-reference master-reference="two-column-page-master"/>
      </fo:page-sequence-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="one-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 1</fo:block>
         <fo:block margin-bottom="5cm">Item 2</fo:block>
         <fo:block margin-bottom="5cm">Item 3</fo:block>
         <fo:block margin-bottom="5cm">Item 4</fo:block>
         <fo:block margin-bottom="5cm">Item 5</fo:block>
         <fo:block margin-bottom="5cm">Item 6</fo:block>
         <fo:block margin-bottom="5cm">Item 7</fo:block>
         <fo:block margin-bottom="5cm">Item 8</fo:block>
         <fo:block margin-bottom="5cm">Item 9</fo:block>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="two-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 10</fo:block>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="one-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 11</fo:block>
         <fo:block margin-bottom="5cm">Item 12</fo:block>
         <fo:block margin-bottom="5cm">Item 13</fo:block>
         <fo:block margin-bottom="5cm">Item 14</fo:block>
         <fo:block margin-bottom="5cm">Item 15</fo:block>
         <fo:block margin-bottom="5cm">Item 16</fo:block>
         <fo:block margin-bottom="5cm">Item 17</fo:block>
         <fo:block margin-bottom="5cm">Item 18</fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>

Here is an XSLT 2.0 solution that uses xsl:for-each-group with group-adjacent:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fo="http://www.w3.org/1999/XSL/Format">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="/">
      <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>

          <fo:simple-page-master master-name="one-column-page-master">
            <fo:region-body margin-top="3cm" region-name="body" 
                            column-count="1"/>
          </fo:simple-page-master>

          <fo:simple-page-master master-name="two-column-page-master">
            <fo:region-body margin-top="3cm" region-name="body" 
                            column-count="2"/>
            <fo:region-before region-name="header" extent="2cm"/>
          </fo:simple-page-master>

          <fo:page-sequence-master master-name="one-column-page">
            <fo:repeatable-page-master-reference 
                master-reference="one-column-page-master"/>
          </fo:page-sequence-master>

          <fo:page-sequence-master master-name="two-column-page">
            <fo:repeatable-page-master-reference 
                master-reference="two-column-page-master"/>
          </fo:page-sequence-master>

        </fo:layout-master-set>
        <xsl:apply-templates/>
      </fo:root>
    </xsl:template>

    <xsl:template match="items">
      <xsl:for-each-group select="item" 
                          group-adjacent="@cols">

        <xsl:choose>
          <xsl:when test="@cols = 1">
            <fo:page-sequence master-reference="one-column-page">
              <fo:flow flow-name="body">
                <xsl:for-each select="current-group()">
                  <xsl:apply-templates select="."/>
                </xsl:for-each>
              </fo:flow>
            </fo:page-sequence>
          </xsl:when>

          <xsl:otherwise>
            <fo:page-sequence master-reference="two-column-page">
              <fo:flow flow-name="body">
                <xsl:for-each select="current-group()">
                  <xsl:apply-templates select="."/>
                </xsl:for-each>
              </fo:flow>
            </fo:page-sequence>
          </xsl:otherwise>

        </xsl:choose>
      </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="item">
      <fo:block margin-bottom="5cm">
        <xsl:apply-templates/>
      </fo:block>
    </xsl:template>

</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:layout-master-set>
      <fo:simple-page-master master-name="one-column-page-master">
         <fo:region-body margin-top="3cm" region-name="body" column-count="1"/>
      </fo:simple-page-master>
      <fo:simple-page-master master-name="two-column-page-master">
         <fo:region-body margin-top="3cm" region-name="body" column-count="2"/>
         <fo:region-before region-name="header" extent="2cm"/>
      </fo:simple-page-master>
      <fo:page-sequence-master master-name="one-column-page">
         <fo:repeatable-page-master-reference master-reference="one-column-page-master"/>
      </fo:page-sequence-master>
      <fo:page-sequence-master master-name="two-column-page">
         <fo:repeatable-page-master-reference master-reference="two-column-page-master"/>
      </fo:page-sequence-master>
   </fo:layout-master-set>
   <fo:page-sequence master-reference="one-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 1</fo:block>
         <fo:block margin-bottom="5cm">Item 2</fo:block>
         <fo:block margin-bottom="5cm">Item 3</fo:block>
         <fo:block margin-bottom="5cm">Item 4</fo:block>
         <fo:block margin-bottom="5cm">Item 5</fo:block>
         <fo:block margin-bottom="5cm">Item 6</fo:block>
         <fo:block margin-bottom="5cm">Item 7</fo:block>
         <fo:block margin-bottom="5cm">Item 8</fo:block>
         <fo:block margin-bottom="5cm">Item 9</fo:block>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="two-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 10</fo:block>
      </fo:flow>
   </fo:page-sequence>
   <fo:page-sequence master-reference="one-column-page">
      <fo:flow flow-name="body">
         <fo:block margin-bottom="5cm">Item 11</fo:block>
         <fo:block margin-bottom="5cm">Item 12</fo:block>
         <fo:block margin-bottom="5cm">Item 13</fo:block>
         <fo:block margin-bottom="5cm">Item 14</fo:block>
         <fo:block margin-bottom="5cm">Item 15</fo:block>
         <fo:block margin-bottom="5cm">Item 16</fo:block>
         <fo:block margin-bottom="5cm">Item 17</fo:block>
         <fo:block margin-bottom="5cm">Item 18</fo:block>
      </fo:flow>
   </fo:page-sequence>
</fo:root>
路还长,别太狂 2024-12-03 14:45:42

我需要能够在单列页面布局中打印具有“cols=1”的“项目”,以及在双列页面布局中打印具有“cols=2”的“项目” 。必须保留项目的顺序。

您最终希望根据正确页面中的@cols 值对相邻的item 元素进行分组顺序。

XSLT 1.0 指令(例如 xsl:choosexsl:for-each)并不真正适合此任务。我认为你必须改变一下主意。这里举例说明如何通过递归实现结果分组。

不太清楚您想要在每个流程元素中包含什么,然后我决定向您展示如何对元素进行分组;然后,您可以根据您的要求调整代码。


[XSLT 1.0]

<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 indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="text()"/>

    <xsl:template match="/*">
        <fo:root>
            <!-- layout master stuff -->
            <xsl:apply-templates select="item"/>
        </fo:root>
    </xsl:template>

    <!-- match @cols 1, first group occurrences -->  
    <xsl:template match="/*/item[@cols=1]
     [not(preceding-sibling::item[1][@cols=1])]">
        <fo:page-sequence master-reference="one-column-page">
            <xsl:copy-of select="."/>
            <xsl:apply-templates select="
                following-sibling::*[1][self::item[@cols=1]]" mode="flow">
                <xsl:with-param name="cols" select="1"/>
            </xsl:apply-templates>
        </fo:page-sequence>
    </xsl:template>

    <!-- match @cols 2, first group occurrences -->
    <xsl:template match="/*/item[@cols=2]
     [not(preceding-sibling::item[1][@cols=2])]">
        <fo:page-sequence master-reference="two-column-page">
            <xsl:copy-of select="."/>
            <xsl:apply-templates select="
                following-sibling::*[1][self::item[@cols=2]]" mode="flow">
                <xsl:with-param name="cols" select="2"/>
            </xsl:apply-templates>
        </fo:page-sequence>
    </xsl:template>

    <!-- recursive match adjacent @cols -->
    <xsl:template match="item" mode="flow">
        <xsl:param name="cols"/>
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="
            following-sibling::*[1][self::item[@cols=$cols]]" mode="flow">
            <xsl:with-param name="cols" select="$cols"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

当应用于问题中提供的示例输入时,会产生:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:page-sequence master-reference="one-column-page">
      <item cols="1">Item 1</item>
      <item cols="1">Item 2</item>
      <item cols="1">Item 3</item>
      <item cols="1">Item 4</item>
      <item cols="1">Item 5</item>
      <item cols="1">Item 6</item>
      <item cols="1">Item 7</item>
      <item cols="1">Item 8</item>
      <item cols="1">Item 9</item>
   </fo:page-sequence>
   <fo:page-sequence master-reference="two-column-page">
      <item cols="2">Item 10</item>
   </fo:page-sequence>
   <fo:page-sequence master-reference="one-column-page">
      <item cols="1">Item 11</item>
      <item cols="1">Item 12</item>
      <item cols="1">Item 13</item>
      <item cols="1">Item 14</item>
      <item cols="1">Item 15</item>
      <item cols="1">Item 16</item>
      <item cols="1">Item 17</item>
      <item cols="1">Item 18</item>
   </fo:page-sequence>
</fo:root>

I need to be able to print the 'item's that have 'cols=1' in a single column page layout, and the 'item's that have 'cols=2' in a double column page layout. The ordering of the items has to be preserved.

You want finally group adjacent item elements according the value of @cols in the proper fo page sequence.

XSLT 1.0 instructions like xsl:choose and xsl:for-each are not really suitable to this task. I think you have to change your mind a bit. Here an example of how to achieve the result grouping by recursion.

It's not really clear what you want to include inside each flow element, then I decided to show you just how to group elements; then, you can adapt the code to your requirements.


[XSLT 1.0]

<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 indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="text()"/>

    <xsl:template match="/*">
        <fo:root>
            <!-- layout master stuff -->
            <xsl:apply-templates select="item"/>
        </fo:root>
    </xsl:template>

    <!-- match @cols 1, first group occurrences -->  
    <xsl:template match="/*/item[@cols=1]
     [not(preceding-sibling::item[1][@cols=1])]">
        <fo:page-sequence master-reference="one-column-page">
            <xsl:copy-of select="."/>
            <xsl:apply-templates select="
                following-sibling::*[1][self::item[@cols=1]]" mode="flow">
                <xsl:with-param name="cols" select="1"/>
            </xsl:apply-templates>
        </fo:page-sequence>
    </xsl:template>

    <!-- match @cols 2, first group occurrences -->
    <xsl:template match="/*/item[@cols=2]
     [not(preceding-sibling::item[1][@cols=2])]">
        <fo:page-sequence master-reference="two-column-page">
            <xsl:copy-of select="."/>
            <xsl:apply-templates select="
                following-sibling::*[1][self::item[@cols=2]]" mode="flow">
                <xsl:with-param name="cols" select="2"/>
            </xsl:apply-templates>
        </fo:page-sequence>
    </xsl:template>

    <!-- recursive match adjacent @cols -->
    <xsl:template match="item" mode="flow">
        <xsl:param name="cols"/>
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="
            following-sibling::*[1][self::item[@cols=$cols]]" mode="flow">
            <xsl:with-param name="cols" select="$cols"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

When applied to the sample input provided in the question produces:

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
   <fo:page-sequence master-reference="one-column-page">
      <item cols="1">Item 1</item>
      <item cols="1">Item 2</item>
      <item cols="1">Item 3</item>
      <item cols="1">Item 4</item>
      <item cols="1">Item 5</item>
      <item cols="1">Item 6</item>
      <item cols="1">Item 7</item>
      <item cols="1">Item 8</item>
      <item cols="1">Item 9</item>
   </fo:page-sequence>
   <fo:page-sequence master-reference="two-column-page">
      <item cols="2">Item 10</item>
   </fo:page-sequence>
   <fo:page-sequence master-reference="one-column-page">
      <item cols="1">Item 11</item>
      <item cols="1">Item 12</item>
      <item cols="1">Item 13</item>
      <item cols="1">Item 14</item>
      <item cols="1">Item 15</item>
      <item cols="1">Item 16</item>
      <item cols="1">Item 17</item>
      <item cols="1">Item 18</item>
   </fo:page-sequence>
</fo:root>
小情绪 2024-12-03 14:45:42

@empo:太棒了!因此,基本方法是在主循环中处理关键“项目”(其中 @cols 发生变化),并在递归模板调用中处理这些项目的相邻节点。我使用了你的方法并做了一些更改以使代码更简单,但这非常有效!

<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 indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="text()"/>

    <xsl:template match="/*">
        <fo:root>

            <xsl:for-each select="item">
                <xsl:choose>
                    <xsl:when test="preceding-sibling::item[1]/@cols != @cols or position()=1">
                        <xsl:choose>
                            <xsl:when test="@cols = 1">
                                <fo:page-sequence master-reference="one-column-page">
                                    <xsl:apply-templates select="." mode="recurse">
                                        <xsl:with-param name="cols" select="@cols"/>
                                    </xsl:apply-templates>
                                </fo:page-sequence>

                            </xsl:when>
                            <xsl:when test="@cols = 2">
                                <fo:page-sequence master-reference="two-column-page">
                                    <xsl:apply-templates select="." mode="recurse">
                                        <xsl:with-param name="cols" select="@cols"/>
                                    </xsl:apply-templates>
                                </fo:page-sequence>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </fo:root>
    </xsl:template>


    <!-- recursive match adjacent @cols -->
    <xsl:template match="item" mode="recurse">
        <xsl:param name="cols"/>
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="
            following-sibling::*[1][self::item[@cols=$cols]]"
            mode="recurse">
            <xsl:with-param name="cols" select="$cols"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>

@empo: Excellent! So the basic approach is to handle the critical 'item's (where @cols changes) in the main loop, and handle the adjacent nodes to these in the recursive template call. I've used your approach and made a few changes to make the code simpler, but this works great!

<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 indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="text()"/>

    <xsl:template match="/*">
        <fo:root>

            <xsl:for-each select="item">
                <xsl:choose>
                    <xsl:when test="preceding-sibling::item[1]/@cols != @cols or position()=1">
                        <xsl:choose>
                            <xsl:when test="@cols = 1">
                                <fo:page-sequence master-reference="one-column-page">
                                    <xsl:apply-templates select="." mode="recurse">
                                        <xsl:with-param name="cols" select="@cols"/>
                                    </xsl:apply-templates>
                                </fo:page-sequence>

                            </xsl:when>
                            <xsl:when test="@cols = 2">
                                <fo:page-sequence master-reference="two-column-page">
                                    <xsl:apply-templates select="." mode="recurse">
                                        <xsl:with-param name="cols" select="@cols"/>
                                    </xsl:apply-templates>
                                </fo:page-sequence>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:when>
                </xsl:choose>
            </xsl:for-each>
        </fo:root>
    </xsl:template>


    <!-- recursive match adjacent @cols -->
    <xsl:template match="item" mode="recurse">
        <xsl:param name="cols"/>
        <xsl:copy-of select="."/>
        <xsl:apply-templates select="
            following-sibling::*[1][self::item[@cols=$cols]]"
            mode="recurse">
            <xsl:with-param name="cols" select="$cols"/>
        </xsl:apply-templates>
    </xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文