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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:14:18+00:00 2026-06-11T03:14:18+00:00

I have flat xml structure, i need to convert into hierarchy. With the help

  • 0

I have flat xml structure, i need to convert into hierarchy. With the help of stackoverflow I was able to do it.
Question: Is it possible to show only one branch using the same flat structure?

Here is my xml and xsl files:

XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Stack.xsl"?>
<Items>
    <Item>
        <Id>1</Id>
        <ParentId>0</ParentId>
        <Name>1</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>2</Id>
        <ParentId>1</ParentId>
        <Name>1.1</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>3</Id>
        <ParentId>1</ParentId>
        <Name>1.2</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>4</Id>
        <ParentId>1</ParentId>
        <Name>1.3</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>5</Id>
        <ParentId>1</ParentId>
        <Name>1.4</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>6</Id>
        <ParentId>0</ParentId>
        <Name>2</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>7</Id>
        <ParentId>6</ParentId>
        <Name>2.1</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>8</Id>
        <ParentId>6</ParentId>
        <Name>2.2</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>9</Id>
        <ParentId>0</ParentId>
        <Name>3</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>10</Id>
        <ParentId>3</ParentId>
        <Name>1.2.1</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>11</Id>
        <ParentId>8</ParentId>
        <Name>2.2.1</Name>
        <SortOrder>0</SortOrder>
    </Item>
    <Item>
        <Id>11</Id>
        <ParentId>5</ParentId>
        <Name>1.4.1</Name>
        <SortOrder>0</SortOrder>
    </Item>
</Items>

XSL

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns="http://www.w3.org/1999/xhtml">

    <xsl:param name="SelectedId" select="'10'"/>
    <xsl:key name="ChildNodes" match="/Items/Item" use="ParentId"/>

    <xsl:template match="Items">
        <ul>
          <xsl:apply-templates select="Item[ParentId = 0]" />
        </ul>
    </xsl:template>

    <xsl:template match="Item">
        <li>
            <xsl:choose>
                <xsl:when test="Id = $SelectedId">
                    <b><xsl:value-of select="Name" /></b>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="Name" />
                </xsl:otherwise>
            </xsl:choose>

            <xsl:variable name="Descendants" select="key ('ChildNodes', Id)" />

            <xsl:if test="count ($Descendants) > 0">
                <ul>
                    <xsl:apply-templates select="$Descendants" />
                </ul>
            </xsl:if>
        </li>
    </xsl:template>
</xsl:stylesheet>

Current output I have:

1
   1.1
   1.2
        1.2.1
   1.3
   1.4
        1.4.1
2
   2.1
   2.2
        2.2.1
3

Desireable result example:

1
   1.1
   1.2
        1.2.1
   1.3
   1.4
2
3
  • 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-11T03:14:20+00:00Added an answer on June 11, 2026 at 3:14 am

    One way to do this is to make use of node-set function, which will require the use of an extension namespace in XSLT.

    What you could do is that instead of outputing the Descendants variable directly as currently:

    <ul>
       <xsl:apply-templates select="$Descendants"/>
    </ul>
    

    You instead store the results in a variable

    <xsl:variable name="list">
       <ul>
          <xsl:apply-templates select="$Descendants"/>
       </ul>
    </xsl:variable>
    

    Then you can convert this ‘result tree fragment’ into a node-set, which you can then check for whether the selected element (held in a b element) exists. If so, you can then output it

    <xsl:if test="exsl:node-set($list)//li[b]">
       <xsl:copy-of select="$list"/>
    </xsl:if>
    

    Here is the full XSLT

    <xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
     xmlns:exsl="urn:schemas-microsoft-com:xslt" 
     exclude-result-prefixes="exsl">
       <xsl:output method="html"/>
       <xsl:param name="SelectedId" select="'10'"/>
       <xsl:key name="ChildNodes" match="/Items/Item" use="ParentId"/>
    
       <xsl:template match="Items">
          <ul>
             <xsl:apply-templates select="Item[ParentId = 0]"/>
          </ul>
       </xsl:template>
    
       <xsl:template match="Item">
          <li>
             <xsl:choose>
                <xsl:when test="Id = $SelectedId">
                   <b>
                      <xsl:value-of select="Name"/>
                   </b>
                </xsl:when>
                <xsl:otherwise>
                   <xsl:value-of select="Name"/>
                </xsl:otherwise>
             </xsl:choose>
             <xsl:variable name="Descendants" select="key ('ChildNodes', Id)"/>
             <xsl:if test="count ($Descendants) &gt; 0">
                <xsl:variable name="list">
                   <ul>
                      <xsl:apply-templates select="$Descendants"/>
                   </ul>
                </xsl:variable>
                <xsl:if test="exsl:node-set($list)//li[b]">
                   <xsl:copy-of select="$list"/>
                </xsl:if>
             </xsl:if>
          </li>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your sample XML, the following is output

    <ul>
       <li>1
          <ul>
             <li>1.1</li>
             <li>1.2
                <ul>
                   <li>
                      <b>1.2.1</b>
                   </li>
                </ul></li>
             <li>1.3</li>
             <li>1.4</li>
          </ul></li>
       <li>2</li>
       <li>3</li>
    </ul>
    

    Note, because I am using Microsoft XML here, the extension namespace is “urn:schemas-microsoft-com:xslt”. For other processors, you will probably have to use “http://exslt.org/common”

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

Sidebar

Related Questions

I need to transform large XML files that have a nested (hierarchical) structure of
I have an xml file in a flat structure. We do not control the
I have a flat table that I'm trying to turn into an xml output
I have a flat structured XML file as below: <rs> <r id=r1 lev=0/> <r
I have several hundred files in a non-flat directory structure. My Makefile lists each
I have simple SSIS package which reads data from flat file and insert into
I have simple SSIS package where I import data from flat file into SQL
I have the following XML: <products> <product> <name>Flat Panel Monitor</name> <code>LS123</code> <attributes> <attribute> <group>Color</group>
We have a xml column in SQL Server 2008. We need to do reporting
I have a flat file with xml data that contains parent and child information

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.