Solved

XSL Transform nested elements into attributes

Posted on 2014-11-10
1
98 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
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"
    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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What XAML would produce this? 3 72
Converting BLOB to XMLTYPE 12 102
How do i send an api key in a request header 3 57
How to obtain string using substring and indexOf function 11 51
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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. 
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

16 Experts available now in Live!

Get 1:1 Help Now