如何根据项目的值更改页面顺序,但在第二个页面顺序完成后返回?

How to change page-sequence based on an item's value, but returning after second page-sequence is finished?

我正在尝试设置一个基于 XML 值的样式表,它将更改当前使用的页面序列。 例如,对于下面的 XML:

<items>
    <item order="1">item1</item>
    <item order="2">item2</item>
    <item order="3">item3</item>
    <item order="0">item4</item>
    <item order="4">item5</item>
</items>

预期结果为:

 ....<fo:page-sequence master-reference="A4PageSeq">
      <fo:flow flow-name="body">
         <fo:block>item1</fo:block>
         <fo:block>item2</fo:block>
         <fo:block>item3</fo:block>
      </fo:flow>
   </fo:page-sequence>
<fo:page-sequence master-reference="A3PageSeq">
      <fo:flow flow-name="body">
         <fo:block>item4</fo:block>
      </fo:flow>
   </fo:page-sequence>
<fo:page-sequence master-reference="A4PageSeq">
      <fo:flow flow-name="body">
         <fo:block>item5</fo:block>
      </fo:flow>
   </fo:page-sequence>..

过去,我使用下面的代码,因为@order 可以取“1”或“0”的值。但是,这不再适用于此新输入。我试图使用一个变量,但是它最终将所有内容添加到同一页面序列下。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" version="2.0">
    <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="A4Page" fo:page-width="8.5in" fo:page-height="11in">
                    <fo:region-body region-name="body" margin="0.7in"/>
                </fo:simple-page-master>
                <fo:simple-page-master master-name="A3Page" fo:page-width="11in" fo:page-height="17in">
                    <fo:region-body region-name="body" margin="0.7in"/>
                </fo:simple-page-master>
                <fo:page-sequence-master master-name="A4PageSeq">
                    <fo:repeatable-page-master-reference master-reference="A4Page"/>
                </fo:page-sequence-master>
                <fo:page-sequence-master master-name="A3PageSeq">
                    <fo:repeatable-page-master-reference master-reference="A3Page"/>
                </fo:page-sequence-master>
            </fo:layout-master-set>
            <xsl:apply-templates/>
        </fo:root>
    </xsl:template>
    
    
    <xsl:variable name="groupKey">
            <xsl:choose>
                <xsl:when test="/items/item/@order != 0">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
    </xsl:variable>
    
    
    <xsl:template match="items">

        <xsl:for-each-group select="item" group-adjacent="@order">
            <xsl:choose>
                <xsl:when test="$groupKey != 0">
                    <fo:page-sequence master-reference="A4PageSeq">
                        <fo:flow flow-name="body">
                            <xsl:for-each select="current-group()">
                                <xsl:apply-templates select="."/>
                                <fo:block>This is A4 page seq</fo:block>
                            </xsl:for-each>
                        </fo:flow>
                    </fo:page-sequence>
                </xsl:when>
                <xsl:otherwise>
                    <fo:page-sequence master-reference="A3PageSeq">
                        <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>
            <xsl:apply-templates/>
        </fo:block>
    </xsl:template>
</xsl:stylesheet>

如何在 @order 属性后进行分组,以便每次 @order=0 时页面序列都更新?

我不确定我是否理解其中的逻辑,但它似乎使用了

<xsl:for-each-group select="item" group-adjacent="@order != 0">
  <xsl:choose>
    <xsl:when test="current-grouping-key()">
                    <fo:page-sequence master-reference="A4PageSeq">
                        <fo:flow flow-name="body">
                            <xsl:apply-templates select="current-group()"/>
                        </fo:flow>
                    </fo:page-sequence>
   </xsl:when>
   <xsl:otherwise>
     <fo:page-sequence master-reference="A3PageSeq">
      <fo:flow flow-name="body">
         <xsl:apply-templates select="current-group()"/>
      </fo:flow>
     </fo:page-sequence>
   </xsl:otherwise>
 </xsl:choose>
</xsl:for-each-group>

可能会。如果可以有相邻的 @order=0 项目并且每个项目都需要自己的序列,那么在 xsl:otherwise 内部然后在内部使用 for-each 或将 current-group() 推送到设置序列的模板.

您可以删除 <xsl:variable name="groupKey"> 变量,然后将主模板更改为

<xsl:template match="items">
     <xsl:for-each-group select="item" group-adjacent="@order != 0"> 
        <xsl:choose>
            <xsl:when test="@order != 0">
                <fo:page-sequence master-reference="A4PageSeq">
                    <fo:flow flow-name="body">
                        <xsl:for-each select="current-group()">
                            <xsl:apply-templates select="."/>
                            <!-- <fo:block>This is A4 page seq</fo:block> -->
                        </xsl:for-each>
                    </fo:flow>
                </fo:page-sequence>
            </xsl:when>
            <xsl:otherwise>
                <fo:page-sequence master-reference="A3PageSeq">
                    <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>

现在,结果应该符合预期。