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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:04:55+00:00 2026-05-15T08:04:55+00:00

I have the following XML (it is simplified and most attributes are omitted): <Document>

  • 0

I have the following XML (it is simplified and most attributes are omitted):

<Document>
  <Transfer Name="" From="" To=""/>
  <Transfer Name="" From="" To=""/>
  <OtherElement/>
  <OtherElement/>
  <Flight AirLina="" From="" To=""/>
  <Flight AirLina="" From="" To=""/>
  <OtherElement/>
  <Hotel Name="" Duration=""/>
  <Hotel Name="" Duration=""/>
  <OtherElement/>
  <OtherElement/>
  <Extras Name="" Price=""/>
  <Extras Name="" Price=""/>
  <Extras Name="" Price=""/>
  <Extras Name="" Price=""/>
  <Extras Name="" Price=""/>
  <Extras Name="" Price=""/>
  <OtherElement/>
  <OtherElement/>
</Document>

I have a variable, containing different elements:

<xsl:variable name="packageElements" 
select="/Document/Transfer | /Document/Coach | /Document/Flight | /Document/Hotel | /Document/Extras" />

I would like to display that data in a table with 2 columns. I am using XSLT1.0 and MSXSL processor.

I have been trying it out with the simplest solution I could think of:

<table>
  <tbody>
    <xsl:for-each select="$packageElements[position() mod 2 = 1]">
      <tr>
        <td>
          <!-- current element -->
          <xsl:value-of select="local-name()"/>
        </td>
        <td>
          <!-- element following the current in the $packageElements variable -->
          <!-- Here is where I'm stuck, I can't figure out how to correctly pick it up :( -->
        </td>
      </tr>
    </xsl:for-each>
  </tbody>
</table>

Would really appreciate any help.

  • 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-15T08:04:56+00:00Added an answer on May 15, 2026 at 8:04 am

    Alright,

    I have combined @Dimitre Novatchev’s idea from this post’s answers and @Tomalak’s from [XSLT]: Rendering a node sequence as M x N table post. I really liked @Tomalak’s solution with $perRow variable and the <xsl:template name="filler"> template to cope with empty cells.

    With the idea taken from @Dimitre Novatchev answer I’m holding on to $trStartPos. I then calculate $lowerBoundry and $upperBoundry that are used to accumulate all elements that should appear in a row. There might be a more elegant way to do this calculation – please let me know if you come up with one, I would really appreciate it!

    XSLT Transformation

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- Select only required elements -->
    <xsl:variable name="tableData" 
         select="/nums/A | /nums/B | /nums/C | /nums/D | /nums/E "/>
    <xsl:variable name="perRow" select="2"/>
    
    <xsl:template match="/">
        <table border="1">
            <tbody>
                <xsl:apply-templates 
                    select="$tableData[position() mod $perRow = 1]" mode="tr"/>
            </tbody>            
        </table>
    </xsl:template>
    
    
    <xsl:template match="nums/*" mode="tr">
        <xsl:variable name="trStartPos" select="position()" />
        <xsl:variable name="upperBoundry" select="$trStartPos * $perRow" />
        <xsl:variable name="lowerBoundry" select="$upperBoundry - $perRow" />
    
        <tr>
            <xsl:variable name="tdsData" 
                select="$tableData[(position() &gt; $lowerBoundry) and (position() &lt;= $upperBoundry)]" />
            <xsl:apply-templates select="$tdsData" mode="td"/>
            <!-- fill up the last row - @Tomalak's solution -->
            <xsl:if test="count($tdsData) &lt; $perRow">
                <xsl:call-template name="filler">
                    <xsl:with-param name="rest" select="$perRow - count($tdsData)" />
                </xsl:call-template>
            </xsl:if>
        </tr>
    </xsl:template>
    
    
    <!-- Templates for specific elements could be easily added with appropriate info to
         be displayed depending on the element. This one is general just to display
         elements' name and value -->
    <xsl:template match="nums/*" mode="td">
        <td>
            El. name: <xsl:value-of select="local-name()"/> -
            El. value: <xsl:value-of select="."/>
        </td>
    </xsl:template>
    
    <!-- @Tomalak solution (please read beginning of this answer for reference) -->
    <xsl:template name="filler">
        <xsl:param name="rest" select="0" />
        <xsl:if test="$rest">
            <td>&#160;</td>
            <xsl:call-template name="filler">
                <xsl:with-param name="rest" select="$rest - 1" />
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
    </xsl:stylesheet>
    

    applied on the following XML

    <nums>
      <A>A-01</A>
      <num>02</num>
      <num>03</num>
      <num>04</num>
      <B>B-05</B>
      <num>06</num>
      <num>07</num>
      <C>C-08</C>
      <num>09</num>
      <D>D-10</D>
      <num>11</num>
      <num>12</num>
      <num>13</num>
      <E>E-14</E>
      <num>15</num>
    </nums>
    

    results in the following output

    <table border="1">
        <tbody>
            <tr>
                <td>
                    El. name: A -
                    El. value: A-01
                </td>
                <td>
                    El. name: B -
                    El. value: B-05
                </td>
            </tr>
            <tr>
                <td>
                    El. name: C -
                    El. value: C-08
                </td>
                <td>
                    El. name: D -
                    El. value: D-10
                </td>
            </tr>
            <tr>
                <td>
                    El. name: E -
                    El. value: E-14
                </td>
                <td> </td>
            </tr>
        </tbody>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
I have the following xml that's sent to me from a web service. I'm
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following xml I'd like to deserialize into a class <?xml version=1.0
Is it possible to merge elements using XSLT. If I have the following XML
I have the following sample XML structure: <SavingAccounts> <SavingAccount> <ServiceOnline>yes</ServiceOnline> <ServiceViaPhone>no</ServiceViaPhone> </SavingAccount> <SavingAccount> <ServiceOnline>no</ServiceOnline>
I basically have the following flow: XML -> JSON -> Spring MVC -> jsp
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
Say I have the following web.config: <?xml version=1.0 encoding=utf-8?> <configuration> <system.web> <authentication mode=Windows></authentication> </system.web>

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.