Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6200191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:19:23+00:00 2026-05-24T04:19:23+00:00

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

  • 0

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).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:19:24+00:00Added an answer on May 24, 2026 at 4:19 am

    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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a complex SQL query that joins multiple tables. Here's a watered down
I'd like to do something like this, where based on some condition I decide
I am basically producing a simple, watered down, iPhone specific sites on a sub-domain
I have a very simple project. Extremely watered down. All it does is load
If I have a model that looks like this: and I do a Linq
I've spent the last 2 days trying to figure this out, basically I have
I'd like to open an OpenGL context without X in Linux. Is there any
The issue I'm currently having is mapping multiple GUI fields to object properties (i.e.
I am having trouble with permissions on Windows 2008 server. i have a command
If you'll take a look at this: http://jsfiddle.net/hunterscott/JYLVQ/ You'll notice that if you try

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.