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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:38:38+00:00 2026-05-27T15:38:38+00:00

I need to transform large XML files that have a nested (hierarchical) structure of

  • 0

I need to transform large XML files that have a nested (hierarchical) structure of the form

<Root>
   Flat XML
   Hierarchical XML (multiple blocks, some repetitive)
   Flat XML
</Root>

into a flatter (“shredded”) form, with 1 block for each repetitive nested block.

The data has numerous different tags and hierarchy variations (especially in the number of tags of the shredded XML before and after the hierarchical XML), so ideally no assumption should be made about tag and attribute names, or the hierarchical level.

A top-level view of the hierarchy for just 4 levels would look something like

<Level 1>
   ...
   <Level 2>
      ...
      <Level 3>
        ...
        <Level 4>A</Level 4>
        <Level 4>B</Level 4>
        ...
      </Level 3>
      ...
   </Level 2>
   ...
</Level 1>

and the desired output would then be

<Level 1>
  ...
  <Level 2>
    ...
      <Level 3>
        ...
        <Level 4>A</Level 4>
        ...
      </Level 3>
    ...
  </Level 2>
  ...
</Level 1>

<Level 1>
  ...
  <Level 2>
    ...
      <Level 3>
        ...
        <Level 4>B</Level 4>
        ...
      </Level 3>
    ...
  </Level 2>
  ...
</Level 1>

That is, if at each level i there are Li different components, a total of Product(Li) different components will be produced (just 2 above, since the only differentiating factor is Level 4, so L1*L2*L3*L4 = 2).

From what I have seen around, XSLT may be the way to go, but any other solution (e.g., StAX or even JDOM) would do.

A more detailed example, using fictitious information, would be

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="US">
      <Comment>List of previous jobs in the US</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Senior Developer">
          <StartDate>01/10/2001</StartDate>
          <Months>38</Months>
        </Job>
        <Job title = "Senior Developer">
          <StartDate>01/12/2004</StartDate>
          <Months>6</Months>
        </Job>
        <Job title = "Senior Developer">
          <StartDate>01/06/2005</StartDate>
          <Months>10</Months>
        </Job>
      </JobDetails>
    </Employment>
  </EmploymentHistory>
  <EmploymentHistory>
    <Employment country="UK">
      <Comment>List of previous jobs in the UK</Comment>
      <Jobs>2</Jobs>
      <JobDetails>
        <Job title = "Junior Developer">
          <StartDate>01/05/1999</StartDate>
          <Months>25</Months>
        </Job>
        <Job title = "Junior Developer">
          <StartDate>01/07/2001</StartDate>
          <Months>3</Months>
        </Job>
      </JobDetails>
    </Employment>
  </EmploymentHistory>
  <Available>true</Available>
  <Experience unit="years">6</Experience>
</Employee>

The above data should be shredded into 5 blocks (i.e., one for each different <Job> block), each of which will leave all other tags identical and just have a single <Job> element. So, given the 5 different <Job> blocks in the above example, the transformed (“shredded”) XML would be

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="US">
      <Comment>List of previous jobs in the US</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Senior Developer">
          <StartDate>01/10/2001</StartDate>
          <Months>38</Months>
        </Job>
      </JobDetails>
      <Available>true</Available>
     <Experience unit="years">6</Experience>
    </Employment>
  </EmploymentHistory>
</Employee>

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="US">
      <Comment>List of previous jobs in the US</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Senior Developer">
          <StartDate>01/12/2004</StartDate>
          <Months>6</Months>
        </Job>
      </JobDetails>
      <Available>true</Available>
     <Experience unit="years">6</Experience>
    </Employment>
  </EmploymentHistory>
</Employee>

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="US">
      <Comment>List of previous jobs in the US</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Senior Developer">
          <StartDate>01/06/2005</StartDate>
          <Months>10</Months>
        </Job>
      </JobDetails>
      <Available>true</Available>
     <Experience unit="years">6</Experience>
    </Employment>
  </EmploymentHistory>
</Employee>

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="UK">
      <Comment>List of previous jobs in the UK</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Junior Developer">
          <StartDate>01/05/1999</StartDate>
          <Months>25</Months>
        </Job>
      </JobDetails>
      <Available>true</Available>
     <Experience unit="years">6</Experience>
    </Employment>
  </EmploymentHistory>
</Employee>

<Employee name="A Name">
  <Address>123 A Street</Address>
  <Age>28</Age>
  <EmploymentHistory>
    <Employment country="UK">
      <Comment>List of previous jobs in the UK</Comment>
      <Jobs>3</Jobs>
      <JobDetails>
        <Job title = "Junior Developer">
          <StartDate>01/07/2001</StartDate>
          <Months>3</Months>
        </Job>
      </JobDetails>
      <Available>true</Available>
     <Experience unit="years">6</Experience>
    </Employment>
  </EmploymentHistory>
