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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:55:16+00:00 2026-06-12T18:55:16+00:00

I have an XML file with following format: <DataSet> <Data id =1 columns =4>

  • 0

I have an XML file with following format:

 <DataSet>
  <Data id ="1" columns ="4">
    <item name ="data1" value="value1"/>
    <item name ="data2" value="value2"/>
    <item name ="data3" value="value3"/>
    <item name ="data4" value="value4"/>
    <item name ="data5" value="value5"/>
  </Data>
  <Data id="2" columns ="2">
    <item name ="data1" value="value1"/>
    <item name ="data2" value="value2"/>
    <item name ="data3" value="value3"/>
    <item name ="data4" value="value4"/>
  </Data>
</DataSet>

and I need an XSL transformation to get following table structure. Here the idea is to display the name and value attributes in two adjacent cells. So an ‘item’ will be associated with 2 columns and a row will be holding name/value pairs of two items. The number of columns will be specified in the Data element and will be always multiples of 2.

<report>
  <table>
    <tr>
      <td>data1</td>
      <td>value1</td>
      <td>data2</td>
      <td>value2</td>
    </tr>
    <tr>
      <td>data3</td>
      <td>value3</td>
      <td>data4</td>
      <td>value4</td>
    </tr>
    <tr>
      <td>data5</td>
      <td>value5</td>
      <td></td>
      <td></td>
    </tr>
 </table>
 <table>
    <tr>
      <td>data1</td>
      <td>value1</td>
    </tr>
    <tr>
      <td>data2</td>
      <td>value2</td>
    </tr>
    <tr>
      <td>data3</td>
      <td>value3</td>
    </tr>
    <tr>
      <td>data4</td>
      <td>value4</td>
    </tr>
  </table>
</report>
  • 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-12T18:55:17+00:00Added an answer on June 12, 2026 at 6:55 pm

    The following XSL transformation applied to the provided input produces the desired output. Some explanation is provided below.

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
        <xsl:template match="/DataSet"><report>
            <xsl:apply-templates select="@*|node()" />
        </report></xsl:template>
    
        <xsl:template match="Data">
            <xsl:variable name="count" select="count(item)" />
            <xsl:variable name="M" select="@columns div 2" />
            <xsl:variable name="N" select="($count + ($count mod $M)) div $M" />
            <table>
                <xsl:call-template name="nth-row">
                    <xsl:with-param name="n" select="1" />
                    <xsl:with-param name="M" select="$M" />
                    <xsl:with-param name="N" select="$N" />
                </xsl:call-template>
            </table>
        </xsl:template>
    
        <xsl:template name="nth-row">
            <xsl:param name="n" />
            <xsl:param name="N" />
            <xsl:param name="M" />
            <tr>
                <xsl:call-template name="nmth-cell">
                    <xsl:with-param name="n" select="$n" />
                    <xsl:with-param name="m" select="1" />
                    <xsl:with-param name="N" select="$N" />
                    <xsl:with-param name="M" select="$M" />
                </xsl:call-template>
            </tr>
            <xsl:if test="$N > $n">
                <xsl:call-template name="nth-row">
                    <xsl:with-param name="n" select="$n + 1" />
                    <xsl:with-param name="N" select="$N" />
                    <xsl:with-param name="M" select="$M" />
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    
        <xsl:template name="nmth-cell">
            <xsl:param name="n" />
            <xsl:param name="m" />
            <xsl:param name="N" />
            <xsl:param name="M" />
            <xsl:variable name="pos" select="($n - 1) * $M + $m" />
            <xsl:choose>
                <xsl:when test="item[position()=$pos]">
                    <td><xsl:value-of select="item[position()=$pos]/@name" /></td>
                    <td><xsl:value-of select="item[position()=$pos]/@value" /></td>
                </xsl:when>
                <xsl:otherwise>
                    <td></td>
                    <td></td>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:if test="$M > $m">
                <xsl:call-template name="nmth-cell">
                    <xsl:with-param name="n" select="$n" />
                    <xsl:with-param name="m" select="$m + 1" />
                    <xsl:with-param name="N" select="$N" />
                    <xsl:with-param name="M" select="$M" />
                </xsl:call-template>
            </xsl:if>
        </xsl:template>
    </xsl:transform>
    
    1. Matching /DataSet produces the root element <report /> and continues applying templates.

    2. Matching Data from inside /DataSet produces a <table /> for each <Data /> element and then starts the interesting part by calling the template named nth-row. The variables and parameters used are:

      • n: number of current row, starting at 1
      • M: number of columns, calculated from the attribute @columns divided by two, because each <item /> results in two <td /> elements.
      • N: number of rows, calculated from the number of <item /> elements present and divided by M. To account for div truncating integer values the remainder $count mod $M is added to $count before.
    3. Now there come some recursive template calls. Each time nth-row is called, it outputs a <tr /> and then calls nmth-cell with appropriate parameters. As long as the current row is not the last one, nth-row is recursively called with an incremented value of $n.

    4. Finally the template nmth-cell each time it is called outputs two <td /> elements containing the values from the appropriate <item /> or nothing, if there is no corresponding <item />. As long as the current column is not the last one, nmth-cell is recursively called with an incremented value of $m.

    I hope this helps. Feel free to ask, if there is anything wrong with this or unclear to you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML File with the following format: <containers> <container> <item> item name
I have a XML file with the following data format: <net NetName=abc attr1=123 attr2=234
I have an XML file in the following format: <doc> <id name=X> <type name=A>
I have an XML file with the following format: <?xml version=1.0 encoding=UTF-8?> <Items> <Item
I have an xml file in the following format: <food> <desert> cake <desert> </food>
I have the following XML File: <persons> <person name=shawn> <age>34</age> <hair style=spikes>red</hair> </person> <person
I have the following xml file: <xfa:data> <form1> <Page1> <Page2> <contractInfo> ... </contractInfo> <paymentInfo>
I have a xml file main.xml with following markup and data. main.xml <xml> <content>
I have the xml file in the following format. <root> ... <start/> some text
I have a XML File in the following format: <?xml version='1.0' encoding='UTF-8'?> <entry xmlns='http://www.w3.org/2005/Atom'

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.