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 9168275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:35:26+00:00 2026-06-17T15:35:26+00:00

I am fairly new to XSL and am looking to optimize my code. What

  • 0

I am fairly new to XSL and am looking to optimize my code. What I need to do is vary the output of my XSL if the item count is 3 or more than 3. If there are 3 items, each <div/> should have only 1 <a/> element. If there are 6 the <div/> should contain 2 <a/> elements.
The code I have below works, but I know there has to be a better way.

If 3 items it looks like this

<div>
   <div>
      <a href="/link1.html">
         <div>
            <h1>Header 1</h1>
            <p>Body coy 1</p>
         </div>
      </a>
   </div>
   <div>
      <a href="/link2.html">
         <div>
            <h1>Header 2</h1>
            <p>Body coy 2</p>
         </div>
      </a>
   </div>
   <div>
      <a href="/link3.html">
         <div>
            <h1>Header 3</h1>
            <p>Body coy 3</p>
         </div>
      </a>
   </div>
</div>

If there are 6 items it should like this:

<div>
   <div>
      <a href="/link1.html">
         <div>
            <h1>Header 1</h1>
            <p>Body coy 1</p>
         </div>
      </a>
      <a href="/link2.html">
         <div>
            <h1>Header 2</h1>
            <p>Body coy 2</p>
         </div>
      </a>
   </div>
   <div>
      <a href="/link3.html">
         <div>
            <h1>Header 3</h1>
            <p>Body coy 3</p>
         </div>
      </a>
      <a href="/link4.html">
         <div>
            <h1>Header 4</h1>
            <p>Body coy 4</p>
         </div>
      </a>
   </div>
   <div>
      <a href="/link5.html">
         <div>
            <h1>Header 5</h1>
            <p>Body coy 5</p>
         </div>
      </a>
      <a href="/link6.html">
         <div>
            <h1>Header 6</h1>
            <p>Body coy 6</p>
         </div>
      </a>
   </div>
</div>

XML (6 items):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <article>
        <header>Header 1</header>
        <body>Body coy 1</body>
        <url><a href="/link1.html" title="Link1">Link 1</a></url>
    </article>
    <article>
        <header>Header 2</header>
        <body>Body coy 2</body>
        <url><a href="/link2.html" title="Link2">Link 2</a></url>
    </article>
    <article>
        <header>Header 3</header>
        <body>Body coy 3</body>
        <url><a href="/link3.html" title="Link1">Link 3</a></url>
    </article>
    <article>
        <header>Header 4</header>
        <body>Body coy 4</body>
        <url><a href="/link4.html" title="Link4">Link 4</a></url>
    </article>
    <article>
        <header>Header 5</header>
        <body>Body coy 5</body>
        <url><a href="/link5.html" title="Link1">Link 5</a></url>
    </article>
    <article>
        <header>Header 6</header>
        <body>Body coy 6</body>
        <url><a href="/link6.html" title="Link1">Link 6</a></url>
    </article>
</root>

XML (3 items):

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <article>
        <header>Header 1</header>
        <body>Body coy 1</body>
        <url><a href="/link1.html" title="Link1">Link 1</a></url>
    </article>
    <article>
        <header>Header 2</header>
        <body>Body coy 2</body>
        <url><a href="/link2.html" title="Link2">Link 2</a></url>
    </article>
    <article>
        <header>Header 3</header>
        <body>Body coy 3</body>
        <url><a href="/link3.html" title="Link1">Link 3</a></url>
    </article>
</root>

XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!--  the number of items to include in each group -->

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

    <xsl:template match="/root">
        <div>
            <xsl:variable name="articleCount" select="count(article)"/>
            <xsl:variable name="group" select="$articleCount div 3" />

            <xsl:choose>
                <xsl:when test="$articleCount = 3">


                    <xsl:for-each select="article">
                        <div>
                            <xsl:call-template name="articleItem">
                                <xsl:with-param name="item" select="."/>
                            </xsl:call-template>
                        </div>
                    </xsl:for-each>

                </xsl:when>
                <xsl:otherwise>
                    <xsl:apply-templates
                        select="article[position() mod 2 = 1]" >
                        <xsl:with-param name="group" select="2"/>
                    </xsl:apply-templates>
                </xsl:otherwise>
            </xsl:choose>
        </div>
    </xsl:template>

    <xsl:template match="article" mode="inner">
        <xsl:param name="group"/>
        <!-- handle items appropriately here -->

        <xsl:call-template name="articleItem">
            <xsl:with-param name="item" select="."/>
        </xsl:call-template>
    </xsl:template>

    <xsl:template match="article">
        <xsl:param name="group"/>
        <div class="article">
            <xsl:apply-templates
                select=".|following-sibling::article[$group &gt; position()]" 
                mode="inner" />
        </div>
    </xsl:template>

    <xsl:template name="articleItem">
        <xsl:param name="item"/>
        <a>
            <xsl:attribute name="href"><xsl:value-of select="url/a/@href" /></xsl:attribute>
            <xsl:call-template name="articleContent">
                <xsl:with-param name="item" select="."/>
            </xsl:call-template>
        </a>
    </xsl:template>

    <xsl:template name="articleContent">
        <xsl:param name="item"/>
        <div class="article-copy">
            <h1><xsl:value-of disable-output-escaping="yes" select="header" /></h1>
            <p><xsl:value-of disable-output-escaping="yes" select="body" /></p>
        </div>
    </xsl:template>
</xsl:stylesheet>
  • 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-06-17T15:35:27+00:00Added an answer on June 17, 2026 at 3:35 pm

    This looks like it works. Could you give it a try?

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <!--  the number of items to include in each group -->
    
      <xsl:output omit-xml-declaration="yes" indent="yes"/>
      <xsl:strip-space elements="*"/>
    
      <xsl:template match="/root">
        <div>
          <xsl:variable name="sideBySide" select="count(article) > 3" />
    
          <!-- If not($sideBySide), loop through the articles 1 at a time
               If $sideBySide, loop through articles in odd-numbered positions -->
          <xsl:for-each select="article[(position() - 1) mod (1 + $sideBySide) = 0]">
            <xsl:call-template name="articleRow">
              <!-- Pass the current article, AND its first sibling if $sideBySide -->
              <xsl:with-param name="articles" select=". | following-sibling::article[1][$sideBySide]" />
            </xsl:call-template>
          </xsl:for-each>
        </div>
      </xsl:template>
    
      <xsl:template name="articleRow">
        <xsl:param name="articles" />
        <div>
          <xsl:apply-templates select="$articles" />
        </div>
      </xsl:template>
    
      <xsl:template match="article">
        <a href="{url/a/@href}">
          <div class="article-copy">
            <h1>
              <xsl:value-of disable-output-escaping="yes" select="header" />
            </h1>
            <p>
              <xsl:value-of disable-output-escaping="yes" select="body" />
            </p>
          </div>
        </a>
      </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am fairly new to XSL and need help with a transformation issue. I
I fairly new to regular expressions and need some help. I need to filter
Fairly new to CR and i am looking for some help, Basically I want
Im fairly new to developing in Drupal, i am using the 7.x code. I
Fairly new to .Net here and having trouble with a line of code. I
I'm fairly new to XML and XSL Stylesheets, and I've been tasked with creating
still fairly new to matlab, picked up this data analysis code from someone and
Hi fairly new to using MVC radio buttons. I have some code: @Html.RadioButtonFor(modelItem =>
Fairly new to asp.net Mvc and jquery. Have the following code working fine, but
Fairly new to programming. I just can't wrap my head around how to get

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.