</Employee>
  • 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-27T15:38:39+00:00Added an answer on May 27, 2026 at 3:38 pm

    Given the following XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <Employee name="A Name">
      <Address>123 A Street</Address>
      <Age>28</Age>
      <EmploymentHistory>
        <Employment country="US">
          <Comment>List of previous jobs in the US</Comment>
          <Jobs>3</Jobs>
          <JobDetails>
            <Job title = "Developer">
              <StartDate>01/10/2001</StartDate>
              <Months>38</Months>
            </Job>
            <Job title = "Developer">
              <StartDate>01/12/2004</StartDate>
              <Months>6</Months>
            </Job>
            <Job title = "Developer">
              <StartDate>01/06/2005</StartDate>
              <Months>10</Months>
            </Job>
          </JobDetails>
          </Employment>
          <Employment country="UK">
            <Comment>List of previous jobs in the UK</Comment>
            <Jobs>2</Jobs>
            <JobDetails>
              <Job title = "Developer">
                <StartDate>01/05/1999</StartDate>
                <Months>25</Months>
              </Job>
              <Job title = "Developer">
                <StartDate>01/07/2001</StartDate>
                <Months>3</Months>
              </Job>
            </JobDetails>
            </Employment>
      </EmploymentHistory>
      <Available>true</Available>
      <Experience unit="years">6</Experience>
    </Employee>
    

    The following XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/">
          <Output>
            <xsl:apply-templates select="//Employee/EmploymentHistory/Employment/JobDetails/Job" />
          </Output>
        </xsl:template>
    
      <xsl:template match="//Employee/EmploymentHistory/Employment/JobDetails/Job">
        <Employee>
          <xsl:attribute name="name">
            <xsl:value-of select="ancestor::Employee/@name"/>
          </xsl:attribute>
          <Address>
            <xsl:value-of select="ancestor::Employee/Address"/>
          </Address>
          <Age>
            <xsl:value-of select="ancestor::Employee/Age"/>
          </Age>
          <EmploymentHistory>
            <Employment>
              <xsl:attribute name="country">
                <xsl:value-of select="ancestor::Employment/@country"/>
              </xsl:attribute>
              <Comment>
                <xsl:value-of select="ancestor::Employment/Comment"/>
              </Comment>
              <Jobs>
                <xsl:value-of select="ancestor::Employment/Jobs"/>
              </Jobs>
              <JobDetails>
                <xsl:copy-of select="."/>
              </JobDetails>
              <Available>
                <xsl:value-of select="ancestor::Employee/Available"/>
              </Available>
              <Experience>
                <xsl:attribute name="unit">
                  <xsl:value-of select="ancestor::Employee/Experience/@unit"/>
                </xsl:attribute>
                <xsl:value-of select="ancestor::Employee/Experience"/>
              </Experience>
            </Employment>
          </EmploymentHistory>
        </Employee>
    
      </xsl:template>
    
    
    </xsl:stylesheet>
    

    Gives the following output:

    <?xml version="1.0" encoding="utf-8"?>
    <Output>
      <Employee name="A Name">
        <Address>123 A Street</Address>
        <Age>28</Age>
        <EmploymentHistory>
          <Employment country="US">
            <Comment>List of previous jobs in the US</Comment>
            <Jobs>3</Jobs>
            <JobDetails>
              <Job title="Developer">
              <StartDate>01/10/2001</StartDate>
              <Months>38</Months>
            </Job>
            </JobDetails>
            <Available>true</Available>
            <Experience unit="years">6</Experience>
          </Employment>
        </EmploymentHistory>
      </Employee>
      <Employee name="A Name">
        <Address>123 A Street</Address>
        <Age>28</Age>
        <EmploymentHistory>
          <Employment country="US">
            <Comment>List of previous jobs in the US</Comment>
            <Jobs>3</Jobs>
            <JobDetails>
              <Job title="Developer">
              <StartDate>01/12/2004</StartDate>
              <Months>6</Months>
            </Job>
            </JobDetails>
            <Available>true</Available>
            <Experience unit="years">6</Experience>
          </Employment>
        </EmploymentHistory>
      </Employee>
      <Employee name="A Name">
        <Address>123 A Street</Address>
        <Age>28</Age>
        <EmploymentHistory>
          <Employment country="US">
            <Comment>List of previous jobs in the US</Comment>
            <Jobs>3</Jobs>
            <JobDetails>
              <Job title="Developer">
              <StartDate>01/06/2005</StartDate>
              <Months>10</Months>
            </Job>
            </JobDetails>
            <Available>true</Available>
            <Experience unit="years">6</Experience>
          </Employment>
        </EmploymentHistory>
      </Employee>
      <Employee name="A Name">
        <Address>123 A Street</Address>
        <Age>28</Age>
        <EmploymentHistory>
          <Employment country="UK">
            <Comment>List of previous jobs in the UK</Comment>
            <Jobs>2</Jobs>
            <JobDetails>
              <Job title="Developer">
                <StartDate>01/05/1999</StartDate>
                <Months>25</Months>
              </Job>
            </JobDetails>
            <Available>true</Available>
            <Experience unit="years">6</Experience>
          </Employment>
        </EmploymentHistory>
      </Employee>
      <Employee name="A Name">
        <Address>123 A Street</Address>
        <Age>28</Age>
        <EmploymentHistory>
          <Employment country="UK">
            <Comment>List of previous jobs in the UK</Comment>
            <Jobs>2</Jobs>
            <JobDetails>
              <Job title="Developer">
                <StartDate>01/07/2001</StartDate>
                <Months>3</Months>
              </Job>
            </JobDetails>
            <Available>true</Available>
            <Experience unit="years">6</Experience>
          </Employment>
        </EmploymentHistory>
      </Employee>
    </Output>
    

    Note that I’ve added an Output root element to ensure the document is well formed.

    Is this what you wanted?

    You might also be able to use xsl:copy to copy the higher level elements, but I need to think about this one a bit more. With the above xslt, you have more control, but also you have to redefine your elements…

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

Sidebar

Related Questions

Question We have a large number of xml configuration files that we want merged
I need to transform my nested sets structure (mysql) into json for this spacetree
I need a tool to execute XSLTs against very large XML files. To be
I have a large commit of many files on one branch, I need to
We have large sets (10+) of very large files (> 1 GB) that we
I need to transform a valid XML document to the OFX v1.0.2 format .
I am having some problems with an XML transform and need some help. The
I have an existing CakePHP that runs on a LAMP environment and need to
I have a need to transfer large streams of data from a web service.
I have a bunch of pretty large CSV (comma separated values) files and I

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.