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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:12:55+00:00 2026-06-14T00:12:55+00:00

This is an extension to the question I asked earlier – XSLT 1.0 Grouping

  • 0

This is an extension to the question I asked earlier – XSLT 1.0 Grouping with multiple elements with same name
The output format has changed and hence reposting.

I have an XML that looks like –

<resultset>
    <hit>
        <content>
            <ITEM>
                <TITLE>Office Cleaning1</TITLE>
                <DESCRIPTION>blah blah blah</DESCRIPTION>
                <Hierarchy>level1A~level2A~level3A</Hierarchy>
                <Hierarchy>level1B~level2B~level3B</Hierarchy>
            </ITEM>
        </content>
    </hit>
    <hit>
        <content>
            <ITEM>
                <TITLE>Office Cleaning2</TITLE>
                <DESCRIPTION>blah blah blah</DESCRIPTION>
                <Hierarchy>level1A~level2A~level3B</Hierarchy>
            </ITEM>
        </content>
    </hit>
    <hit>
        <content>
            <ITEM>
                <TITLE>Office Cleaning3</TITLE>
                <DESCRIPTION>blah blah blah</DESCRIPTION>
                <Hierarchy>level1A~level2B~level3C</Hierarchy>
            </ITEM>
        </content>
    </hit>
    <hit>
        <content>
            <ITEM>
                <TITLE>Office Cleaning4</TITLE>
                <DESCRIPTION>blah blah blah</DESCRIPTION>
                <Hierarchy>level1A~level2B~level3B</Hierarchy>
                <Hierarchy>level1A~level2B~level3C</Hierarchy>
            </ITEM>
        </content>
    </hit>
    <hit>
        <content>
            <ITEM>
                <TITLE>Office Cleaning5</TITLE>
                <DESCRIPTION>blah blah blah</DESCRIPTION>
                <Hierarchy>level1B~level2B~level3B</Hierarchy>
            </ITEM>
        </content>
    </hit>
</resultset>

Note that there are multiple hierarchy elements which is a concatenated string of level1~level2~level3 I am looking to transform this into something like this –

<TREE>
<LEVELS>
<LEVEL1 name="level1A">
 <LEVEL2 name="level2A">
   <LEVEL3 name="level3A">
      <ITEM Name="Office Cleaning1"/>
   </LEVEL3>
   <LEVEL3 name="level3B">
      <ITEM Name="Office Cleaning2"/>
   </LEVEL3>
 </LEVEL2>
 <LEVEL2 name="level2B">
   <LEVEL3 name="level3B">
        <ITEM Name="Office Cleaning4"/>
   </LEVEL3>
   <LEVEL3 name="level3C">
      <ITEM Name="Office Cleaning3"/>
      <ITEM Name="Office Cleaning4"/>
   </LEVEL3>
 </LEVEL2>
</LEVEL1>
<LEVEL1 name="level1B">
  <LEVEL2 name="level2B">
    <LEVEL3 name="level3B">
        <ITEM Name="Office Cleaning1"/>
        <ITEM Name="Office Cleaning5"/>
    </LEVEL3>
  </LEVEL2>
</LEVEL1>
</TREE>

Basically each item has multiple hierarchy associated with it. I need to group them together and each levels to be grouped together as well.

I can actually change the values in the HIERARCHY element in the input XML to any format that might be easier to extract. For e.g. I can make it look like LEVEL1:level1A~LEVEL2:level2A~LEVEL3:level3A
but I cannot add new elements.

  • 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-14T00:12:57+00:00Added an answer on June 14, 2026 at 12:12 am

    I got it working myself. Ignore the extra namespaces. I edited the hierarchy field to have format of level1:value~level2:value~level3:value for ease of substring.

    I am sure there is a better way but this works for me.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:autn="http://schemas.autonomy.com/aci/">
    <xsl:output method="xml" omit-xml-declaration="yes"/>
    
     <xsl:strip-space elements="*"/>
    
      <xsl:key name="TOPLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL1:'),'~')"/>
      <xsl:key name="MIDLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL2:'),'~')"/>
      <xsl:key name="BOTTOMLEVEL" match="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY" use="substring-before(substring-after(.,'LEVEL3:'),'~')"/>
    
    
     <xsl:template match="/">
     <TREE>
        <xsl:for-each select="autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[generate-id() = generate-id(key('TOPLEVEL',substring-before(substring-after(.,'LEVEL1:'),'~') )[1])]">
        <xsl:variable name="TOP" select="substring-before(substring-after(.,'LEVEL1:'),'~')"/>
         <LEVEL1>
            <xsl:attribute name="name"><xsl:value-of select="substring-after($TOP,'+')"/></xsl:attribute>
            <xsl:attribute name="id"><xsl:value-of select="substring-before($TOP,'+')"/></xsl:attribute>
                <xsl:for-each select="//autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP and generate-id() = generate-id(key('MIDLEVEL',substring-before(substring-after(.,'LEVEL2:'),'~') )[1])]">
                <xsl:variable name="MID" select="substring-before(substring-after(.,'LEVEL2:'),'~')"/>
                    <LEVEL2>
                        <xsl:attribute name="name"><xsl:value-of select="substring-after($MID,'+')"/></xsl:attribute>
                        <xsl:attribute name="id"><xsl:value-of select="substring-before($MID,'+')"/></xsl:attribute>
                        <xsl:for-each select="//autnresponse/responsedata/autn:hit/autn:content/DOCUMENT/HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP  and substring-before(substring-after(.,'LEVEL2:'),'~')=$MID and generate-id() = generate-id(key('BOTTOMLEVEL',substring-before(substring-after(.,'LEVEL3:'),'~') )[1])]">
                        <xsl:variable name="BOTTOM" select="substring-before(substring-after(.,'LEVEL3:'),'~')"/>
                        <LEVEL3>
                            <xsl:attribute name="name"><xsl:value-of select="substring-after($BOTTOM,'+')"/></xsl:attribute>
                            <xsl:attribute name="id"><xsl:value-of select="substring-before($BOTTOM,'+')"/></xsl:attribute>
                            <xsl:apply-templates select="//HIERARCHY[substring-before(substring-after(.,'LEVEL1:'),'~')=$TOP  and substring-before(substring-after(.,'LEVEL2:'),'~')=$MID and substring-before(substring-after(.,'LEVEL3:'),'~')=$BOTTOM]"/>
                        </LEVEL3>
                        </xsl:for-each>
                    </LEVEL2>
                </xsl:for-each>
        </LEVEL1>
        </xsl:for-each>
     </TREE>
     </xsl:template>
      <xsl:template match="HIERARCHY">
        <ITEM>
            <xsl:attribute name="name"><xsl:value-of select="../DRETITLE"/></xsl:attribute>
            <xsl:attribute name="id"><xsl:value-of select="../ID"/></xsl:attribute>
        </ITEM>
      </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an extension of a question I asked earlier: Python Delegate Pattern
Note: This is an extension of an earlier question I asked here: Do additional
Note: This is an extension of an earlier question I asked here: Do additional
I have asked this question earlier, please click here ! this is extension to
This is an extension for this question asked an hour ago. We cannot modify
This is an extension of the question I asked here I have a relationship
This is an extension to the question I asked here: Get text from clipboard
This is an extension of a question I previously asked here . Long story
This is an extension to a question which I asked some time ago. I
This is an extension / next step of this question I asked a few

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.