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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:02:31+00:00 2026-06-15T13:02:31+00:00

I have a requirement to transform a sequential XML node list into a hierarchical,

  • 0

I have a requirement to transform a sequential XML node list into a hierarchical, but I run into some XSLT specific knowledge gap.
The input XML contains articles, colors and sizes. In the sample below ‘Record1’ is an article, ‘Record2’ represents a color and ‘Record3’ are the sizes. The number of colors and sizes (record2 and record3) elements can vary.

<root>
 <Record1>...</Record1>
 <Record2>...</Record2>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record2>...</Record2>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record1>...</Record1>
 <Record2>...</Record2>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record2>...</Record2>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record3>...</Record3>
 <Record3>...</Record3>
</root> 

All fields are on the same hierarchical level, but still I have to create this structure as output:

<root>
 <article>              -> Record1
  <color>               -> Record2
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
  </color>
  <color>               -> Record2
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
  </color>
 </article>
 <article>              -> Record1
  <color>               -> Record2
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
  </color>
  <color>               -> Record2
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
   <size>...</size>     -> Record3
  </color>
 </article>
</root>

I’ve tried to iterate the nodes sequentially but for example the ‘article’ (=record1) node tag needs to remain unclosed while ‘color’ (=record2) nodes are processed. The same counts for processing ‘size’ (=record3) having ‘color’ unclosed, but that is not allowed by XSLT.
My next idea was to call a template for every article, color and size level, but I don’t know how to select for example all ‘record3’ nodes between the current ‘record2’ and the next article represented by ‘record1’.

I’ve also got a limitation on the XSLT version because I need this transformation in BizTalk Server which only supports XSLT 1.0

Can someone push me in the right direction?

  • 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-15T13:02:32+00:00Added an answer on June 15, 2026 at 1:02 pm

    Here’s one XSLT 1.0 option. I’m not sure what you wanted to do with the values of Record1 and Record2, so I put them in a val attribute.

    XML Input

    <root>
        <Record1>article1</Record1>
        <Record2>color1</Record2>
        <Record3>size1</Record3>
        <Record3>size2</Record3>
        <Record2>color2</Record2>
        <Record3>size3</Record3>
        <Record3>size4</Record3>
        <Record3>size5</Record3>
        <Record3>size6</Record3>
        <Record1>article2</Record1>
        <Record2>color3</Record2>
        <Record3>size7</Record3>
        <Record3>size8</Record3>
        <Record2>color4</Record2>
        <Record3>size9</Record3>
        <Record3>size10</Record3>
        <Record3>size11</Record3>
        <Record3>size12</Record3>
    </root>
    

    XSLT 1.0

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:template match="/*">
            <xsl:copy>
                <xsl:apply-templates select="Record1"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="Record1">
            <article val="{.}">
                <xsl:apply-templates select="following-sibling::Record2[generate-id(preceding-sibling::Record1[1])=generate-id(current())]"/>
            </article>
        </xsl:template>
    
        <xsl:template match="Record2">
            <color val="{.}">
                <xsl:apply-templates select="following-sibling::Record3[generate-id(preceding-sibling::Record2[1])=generate-id(current())]"/>
            </color>
        </xsl:template>
    
        <xsl:template match="Record3">
            <size>
                <xsl:value-of select="."/>
            </size>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XML Output

    <root>
       <article val="article1">
          <color val="color1">
             <size>size1</size>
             <size>size2</size>
          </color>
          <color val="color2">
             <size>size3</size>
             <size>size4</size>
             <size>size5</size>
             <size>size6</size>
          </color>
       </article>
       <article val="article2">
          <color val="color3">
             <size>size7</size>
             <size>size8</size>
          </color>
          <color val="color4">
             <size>size9</size>
             <size>size10</size>
             <size>size11</size>
             <size>size12</size>
          </color>
       </article>
    </root>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to run a transform on a xml file. it is
I've got a requirement to take some XML and transform it into a fixed-width
I have this kinda interesting requirement. Typically you use XSLT to transform an XML
I have a requirement where I need to transform one XML document to another
I have requirement where some times I would like to load children as well
I have requirement of specifying web part connections in onet.xml. So when site is
I have requirement to get Facebook friends list with their images. How can I
I have one huge XML document. I have set of XSL representing each node
I have a requirement to create a simple database in Access to collect some
I have the requirement to transform images attached to every document (actually need images

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.