Solved

XSL Transform nested elements into attributes

Posted on 2014-11-10
1
96 Views
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
<MenuDataSet>
  <Menu>
    <ChildID>0</ChildID>
    <ParentID></ParentID>
    <Title>Main Menu</Title>
   <PageURL></PageURL>
   <Menu>
      <ChildID>2</ChildID>
      <ParentID>0</ParentID>
      <Title>Title 2</Title>
      <PageURL>pageurl2.com</PageURL>
      <SortOrder>3</SortOrder>
      <Menu>
        <ChildID>3</ChildID>
        <ParentID>2</ParentID>
        <Title>Title 3</Title>
        <PageURL>Pageurl3.com</PageURL>
        <SortOrder>9</SortOrder>
        <Menu>
          <ChildID>4</ChildID>
          <ParentID>3</ParentID>
          <Title>Title 4</Title>
          <PageURL>Pageurl4.com</PageURL>
          <SortOrder>1</SortOrder>
        </Menu>
      </Menu>
    </Menu>
    <Menu>
      <ChildID>1</ChildID>
      <ParentID>0</ParentID>
      <Title>Title 1</Title>
      <PageURL>Pageurl1.com</PageURL>
      <SortOrder>4</SortOrder>
      <BaseModel>AX25L</BaseModel>
    </Menu>
  </Menu>
</MenuDataSet>

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

<MenuDataSet>
   <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>
      </Menu>
  </Menu>
  <Menu Title="Title 1" PageURL="pagurl1.com"/>
</MenuDataSet>

Thanks in advance!
0
Comment
Question by:PearlIT
1 Comment
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
Comment Utility
Yes that is possible, easy even

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

Open in new window

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

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…
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. 
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now