[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: flat XML to normal XML
Hi, With just the first node commented thus not providing support for handling the transport tag, the following stylesheet produces the output you want. Iam sure this can be extended to handle the transpot tags. Cheers, Omprakash.V XSLT: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:template match="/"> <NewNodeSet> <xsl:apply-templates select="/DataSet/Table[1]"> <xsl:with-param name="level" select="/DataSet/Table/Level"/> </xsl:apply-templates> </NewNodeSet> </xsl:template> <xsl:template match="Table"> <xsl:param name="level"/> <Brand name="{Description}"> <xsl:apply-templates select="following::Table[1]" mode="model"> <xsl:with-param name="level" select="$level"/> </xsl:apply-templates> </Brand> <xsl:apply-templates select="following::Table[Level = number($level)]"> <xsl:with-param name="level" select="$level"/> </xsl:apply-templates> </xsl:template> <xsl:template match="Table" mode="model"> <xsl:param name="level"/> <xsl:if test="not(Level = number($level))"> <Model> <xsl:value-of select="Description"/> </Model> <xsl:apply-templates select="following::Table[1]" mode="model"> <xsl:with-param name="level" select="$level"/> </xsl:apply-templates> </xsl:if> </xsl:template> </xsl:stylesheet> XML File: <?xml version="1.0" encoding="UTF-8"?> <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> ... ... </DataSet> Output: <?xml version="1.0" encoding="UTF-8"?> <NewNodeSet> <Brand name="BMW"> <Model>316</Model> <Model>Z4</Model> </Brand> <Brand name="Citroen"> <Model>C2</Model> <Model>C4 coupe</Model> </Brand> <Brand name="Alfa Romeo"> <Model>156</Model> <Model>Motorcycles</Model> </Brand> <Brand name="Alfa Romeo"> <Model>156</Model> <Model>Motorcycles</Model> </Brand> </NewNodeSet> Borut Bolhina <bob@xxxxxxxx> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx cc: (bcc: omprakash.v/Polaris) 05/16/2005 03:54 Subject: flat XML to normal XML PM Please respond to xsl-list Hello list, I wish to transform an xml which has level information in each item (Table) to xml which has hierarchy as stated in those item's element. <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> ... ... </DataSet> Target xml should look something like this: <NewDataSet> <Transport name="Cars"> <Brand name="BMW"> <Model>316</Model> <Model>Z4</Model> </Brand> <Brand name="Citroen"> <Model>C2</Model> <Model>C4 coupe</Model> </Brand> <Brand name="Alfa Romeo"> <Model>156</Model> </Brand> </Transport> <Transport name="Motorcycle"> ... </Transport> </NewDataSet> So, based on the Level element in the source xml for each Table entry, an appropriate subtree must be constructed and inserted in target xml. Levels are ordered following this pattern: 1,2,3,3,...,3,2,3,...,3,...,1,2,3,3,...,2,3,...,3 Busting my head over this for two days now, help appreciated. If only I could find the guy who created this "flat" xml...$%##@@! --Bob This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited. Visit Us at http://www.polaris.co.in
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|