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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:44:51+00:00 2026-06-19T00:44:51+00:00

I would like to know how to use xsl templates to transform my xml

  • 0

I would like to know how to use xsl templates to transform my xml document to another xml document with original element hierarchy. I would also like to add some attributes to elements in the new generated XML.

My original XML file looks something like this:

<shop> 
  <product>
    <cookie ID="001">
    <price>2</price>
    </cookie>
  </product>

  <product>
    <bread ID="002">
    <price>5</price>
    </bread>
  </product>

  <product>
    <milk ID="003">
    <price>2</price>
    </milk>
  </product>
</shop>

I would like to transform this to following XML:

<newXML> 
  <newElement>
    <newElement ID="001">
    <newElement price="2"/>
    </newElement>
  </newElement>

  <newElement>
    <newElement ID="002">
    <newElement price="5"/>
    </newElement>
  </newElement>

  <newElement>
    <newElement ID="003">
    <newElement price="2"/>
    </newElement>
  </newElement>
</newXML>

What would be a good way to do this? Can this be done using recursion with templates or is there some better way? I’ve been trying to use following logic:

  1. make a template that creates an element
  2. read current elements ID if it exist and put it to newElement
  3. if current element has a child, apply this template to it (some kind of recursion)

Despite of many attempts I haven’t been able to make this work. Your help would be greatly appreciated!

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

    To do this, you would build upon the XSLT Identity Transform, which is a very common design pattern in XSLT. To start with you just have a template that copies the elements and outputs them exactly as-is]

    <xsl:template match="@*|node()">
       <xsl:copy>
          <xsl:apply-templates select="@*|node()"/>
       </xsl:copy>
    </xsl:template>
    

    What you then do is add extra templates to match your elements, and add codes to rename them, or add attributes as required. For example, to rename the root element you would add the following template:

    <xsl:template match="shop">
      <newXML>
        <xsl:apply-templates select="@*|node()"/>
      </newXML>
    </xsl:template>
    

    You don’t have to use specific element names here either. If you wanted to rename and element with an ID attribute to the same name, you could do something like this

    <xsl:template match="product/*[@ID]">
       <newElement>
          <xsl:apply-templates select="@*|node()"/>
       </newElement>
    </xsl:template>
    

    And in the case of your price element, if you wanted to create an attribute based on the element’s text content, you could add a template like so:

    <xsl:template match="price">
       <newElement price="{.}" />
    </xsl:template>
    

    (Note, this uses Attribute Value Templates to create the attribute here. THis is usually preferred to using xsl:attribute)

    Try this XML for starters:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="xml" indent="yes"/>
    
       <xsl:template match="shop">
          <newXML>
             <xsl:apply-templates select="@*|node()"/>
          </newXML>
       </xsl:template>
    
       <xsl:template match="product">
          <newElement>
             <xsl:apply-templates select="@*|node()"/>
          </newElement>
       </xsl:template>
    
       <xsl:template match="product/*[@ID]">
          <newElement>
             <xsl:apply-templates select="@*|node()"/>
          </newElement>
       </xsl:template>
    
       <xsl:template match="price">
          <newElement price="{.}" />
       </xsl:template>
    
       <xsl:template match="@*|node()">
          <xsl:copy>
             <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
       </xsl:template>
    </xsl:stylesheet>
    

    This outputs the following:

    <newXML>
       <newElement>
          <newElement ID="001">
             <newElement price="2"/>
          </newElement>
       </newElement>
       <newElement>
          <newElement ID="002">
             <newElement price="5"/>
          </newElement>
       </newElement>
       <newElement>
          <newElement ID="003">
             <newElement price="2"/>
          </newElement>
       </newElement>
    </newXML>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use XSL to create excel .xml files that Excel 2003
I would like to know how to use Queue in the best functional way.
I would like to know what code to use to convert a double[] array
I would like to know how could I use JSON for passing data from
I would like to know if I can use the following JQuery function on
I would like to know how can I use tinyint in SQL Server 2005
I would like to know how can I use jQuery to select multiple items
I would like to know a few practical use-cases (if they are not related/tied
I would like to know if it is safe to use the following code
I would like to know: If it's possible to use Lazarus (Pascal) for both

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.