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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:31:33+00:00 2026-05-29T20:31:33+00:00

I’ve come up against an XSLT conundrum – you can’t open an element in

  • 0

I’ve come up against an XSLT conundrum – you can’t open an element in one conditional statement and close it in another. I see apparently related questions about this elsewhere on Stackoverflow, but the answers are a bit baffling to an XSLT beginner of low brain wattage.

Basically I’m trying to display items from my XML in columns across the page. I’m just trying to do 2 columns at the moment, though I’d like a solution in which the number of columns isn’t hardcoded.

My XML data’s like this, with about 100 nodes:

   <?xml version="1.0" encoding="UTF-8"?>
    <response>
        <node type="category">
            <collection>
                <node>
                    <articleId>1</articleId>
                    <headline>Merry Christmas says Google as it unveils animated Jingle Bells Doodle</headline>
                </node>
                <node>
                    <articleId>2</articleId>
                    <headline>Google activating 700,000 Android phones every day</headline>
                </node>
                <node>
                    <articleId>3</articleId>
                    <headline>Google attacked by music industry over 'broken pledges' on illegal downloading</headline>
                </node>
            </collection>
        </node>
    </response>

I’d like to translate this into something like:

    <div>
        <div class="left">
            [ the articleId ]
            [ the headline ]
        </div>
        <div class="right">
            [ the articleId ]
            [ the headline ]
        </div>
    </div>

with article 1 on the left, article 2 on the right, article 3 in the next row on the left, etc. etc.

We’ve tried XSLT like this

<xsl:for-each select="$collection/spi:node[(position() mod $columns) != 0]">
<xsl:variable name="pos" select="position()"/>
<xsl:variable name="node" select="."/>
<div>
    <div class="left">
        <xsl:value-of select="../spi:node[$pos]/spi:articleId"/>]
        <xsl:value-of select="../spi:node[$pos]/spi:headline"/>
    </div>
    <div class="right">
        <xsl:value-of select="../spi:node[$pos + 1]/spi:articleId"/>
        <xsl:value-of select="../spi:node[$pos + 1]/spi:headline"/>
    </div>
</div>
</xsl:for-each>

But this only results in empty divs and weird repetitions of articles. Can any XSLT guru point us in the right direction?

Cheers

  • 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-29T20:31:39+00:00Added an answer on May 29, 2026 at 8:31 pm

    If you were to write out the value of your $pos variable, you will find it goes 1, 2, 3… etc, and not 1, 3,… which is what you may be expecting. This is why you are getting the repetition, I think.

    In fact, there is no need to look for the node using the $pos variable, because you will already be positioned on the first node in the pair each time, so all you need to do is something like this

      <xsl:for-each select="$collection/spi:node[(position() mod $columns) != 0]">
         <div>
            <div class="left">
               <xsl:value-of select="articleId"/>
               <xsl:value-of select="headline"/>
            </div>
            <div class="right">
               <xsl:value-of select="following-sibling::spi:node[1]/articleId"/>
               <xsl:value-of select="following-sibling::spi:node[1]/headline"/>
            </div>
         </div>
      </xsl:for-each>
    

    Do note, it is usually best practise to use xsl:apply-templates, rather than xsl:for-each, so you could re-write it like this:

    <xsl:template match="/">
       <xsl:variable name="collection" select="response/node/collection"/>
       <xsl:apply-templates 
          select="$collection/spi:node[(position() mod $columns) != 0]" mode="group"/>
    </xsl:template>
    
    <xsl:template match="node" mode="group">
       <div>
          <div class="left">
             <xsl:call-template name="spi:node"/>
          </div>
          <div class="right">
             <xsl:apply-templates select="following-sibling::spi:node[1]"/>
          </div>
       </div>
    </xsl:template>
    
    <xsl:template name="node" match="node">
       <xsl:value-of select="articleId"/>
       <xsl:value-of select="headline"/>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to select an H1 element which is the second-child in its group
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post

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.