Avatar of Gerral Hubbard
Gerral Hubbard
Flag for United States of America

asked on 

XSL Transform nested elements into attributes

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!
XML

Avatar of undefined
Last Comment
Gertone (Geert Bormans)

8/22/2022 - Mon