XSL Transform nested elements into attributes

Posted on 2014-11-10
Medium Priority
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="pageurl2.com">
         <Menu Title="Title 3" PageURL="pageurl3.com">
            <Menu Title="Title 4" PageURL="pageurl4.com"/>
  <Menu Title="Title 1" PageURL="pagurl1.com"/>

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 2000 total points
ID: 40433455
Yes that is possible, easy even

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    <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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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