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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:25:03+00:00 2026-05-25T19:25:03+00:00

I’ve been trying to transform some simple XML into another simple XML using XSLT.

  • 0

I’ve been trying to transform some simple XML into another simple XML using XSLT. I am new to XSLT, so if someone could give me an example I will expand on it.

I have arbitrary XML files: e.g

<element>
 <child_element>
  <grandchild_element>
     only one
  </grandchild_element>
 </child_element>
 <child_element>
  <grandchild_element>
     one
  </grandchild_element>
  <grandchild_element>
     two
  </grandchild_element>
 </child_element>
</element>

From which I want to produce:

<tree>
 <item class="element" id="1">
  <item class="child_element" id="11">
   <item class="grandchild_element" id="111" value="only one"/>
  </item>
  <item class="child_element" id="12">
   <item class="grandchild_element" id="121" value="only one"/>
   <item class="grandchild_element" id="122" value="only one"/>
  </item>
 </item>
</tree>

Thanks!

  • 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-25T19:25:04+00:00Added an answer on May 25, 2026 at 7:25 pm

    One of the simplest/shortest solutions (only 3 templates no modes, no axes, no count(), only a single xsl:attribute) that is at the same time most generic (works with any element names):

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="/">
      <tree>
       <xsl:apply-templates/>
      </tree>
     </xsl:template>
    
     <xsl:template match="*">
      <xsl:variable name="vNumber">
       <xsl:number count="*" level="multiple"/>
      </xsl:variable>
    
      <item class="{name()}"
            id="{translate($vNumber, '.', '')}">
       <xsl:apply-templates/>
      </item>
     </xsl:template>
    
     <xsl:template match="text()">
      <xsl:attribute name="value">
       <xsl:value-of select="normalize-space()"/>
      </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the provided XML document:

    <element>
     <child_element>
      <grandchild_element>
         only one
      </grandchild_element>
     </child_element>
     <child_element>
      <grandchild_element>
         one
      </grandchild_element>
      <grandchild_element>
         two
      </grandchild_element>
     </child_element>
    </element>
    

    the wanted, correct result is produced:

    <tree>
       <item class="element" id="1">
          <item class="child_element" id="11">
             <item class="grandchild_element" id="111" value="only one"/>
          </item>
          <item class="child_element" id="12">
             <item class="grandchild_element" id="121" value="one"/>
             <item class="grandchild_element" id="122" value="two"/>
          </item>
       </item>
    </tree>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been trying to transform my XML documents to PDF through Apache FOP, but
I've been trying to iterate through an XML file using XSL , however I'm
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,
I am writing an application that converts an ajax xml response into html using
I've been trying to set up some sort of geometry batching for a week
I'm trying to transform a table to an XML struture and I want one
I've been trying to create an xslt template, but it keeps silently failing like
I have been trying to create rotating object using jQuery and without any other
I'm looking to do some really simply XSLT on my XML document, which has
Been trying to find a working implementation of a WPF listview (or listbox) where

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.