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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:49:17+00:00 2026-06-01T23:49:17+00:00

I have a problem an at the moment no idea to solve it ;-(

  • 0

I have a problem an at the moment no idea to solve it ;-(

I have a category structure as input document (xml) and want to build a path structure.
I can only use xslt and want to generate a new xml structure.

The input structure looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<Positions>
    <Positionen>
        <ID>1</ID>
        <Parent></Parent>
    </Positionen>

    <Positionen>
        <ID>2</ID>
        <Parent>1</Parent>
    </Positionen>

    <Positionen>
        <ID>3</ID>
        <Parent>1</Parent>
    </Positionen>

    <Positionen>
        <ID>4</ID>
        <Parent>2</Parent>
    </Positionen>

    <Positionen>
        <ID>5</ID>
        <Parent>4</Parent>
    </Positionen>

    <Positionen>
        <ID>6</ID>
        <Parent>2</Parent>
    </Positionen>

</Positions>

The output structure should be this:

<?xml version="1.0" encoding="UTF-8"?>
<Positions>
    <Positionen>
        <ID>1</ID>
        <Parent></Parent>
        <Path>1</Path>
    </Positionen>

    <Positionen>
        <ID>2</ID>
        <Parent>1</Parent>
        <Path>1/2</Path>
    </Positionen>

    <Positionen>
        <ID>3</ID>
        <Parent>1</Parent>
        <Path>1/3</Path>
    </Positionen>

    <Positionen>
        <ID>4</ID>
        <Parent>2</Parent>
        <Path>1/2/4</Path>
    </Positionen>

    <Positionen>
        <ID>5</ID>
        <Parent>4</Parent>
        <Path>1/2/4/5</Path>
    </Positionen>

    <Positionen>
        <ID>6</ID>
        <Parent>2</Parent>
        <Path>1/2/6</Path>
    </Positionen>

</Positions>

How can I do this with xslt with a recursion?
Hoping fore some help. Thanks in advance.
LStrike

  • 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-01T23:49:18+00:00Added an answer on June 1, 2026 at 11:49 pm

    Some answers to this question are good but rather inefficient (O(N^2)).

    This is so, because the path is constructed from scratch for every Positionen element. The average path length is N/2 and there are N Positionen elements. That means that N*N/2 operations are needed as minimum to construct all paths — and this is quadratical time complexity.

    Here is a more efficient O(N*log(N)) — could be even (O(N) — linear) solution in case it is acceptable for the Positionen elements in the output to be unsorted:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kChildren" match="Positionen" use="Parent"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="/">
      <xsl:variable name="vrtfPass1">
        <xsl:apply-templates select="node()|@*"/>
      </xsl:variable>
    
      <xsl:variable name="vPass1" select="ext:node-set($vrtfPass1)"/>
    
      <xsl:apply-templates select="$vPass1/*" mode="pass2"/>
     </xsl:template>
    
     <xsl:template match="/*">
      <xsl:copy>
        <xsl:apply-templates select="Positionen[not(number(Parent))]" mode="path"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="Positionen" mode="path">
      <xsl:param name="pPath"/>
    
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
        <Path><xsl:value-of select="concat($pPath, ID)"/></Path>
      </xsl:copy>
      <xsl:apply-templates select="key('kChildren', ID)" mode="path">
       <xsl:with-param name="pPath" select="concat($pPath, ID, '/')"/>
      </xsl:apply-templates>
     </xsl:template>
    
     <xsl:template match="/*" mode="pass2">
      <xsl:copy>
           <xsl:apply-templates select="node()|@*">
             <xsl:sort select="ID" data-type="number"/>
           </xsl:apply-templates>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <Positions>
        <Positionen>
            <ID>1</ID>
            <Parent></Parent>
        </Positionen>
        <Positionen>
            <ID>2</ID>
            <Parent>1</Parent>
        </Positionen>
        <Positionen>
            <ID>3</ID>
            <Parent>1</Parent>
        </Positionen>
        <Positionen>
            <ID>4</ID>
            <Parent>2</Parent>
        </Positionen>
        <Positionen>
            <ID>5</ID>
            <Parent>4</Parent>
        </Positionen>
        <Positionen>
            <ID>6</ID>
            <Parent>2</Parent>
        </Positionen>
    </Positions>
    

    the wanted, correct result is produced:

    <Positions>
       <Positionen>
          <ID>1</ID>
          <Parent/>
          <Path>1</Path>
       </Positionen>
       <Positionen>
          <ID>2</ID>
          <Parent>1</Parent>
          <Path>1/2</Path>
       </Positionen>
       <Positionen>
          <ID>3</ID>
          <Parent>1</Parent>
          <Path>1/3</Path>
       </Positionen>
       <Positionen>
          <ID>4</ID>
          <Parent>2</Parent>
          <Path>1/2/4</Path>
       </Positionen>
       <Positionen>
          <ID>5</ID>
          <Parent>4</Parent>
          <Path>1/2/4/5</Path>
       </Positionen>
       <Positionen>
          <ID>6</ID>
          <Parent>2</Parent>
          <Path>1/2/6</Path>
       </Positionen>
    </Positions>
    

    Do Note:

    Every Path is produced by adding the current ID to the path of the parent (which is calculated only once) — an O(1) operation. Fot the total of N paths this is O(N).

    The final sorting makes the time complexity O(N*log(N)) — still much better than quadratical.

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

Sidebar

Related Questions

i have a very strange problem which i cannot solve at the moment. i
I have a bizarre problem at the moment, I am just making a start
I have a problem and I am stack. If anyone have a spare moment
I'm building a micro site at the moment and I have a problem. I
I am playing around with cuda. At the moment I have a problem. I
The following code summarizes the problem I have at the moment. My current execution
I have problem with fancybox. I want to write a function that will run
I have a little problem with a regex. I want to find in my
I have a problem with my Colorbox not loading any content. javascript: $(document).ready(function(){ $(.exampleterms).colorbox({inline:true,
I have this idea for a free backup application. The largest problem I need

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.