Solved

XSL Transform nested elements into attributes

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

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 …
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

756 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