Solved

XSL Transform nested elements into attributes

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
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. 
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

776 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