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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:14:16+00:00 2026-06-04T06:14:16+00:00

I have a flat file with xml data that contains parent and child information

  • 0

I have a flat file with xml data that contains parent and child information i need a xslt to transform

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <CRStructure>
    <objid>CA0D2594-183B-4E80-B2CA-4F915A1E2D32</objid>
    <cr_id>87</cr_id>
    <cr_parent>14</cr_parent>
  </CRStructure>
  <CRStructure>
    <objid>23BD80FA-7ACE-4111-9607-8AC0857868AF</objid>
    <cr_id>172</cr_id>
    <cr_parent>128</cr_parent>
  </CRStructure>
  <CRStructure>
    <objid>E381DE99-AD82-428E-A82B-63EB965BA2F4</objid>
    <cr_id>247</cr_id>
    <cr_parent>138</cr_parent>
      </CRStructure>
  <CRStructure> ............

To

<CR  xmlns="http://ait.com/cr/">

  <CRItems>

    <CRItem ObjectId="69230491-BCB8-4CD5-9FC3-2113FFE832EC">
     <CrId>1</CrId> 

        <CRItems>

        <CRItem ObjectId="2D425940-B3B1-432E-BDA4-6778C9AE8391">
            <CrId>2</CrId>
             <CRItems>

            <CRItem ObjectId="3F2DF482-0485-42C9-A1D2-FCFE0EF6B4E3">
                <CrId>22</CrId>
              </CRItem> .........

The hierarchical structure is always three levels , anyone that can point me in the right direction.

EDIT

Tried this and it seems to work ,

  <xsl:template match="//NewDataSet">
     <CR>
       <CRItems>
     <xsl:for-each select="CRStructure[cr_parent='0']">
       <CRItem>
         <xsl:attribute name="ObjectId">
           <xsl:value-of select="objid" />
         </xsl:attribute>
         <CRItems>
         <xsl:variable name="cridequ" select="cr_id" />
         <xsl:for-each select="//NewDataSet/CRStructure[cr_parent=$cridequ]">
           <CRItem>
             <xsl:attribute name="ObjectId">
               <xsl:value-of select="objid" />
             </xsl:attribute>
             <CRItems>
               <xsl:variable name="cridsub" select="cr_id" />
               <xsl:for-each select="//NewDataSet/CRStructure[cr_parent=$cridsub]">
                 <CRItem>
                   <xsl:attribute name="ObjectId">
                     <xsl:value-of select="objid" />
                   </xsl:attribute>
                 </CRItem>
               </xsl:for-each>
             </CRItems>                          
           </CRItem>
         </xsl:for-each>
         </CRItems>
       </CRItem>   
    </xsl:for-each>
         </CRItems>
     </CR>
  </xsl:template>

Not sure if this is the correct way of solving it , seems like most suggestions i find is to use some combinations of <xsl:template match.... but not sure how to do this

  • 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-04T06:14:17+00:00Added an answer on June 4, 2026 at 6:14 am

    What you are proposing is correct but could be generalized and simplified as:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://ait.com/cr/" version="1.0">
    
        <xsl:output indent="yes"/>
    
        <xsl:key name="sons" match="CRStructure" use="cr_parent"/>
        <xsl:param name="maxLevels">3</xsl:param>
    
        <xsl:template match="NewDataSet">
            <CR>
                <CRItems>
                    <xsl:apply-templates select="key('sons', 0)"/>
                </CRItems>
            </CR>
        </xsl:template>
    
        <xsl:template match="CRStructure">
            <xsl:param name="level">1</xsl:param>
            <xsl:if test="$level &lt;= $maxLevels">
                <CRItem ObjectId="{objid}">
                    <CrId>
                        <xsl:value-of select="cr_id"/>
                    </CrId>
                    <CRItems>
                        <xsl:apply-templates select="key('sons', cr_id)">
                            <xsl:with-param name="level" select="$level  + 1"/>
                        </xsl:apply-templates>
                    </CRItems>
                </CRItem>
            </xsl:if>
        </xsl:template>
    </xsl:stylesheet>
    

    The variable $index has been introduced to avoid to loop forever if there is an error in your input document 😉 …

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

Sidebar

Related Questions

I have a windows mobile 5.0 application (smartphone) that contains a flat data file
I have a flat data file that I need to import into my SQL
I have a Flat File Source that reads data from a file with two
I have simple SSIS package which reads data from flat file and insert into
I have an OLE DB Data source and a Flat File Destination in the
I have simple SSIS package where I import data from flat file into SQL
I have a tab delimited file on a shared path. I've setup that flat
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
I have a flat file with names, addresses, and other data. Most of the
I have a flat file that is pipe delimited and an database table. 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.