xsl-list
[Top] [All Lists]

RE: flat XML to normal XML

2005-05-16 07:36:09
Borut, hi, sorry to be tedious, but I couldn't walk away...here's input XML (extended with motorcycles, in restitution)

<DataSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <Table>
      <Level>1</Level>
      <Description>Cars</Description>
  </Table>
  <Table>
      <Level>2</Level>
      <Description>BMW</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>316</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>Z4</Description>
  </Table>
  <Table>
      <Level>2</Level>
      <Description>Citroen</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>C2</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>C4 coupe</Description>
  </Table>
  <Table>
      <Level>2</Level>
      <Description>Alfa Romeo</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>156</Description>
  </Table>
  <Table>
      <Level>1</Level>
      <Description>Motorcycles</Description>
  </Table>
  <Table>
      <Level>2</Level>
      <Description>Kawasaki</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>Ninja</Description>
  </Table>
  <Table>
      <Level>2</Level>
      <Description>Suzuki</Description>
  </Table>
  <Table>
      <Level>3</Level>
      <Description>Katana</Description>
  </Table>
</DataSet>

The new XSL (not pretty yet):

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

   <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>

   <xsl:template match="/">
       <NewDataSet>
           <xsl:for-each select="DataSet/Table[Level = 1]">
               <xsl:variable name="t" select="."/>
               <Transport name="{Description}">
                   <xsl:for-each select="following-sibling::Table[
                       Level = 2
                       and (
(. = current()/following-sibling::Table[Level = 1][1]/preceding-sibling::Table) or ($t = ./preceding-sibling::Table[Level = 1][1])
                           )
                       ]">
                       <xsl:variable name="b" select="."/>
                       <Brand name="{Description}">
                           <xsl:for-each select="following-sibling::Table[
                               Level = 3
                               and (
. = current()/following-sibling::Table[Level = 2][1]/preceding-sibling::Table or ($b = ./preceding-sibling::Table[Level = 2][1])
                                   )
                               ]">
                               <Model name="{Description}">
                               </Model>
                           </xsl:for-each>
                       </Brand>
                   </xsl:for-each>
               </Transport>
           </xsl:for-each>
       </NewDataSet>
   </xsl:template>
</xsl:stylesheet>

And the resulting output:

<NewDataSet>
 <Transport name="Cars">
   <Brand name="BMW">
     <Model name="316"/>
     <Model name="Z4"/>
   </Brand>
   <Brand name="Citroen">
     <Model name="C2"/>
     <Model name="C4 coupe"/>
   </Brand>
   <Brand name="Alfa Romeo">
     <Model name="156"/>
   </Brand>
 </Transport>
 <Transport name="Motorcycles">
   <Brand name="Kawasaki">
     <Model name="Ninja"/>
   </Brand>
   <Brand name="Suzuki">
     <Model name="Katana"/>
   </Brand>
 </Transport>
</NewDataSet>

If I find even this to be incorrect I'll let people more immediately capable than I respond to you.

Regards,

--A

_________________________________________________________________
On the road to retirement? Check out MSN Life Events for advice on how to get there! http://lifeevents.msn.com/category.aspx?cid=Retirement


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--