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

  • Home
  • SEARCH
  • 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 222199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:06:17+00:00 2026-05-11T19:06:17+00:00

I have a dynamic XML document which represents a tree structure of categories, but

  • 0

I have a dynamic XML document which represents a tree structure of categories, but does so using path separated attributes in arbitrary order – like this:

   <data>    
      <record ID="24" Name="category 1\sub category 1"/>   
      <record ID="26" Name="category 1"/>     
      <record ID="25" Name="category 1\sub category 1\sub category 2"/>    
      <record ID="27" Name="category 1\sub category 1\sub category 3"/>    
      ...
   </data> 

I need to come up with a solution that ‘normalizes’ the XML so that it is transformed into something like this:

   <data>    
      <record ID="26" Name="category 1">    
         <record ID="24" Name="sub category 1">    
            <record ID="25" Name="sub category 2"/>
            <record ID="27" Name="sub category 3"/>    
         </record>
      </record>   
      ...
   </data>

Basically I was wondering if this is something XSLT might be able to address, and how, rather than having to do it programmatically.

  • 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-11T19:06:18+00:00Added an answer on May 11, 2026 at 7:06 pm

    Sure, no problem:

    <xsl:stylesheet 
      version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
    
      <xsl:output indent="yes" />
    
      <xsl:template match="/data">
        <!-- copy the document element -->
        <xsl:copy>
          <!-- That's where we start: all "record" nodes that have no "\". -->
          <xsl:apply-templates mode="recurse" select="/data/record[
            not(contains(@Name, '\'))
          ]" />
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="record" mode="recurse">
        <xsl:param name="starting-path" select="''" />
    
        <!-- The record node and its ID attribute can be copied. --> 
        <xsl:copy>
          <xsl:copy-of select="@ID" />
    
          <!-- Create the new "name" attribute. -->
          <xsl:attribute name="Name">
            <xsl:value-of select="substring-after(@Name, $starting-path)" />
          </xsl:attribute>
    
          <!-- Append a backslash to the current path. -->
          <xsl:variable name="current-path" select="concat(@Name, '\')" />
    
          <!-- Select all "record" nodes that are one level deeper... -->
          <xsl:variable name="children" select="/data/record[
            starts-with(@Name, $current-path)
            and
            not(contains(substring-after(@Name, $current-path), '\'))
          ]" />
    
          <!-- ...and apply this template to them. -->
          <xsl:apply-templates mode="recurse" select="$children">
            <xsl:with-param name="starting-path" select="$current-path" />
          </xsl:apply-templates>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Output on my system:

    <data>
      <record ID="26" Name="category 1">
        <record ID="24" Name="sub category 1">
          <record ID="25" Name="sub category 2"></record>
          <record ID="27" Name="sub category 3"></record>
        </record>
      </record>
    </data>
    

    Note that the whole solution is based on the assumption that all the paths are canonical and do not contain trailing backslashes.

    Also note that any unmatched/orphaned “record” elements will not be in the output (unless they are at the root level, of course).

    One more thing: The template mode (“recurse”) is not strictly necessary. I included it because the template is doing something rather special, and there might be the chance that there is another template in your solution that matches “record” nodes. In this case this solution can be dropped in without breaking anything else. For a standalone solution, the template modes can be dropped anytime.

    Oh, and the last thing: If you want the result document to be ordered by Name, include an <xsl:sort> element with the <xsl:apply-templates> (both occurrences), like so:

    <xsl:apply-templates select="...">
      <xsl:sort select="@Name" data-type="text" order="ascending" />
    </xsl:apply-templates>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 152k
  • Answers 152k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to edit your: wp-config.php file and modify the… May 12, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer You need to use the GetInterface or GetInterfaces methods of… May 12, 2026 at 10:05 am
  • Editorial Team
    Editorial Team added an answer Run the following immediately after the dialog is called in… May 12, 2026 at 10:05 am

Related Questions

I have an XML document defined like so XML File <TABLE> <RECORD> <PAGENUMBER> 1
I'm using a winforms webbrowser control to display some content in a windows forms
I have a JavaScript slideshow that pre-loads images out of a MySQL database and
I am trying to create a dynamic menu by reading an XML file using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.