XSL Transform nested elements into attributes

Posted on 2014-11-10
Last Modified: 2014-11-11
I have a object that is using xml for it's menu.  I want to generate the xml from my database, however the object wants its information as attributes rather than elements.

The problem isn't so much as converting elements into attributes, that I can do.  The problem is that the submenus are of unspecified depths and each menu item <Menu> can be either an actual menu item, or parent to a submenu, or both.

Is it possible to do this in Xslt?

Starting XML
    <Title>Main Menu</Title>
      <Title>Title 2</Title>
        <Title>Title 3</Title>
          <Title>Title 4</Title>
      <Title>Title 1</Title>

Desired resulting XML (the extra elements such as ChildID, ParentID, SortOrder can be included, however I'd rather they not be)

   <Menu Title="Main Menu">
      <Menu Title="Title 2" PageURL="">
         <Menu Title="Title 3" PageURL="">
            <Menu Title="Title 4" PageURL=""/>
  <Menu Title="Title 1" PageURL=""/>

Thanks in advance!
Question by:PearlIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 60

Accepted Solution

Geert Bormans earned 500 total points
ID: 40433455
Yes that is possible, easy even

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl=""
    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>
    <xsl:template match="MenuDataSet">
            <xsl:apply-templates select="Menu"/>
    <xsl:template match="Menu">
            <xsl:attribute name="Title">
                <xsl:value-of select="Title"/>
            <xsl:if test="normalize-space(PageURL)">
                <xsl:attribute name="PageURL">
                    <xsl:value-of select="PageURL"/>
            <xsl:apply-templates select="Menu"/>

Open in new window


Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Word files to Wordpress Posts 2 77
Convert XML to excel12book 5 36
XML XSLT If loop 4 38
customizing windows 10 taskbar and startmenu 2 58
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question