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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:04:57+00:00 2026-05-28T22:04:57+00:00

I’m trying to generate a multi level nested html list from xml/xsl. For example,

  • 0

I’m trying to generate a multi level nested html list from xml/xsl.

For example, a preferred html output would be:

<ul>
 <li>Level 1 - Item 1</li>
    <ul>
        <li>Level 2 - Item 1-1</li>
        <li>Level 2 - Item 1-2</li>
    </ul>

<li> Level 1 - Item 2</li>
    <ul>
        <li>Level 2 - Item 2-1
            <ul>
                <li>Level 3 - Item 2-1-1</li>
                <li>Level 3 - Item 2-1-2</li>
                <li>Level 3 - Item 2-1-3</li>
            </ul>
        </li>
        <li>Level 2 - Item 2-2
            <ul>
                <li>Level 3 - Item 2-2-1</li>
                <li>Level 3 - Item 2-2-2</li>
            </ul>
        </li>
</ul>

XML:

<doc>

    <item>
        <one>Level 1 - Item 1</one>
            <two>Level 2 - Item 1-1</two>
            <two>Level 2 - Item 1-2</two>
    </item>

    <item>
        <one>Level 2 - Item 2</one>
            <two>Level 2 - Item 2-1</two>
                <three>Level 3 - Item 2-1-1</three>
                <three>Level 3 - Item 2-1-2</three>
                <three>Level 3 - Item 2-1-3</three>
            <two>Level 2 - Item 2-2</two>
                <three>Level 3 - Item 2-2-1</three>
                <three>Level 3 - Item 2-2-2</three> 
    </item>

</doc>

My poor attempt XSL:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
    <html>
    <body>
    <xsl:for-each select="doc/item">
    <li><xsl:value-of select="one" />
    <ul>
    <xsl:for-each select="two">
    <li><xsl:value-of select="."/>
    <xsl:for-each select="../three"><ul><li><xsl:value-of select="."/></li></ul></xsl:for-each>
    </li>
    </xsl:for-each>
    </ul>
    </li>
    </xsl:for-each>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>

This is what I’m getting below… Notice that when there’s a level 3 item then all of items have merged and then displaying under both.

    <li>Level 1 - Item 1<ul>
    <li>Level 2 - Item 1-1</li>
    <li>Level 2 - Item 1-2</li>
    </ul>
    </li>
    <li>Level 2 - Item 2<ul>
    <li>Level 2 - Item 2-1<ul>
    <li>Level 3 - Item 2-1-1</li>
    </ul>

    <ul>
    <li>Level 3 - Item 2-1-2</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-1-3</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-2-1</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-2-2</li>
    </ul>
    </li>

    <li>Level 2 - Item 2-2<ul>
    <li>Level 3 - Item 2-1-1</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-1-2</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-1-3</li>
    </ul>
    <ul>
    <li>Level 3 - Item 2-2-1</li>
    </ul>

    <ul>
    <li>Level 3 - Item 2-2-2</li>
    </ul>
    </li>
    </ul>
    </li>

Please provide me with 1.0 solutions and then of course show 2.0 examples to help others as well.

thank you!

  • 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-28T22:04:57+00:00Added an answer on May 28, 2026 at 10:04 pm

    Here is an XSLT 1.0 solution that works with your input XML.

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:my="http://tempuri.org"
      exclude-result-prefixes="my"
    >
      <xsl:output indent="yes" />
    
      <!-- define which elements are where in the hierarchy -->
      <my:level name="one"   higher="" deeper="two,three" />
      <my:level name="two"   higher="one" deeper="three"  />
      <my:level name="three" higher="one,two" deeper="" />
    
      <xsl:template match="doc">
        <body>
          <xsl:apply-templates mode="ul" select="item/*[1]" />
        </body>
      </xsl:template>
    
      <xsl:template match="one|two|three" mode="ul">
        <ul>
          <xsl:apply-templates mode="li" select="." />
        </ul>
      </xsl:template>
    
      <xsl:template match="one|two|three" mode="li">
        <xsl:variable name="myName" select="name()" />
        <xsl:variable name="myID"   select="generate-id()" />
        <!-- select the appropriate hierarchy info for this node -->
        <xsl:variable name="level"  select="
          document('')/*/my:level[@name = $myName]
        " />
        <li>
          <xsl:value-of select="." />
          <!-- create <ul> if immediately follwing sibling is deeper -->
          <xsl:apply-templates mode="ul" select="
            following-sibling::*[1][contains($level/@deeper, name())]
          " />
        </li>
        <!-- process contiguous following siblings of same level -->
        <xsl:apply-templates mode="li" select="
          following-sibling::*[name() = $myName][
            generate-id(
              preceding-sibling::*[contains($level/@higher, name())][1]/following-sibling::*[1]
            ) 
            = $myID
          ]
        " />
      </xsl:template>
    
    </xsl:stylesheet>
    

    Given the input document from your question, it produces this output:

    <body>
      <ul>
        <li>Level 1 - Item 1
          <ul>
            <li>Level 2 - Item 1-1</li>
            <li>Level 2 - Item 1-2</li>
          </ul>
        </li>
      </ul>
      <ul>
        <li>Level 2 - Item 2
          <ul>
            <li>Level 2 - Item 2-1
              <ul>
                <li>Level 3 - Item 2-1-1</li>
                <li>Level 3 - Item 2-1-2</li>
                <li>Level 3 - Item 2-1-3</li>
              </ul>
            </li>
            <li>Level 2 - Item 2-2
              <ul>
                <li>Level 3 - Item 2-2-1</li>
                <li>Level 3 - Item 2-2-2</li>
              </ul>
            </li>
          </ul>
        </li>
      </ul>
    </body>
    

    Frankly, I’m too tired right now to explain the solution in detail. I’ve left a few comments though. Suffice it to say that it is pretty complicated.

    If your XML would look like this (i.e. properly nested):

    <doc>
      <item title="Level 1 - Item 1">
        <item title="Level 2 - Item 1-1" />
        <item title="Level 2 - Item 1-2" />
      </item>
      <item title="Level 2 - Item 2">
        <item title="Level 2 - Item 2-1">
          <item title="Level 3 - Item 2-1-1" />
          <item title="Level 3 - Item 2-1-2" />
          <item title="Level 3 - Item 2-1-3" />
        </item>
        <item title="Level 2 - Item 2-2">
          <item title="Level 3 - Item 2-2-1" />
          <item title="Level 3 - Item 2-2-2" />
        </item>
      </item>
    </doc>
    

    a solution that would produce the same HTML result as above would look like this:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output indent="yes" />
    
      <xsl:template match="doc">
        <body>
          <xsl:for-each select="item">
            <ul>
              <xsl:apply-templates select="." />
            </ul>
          </xsl:for-each>
        </body>
      </xsl:template>
    
      <xsl:template match="item">
        <li>
          <xsl:value-of select="@title" />
          <xsl:if test="item">
            <ul>
              <xsl:apply-templates select="item" />
            </ul>
          </xsl:if>
        </li>
      </xsl:template>
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am currently running into a problem where an element is coming back from

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.