[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: flat XML to normal XML

Subject: Re: [xsl] flat XML to normal XML
From: Borut Bolčina <bob@xxxxxxxx>
Date: Mon, 16 May 2005 17:02:31 +0200
borut bol ina
Aron,

thank you for your solution! I would need to read some XSL books from face to face to solve this. The solution needed just one minor correction:
<Model><xsl:value-of select="Description"/></Model>
otherwise works out of the box. Thanks again to you and others who helped me out!


--Borut

P.S. I see you like bikes ;-)

On 16.5.2005 16:36, Aron Bock wrote:

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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.