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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:23:59+00:00 2026-05-23T07:23:59+00:00

I am trying to produce a nested XML document from a flat XML document

  • 0

I am trying to produce a nested XML document from a flat XML document as below. After searching here and other resources available on the net it seems that that the muenchain method could be the solution however I am struggling applying it to my situation.

The XML I need to translate is of form :

<i>
  <item id="1" name="one" sub_id="10" sub_name="s1" detail_id="t1" detail_name="aaaa"/>
  <item id="1" name="one" sub_id="10" sub_name="s1" detail_id="t2" detail_name="bbb"/>
  <item id="1" name="one" sub_id="20" sub_name="s2" detail_id="t1" detail_name="ccc"/>
  <item id="1" name="one" sub_id="20" sub_name="s2" detail_id="t2" detail_name="ddd"/>
  <item id="2" name="two" sub_id="10" sub_name="s1" detail_id="t1" detail_name="eee"/>
  <item id="2" name="two" sub_id="10" sub_name="s1" detail_id="t2" detail_name="fff"/>
  <item id="2" name="two" sub_id="20" sub_name="s2" detail_id="t1" detail_name="ggg"/>
  <item id="2" name="two" sub_id="20" sub_name="s2" detail_id="t2" detail_name="hhh"/>
  <item id="3" name="three" />
  <item id="4" name="four" sub_id="10" sub_name="s1" detail_id="t1" detail_name="mmm"/>
  <item id="4" name="four" sub_id="10" sub_name="s1" detail_id="t2" detail_name="nnn"/>
  <item id="4" name="four" sub_id="20" sub_name="s2" detail_id="t1" detail_name="ooo"/>
  <item id="4" name="four" sub_id="20" sub_name="s2" detail_id="t2" detail_name="ppp"/>
</i>

I would like to tranform this to XML in the following form:

  <i>
    <item id="1" name="one" sub_items="true">
      <sub_item sub_id="10" sub_name="s1">
        <detail detail_id="t1" detail_name="aaaa"/>
        <detail detail_id="t2" detail_name="bbb"/>
      </sub_item>
      <sub_item sub_id="20" sub_name="s2">
        <detail detail_id="t1" detail_name="ccc"/>
        <detail detail_id="t2" detail_name="ddd"/>
      </sub_item>
    </item>
    <item id="2" name="two" sub_items="true">
      <sub_item sub_id="10" sub_name="s1">
        <detail detail_id="t1" detail_name="eee"/>
        <detail detail_id="t2" detail_name="fff"/>
      </sub_item>
      <sub_item sub_id="20" sub_name="s2">
        <detail detail_id="t1" detail_name="ggg"/>
        <detail detail_id="t2" detail_name="hhh"/>
      </sub_item>
    </item>
    <item id="3" name="three" sub_items="false"/>
    <item id="4" name="four" sub_items="true">
      <sub_item sub_id="10" sub_name="s1">
        <detail detail_id="t1" detail_name="mmm"/>
        <detail detail_id="t2" detail_name="nnn"/>
      </sub_item>
      <sub_item sub_id="20" sub_name="s2">
        <detail detail_id="t1" detail_name="ooo"/>
        <detail detail_id="t2" detail_name="ppp"/>
      </sub_item>
    </item>
  </i>

From my research I have the following XLST to perform the transform. I use the key method on the item id attribute. This doesn’t group the data correctly repeating everything for the given item id at each level – which makes sense given the key. So my problem is how do I go about selecting the nodes required to output each nest level do I need to use another key statement?

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:key name="indexes" match="i/item" use="@id"/>
  <xsl:template match="i">
    <i>
    <xsl:for-each select="item[count(. | key('indexes',@id)[1]) = 1]"  >
      <xsl:sort select="@id"/>
      <item>
        <xsl:attribute name="id">
          <xsl:value-of select="@id"/>
        </xsl:attribute>
        <xsl:attribute name="name">
          <xsl:value-of select="@name"/>
        </xsl:attribute>
        <xsl:attribute name="hasRows">
          <xsl:value-of select="@id"/>
        </xsl:attribute>
        <xsl:for-each select="key('indexes',@id)">
          <subitem>
            <xsl:attribute name ="sub_id">
              <xsl:value-of select="@sub_id"/>
            </xsl:attribute>
            <xsl:attribute name ="sub_name">
              <xsl:value-of select="@sub_name"/>
            </xsl:attribute>
            <xsl:for-each select="key('indexes',@id)">
              <segment>
                <xsl:attribute name ="detail_id">
                  <xsl:value-of select="@detail_id"/>
                </xsl:attribute>
                <xsl:attribute name ="detail_name">
                  <xsl:value-of select="@detail_name"/>
                </xsl:attribute>
              </segment>
            </xsl:for-each>
          </subitem>
        </xsl:for-each>
      </item>
    </xsl:for-each>
    </i>
  </xsl:template>
