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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:48:33+00:00 2026-06-06T19:48:33+00:00

I am a beginner and want to learn XSLT. I came upon an issue

  • 0

I am a beginner and want to learn XSLT. I came upon an issue converting an input XML file to another XML file using XSLT.

My input XML file:

<album>
<album_num>hi.hello</album_num>
<album_name>Cocktail</album_name>
</album>
<album>
<album_num>hey.hello</album_num>
<album_name>Mocktail</album_name>
</album>
<album>
<album_num>hey.mello</album_num>
<album_name>Monkeytail</album_name>
</album>
<album>
<album_num>hey.yellow</album_num>
<album_name>Donkeytail</album_name>
</album>
<album>
<album_num>swallow</album_num>
<album_name>abc</album_name>
</album>

I would like to get an output XML file like this:

<album>
<album_num>
<hi>
<hello>cocktail</hello>
</hi>
</album_num>
<album_num>
<hey>
<hello>MockTail</hello>
<mello>Monkeytail</mello>
<yellow>Donkeytail</yellow>
</hey>
</album_num>
<album_num>
<swallow>abc</swallow>
</album_num>
</album>

I tried the first part by creating variables, but had an issue with merging the similar elements under one element. Any code could help me learn.

My code:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<album>
<xsl:variable name="fstval" select='substring-before(//album/album_num,".")'/>
<xsl:variable name="secval" select='substring-after(//album/album_num,".")'/>
<xsl:variable name="valtoappend" select='//album/album_name'/>
<album_num>
<xsl:element name="{$fstval}">
<xsl:element name="{$secval}">
<xsl:value-of select="$valtoappend"/>
</xsl:element>
</xsl:element>
</xsl:for-each>
</album_num>
</album>
</xsl:template>
</xsl:stylesheet>
  • 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-06T19:48:36+00:00Added an answer on June 6, 2026 at 7:48 pm

    This transformation:

    <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:key name="kAlbumByChildName" match="album" use="name(album_num/*[1])"/>
    
     <xsl:template match="/">
      <xsl:variable name="vrtfPass1">
        <xsl:apply-templates/>
      </xsl:variable>
    
      <xsl:apply-templates mode="pass2" select=
      "ext:node-set($vrtfPass1)/*
               [generate-id()
               =
                generate-id(key('kAlbumByChildName', name(album_num/*[1]))[1])
               ]
      "/>
     </xsl:template>
    
     <xsl:template match="album">
      <album>
       <album_num>
           <xsl:element name="{substring-before(album_num, '.')}">
             <xsl:element name="{substring-after(album_num, '.')}">
               <xsl:value-of select="album_name"/>
             </xsl:element>
           </xsl:element>
       </album_num>
      </album>
     </xsl:template>
    
     <xsl:template match="album" mode="pass2">
      <album>
       <album_num>
            <xsl:apply-templates select="*/*[1]" mode="pass2"/>
        </album_num>
      </album>
     </xsl:template>
    
     <xsl:template match="album_num/*" mode="pass2">
      <xsl:copy>
       <xsl:copy-of select="key('kAlbumByChildName', name())/*/*/*"/>
      </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the following document (the provided XML fragment wrapped in a single top element to make it a well-formed XML document):

    <t>
        <album>
            <album_num>hi.hello</album_num>
            <album_name>Cocktail</album_name>
        </album>
        <album>
            <album_num>hey.hello</album_num>
            <album_name>Mocktail</album_name>
        </album>
        <album>
            <album_num>hey.mello</album_num>
            <album_name>Monkeytail</album_name>
        </album>
        <album>
            <album_num>hey.yellow</album_num>
            <album_name>Donkeytail</album_name>
        </album>
    </t>
    

    produces the wanted, correct result:

    <album>
       <album_num>
          <hi>
             <hello>Cocktail</hello>
          </hi>
       </album_num>
    </album>
    <album>
       <album_num>
          <hey>
             <hello>Mocktail</hello>
             <mello>Monkeytail</mello>
             <yellow>Donkeytail</yellow>
          </hey>
       </album_num>
    </album>
    

    Explanation:

    This is a two-pass transformation. The result of the first pass is:

    <album>
       <album_num>
          <hi>
             <hello>Cocktail</hello>
          </hi>
       </album_num>
    </album>
    
    <album>
       <album_num>
          <hey>
             <hello>Mocktail</hello>
          </hey>
       </album_num>
    </album>
    
    <album>
       <album_num>
          <hey>
             <mello>Monkeytail</mello>
          </hey>
       </album_num>
    </album>
    
    <album>
       <album_num>
          <hey>
             <yellow>Donkeytail</yellow>
          </hey>
       </album_num>
    </album>
    

    The second pass is a standard Muenchian grouping.

    Update:

    Two days after asking this question and receiving a correct answer, the OP has changed the source XML document and wanted result.

    This slightly modified transformation:

    <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:key name="kAlbumByChildName" match="album" use="name(album_num/*[1])"/>
    
         <xsl:template match="/">
          <xsl:variable name="vrtfPass1">
            <xsl:apply-templates/>
          </xsl:variable>
    
          <xsl:apply-templates mode="pass2" select=
          "ext:node-set($vrtfPass1)/*
                   [generate-id()
                   =
                    generate-id(key('kAlbumByChildName', name(album_num/*[1]))[1])
                   or
                    not(album_num/*)
                   ]
          "/>
    
         </xsl:template>
    
         <xsl:template match="album[contains(album_num, '.')]">
          <album>
           <album_num>
               <xsl:element name="{substring-before(album_num, '.')}">
                 <xsl:element name="{substring-after(album_num, '.')}">
                   <xsl:value-of select="album_name"/>
                 </xsl:element>
               </xsl:element>
           </album_num>
          </album>
         </xsl:template>
    
         <xsl:template match="album">
          <album>
           <album_num>
                 <xsl:element name="{album_num}">
                   <xsl:value-of select="album_name"/>
                 </xsl:element>
           </album_num>
          </album>
         </xsl:template>
    
         <xsl:template match="album" mode="pass2">
          <album>
           <album_num>
                <xsl:apply-templates select="*/*[1]" mode="pass2"/>
            </album_num>
          </album>
         </xsl:template>
    
         <xsl:template match="album_num/*" mode="pass2">
          <xsl:copy>
           <xsl:copy-of select="self::*[not(*)]/text()|key('kAlbumByChildName', name())/*/*/*"/>
          </xsl:copy>
         </xsl:template>
    </xsl:stylesheet>
    

    when applied on the new version of the XML document:

    <t>
        <album>
            <album_num>hi.hello</album_num>
            <album_name>Cocktail</album_name>
        </album>
        <album>
            <album_num>hey.hello</album_num>
            <album_name>Mocktail</album_name>
        </album>
        <album>
            <album_num>hey.mello</album_num>
            <album_name>Monkeytail</album_name>
        </album>
        <album>
            <album_num>hey.yellow</album_num>
            <album_name>Donkeytail</album_name>
        </album>
        <album>
            <album_num>swallow</album_num>
            <album_name>abc</album_name>
        </album>
    </t>
    

    produces the new wanted result:

    <album>
       <album_num>
          <hi>
             <hello>Cocktail</hello>
          </hi>
       </album_num>
    </album>
    <album>
       <album_num>
          <hey>
             <hello>Mocktail</hello>
             <mello>Monkeytail</mello>
             <yellow>Donkeytail</yellow>
          </hey>
       </album_num>
    </album>
    <album>
       <album_num>
          <swallow>abc</swallow>
       </album_num>
    </album>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm beginner in MVC3, and I want to get a value from an another
I'm an absolute beginner using IDLE (Python 2.6.4) to learn the basics. I recently
I'm a complete beginner when it comes to ASP.NET but I want to learn
I am a beginner in python. For practice reasons I want to learn how
I've just started to learn xml, so i'm a beginner in this domain. I
I want to learn WPF using C# properly. I read basic concept of WPF
I am beginner of Android OpenGL I want to learn and make Live wallpaper
Could someone please help me in here, I'm just a beginner who want to
I am a PHP beginner and I want to do the following: I want
I am beginner at Sharepoint and i want to make a small application on

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.