</xsl:stylesheet>

Also is it possible to populate the *sub_items* attribute with true when subitems/details for an item exist and false when they don’t?

Lastly to improve XSLT understanding/skills can anyone recommend good learning resources?

  • 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-23T07:23:59+00:00Added an answer on May 23, 2026 at 7:23 am

    Notice this XSLT 1.0 transform with use of:

    • AVT (attribute value templates) to simplify the attributes definition)
    • one main key, used to group first level item
    • One second compound key to perform sublevel groupings
    • extensive use of xsl:apply-templates with modes, versus xsl:for-each

      <xsl:stylesheet version="1.0"
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      
      <xsl:output method="xml" indent="yes"/>
      <xsl:strip-space elements="*"/>
      
      <xsl:key name="kSubs" match="item" use="concat(@id,'|',@sub_id)"/> 
      <xsl:key name="kItems" match="item" use="@id"/> 
      
      <xsl:template match="i">
          <i>
              <xsl:apply-templates select="item[
                  generate-id()
                      = generate-id(key('kItems',@id)[1])]"/>
          </i>
      </xsl:template>
      
      <xsl:template match="item">
          <item id="{@id}" name="{@name}" sub_items="{boolean(@sub_id)}">
              <xsl:apply-templates select=".|following-sibling::item[
                  (generate-id()
                          = generate-id(key('kSubs',concat(@id,'|',@sub_id))[1]))
                  and
                  (./@id 
                          = current()/@id)
                  ]" mode="sub_id"/>
          </item>
      </xsl:template>
      
      <xsl:template match="item[@sub_id]" mode="sub_id">
          <sub_item sub_id="{@sub_id}" sub_name="{@sub_name}">
              <xsl:apply-templates select="key('kSubs',concat(@id,'|',@sub_id))" 
               mode="detail"/>
          </sub_item>
      </xsl:template>
      
      <xsl:template match="item" mode="detail">
          <detail detail_id="{@detail_id}" detail_name="{@detail_name}"/>
      </xsl:template>
      
      </xsl:stylesheet>
      

    Applied on the input shown in your question, produces:

    <i>
       <item id="1" name="one" sub_items="true">
          <sub_item sub_id="10" sub_name="s1">
             <detail detail_id="t1" detail_name="aaaa"/>
             <detail detail_id="t2" detail_name="bbb"/>
          </sub_item>
          <sub_item sub_id="20" sub_name="s2">
             <detail detail_id="t1" detail_name="ccc"/>
             <detail detail_id="t2" detail_name="ddd"/>
          </sub_item>
       </item>
       <item id="2" name="two" sub_items="true">
          <sub_item sub_id="10" sub_name="s1">
             <detail detail_id="t1" detail_name="eee"/>
             <detail detail_id="t2" detail_name="fff"/>
          </sub_item>
          <sub_item sub_id="20" sub_name="s2">
             <detail detail_id="t1" detail_name="ggg"/>
             <detail detail_id="t2" detail_name="hhh"/>
          </sub_item>
       </item>
       <item id="3" name="three" sub_items="false"/>
       <item id="4" name="four" sub_items="true">
          <sub_item sub_id="10" sub_name="s1">
             <detail detail_id="t1" detail_name="mmm"/>
             <detail detail_id="t2" detail_name="nnn"/>
          </sub_item>
          <sub_item sub_id="20" sub_name="s2">
             <detail detail_id="t1" detail_name="ooo"/>
             <detail detail_id="t2" detail_name="ppp"/>
          </sub_item>
       </item>
    </i>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to produce a pretty simple XML schema for an XML similar to
I'm trying to produce this XML statement (minus the formatting and specific values): <swatchcolor
I'm trying to produce a clipping area in a UIView that's generated from path
I'm trying to produce a plot with two lines using data taken from stdin.
I'm trying to produce sheets of photographs with captions arranged in a grid using
I'm trying to produce just the day number in a WPF text block, without
I'm trying to produce a report that has multiple grouping but does not just
I have been trying to produce a statically linked single binary version of my
I'm trying to produce random integers (uniformly distributed). I found this snippet on an
I am trying to produce SSRS reports to integrate with a MOSS Dashboard. Reporting

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.