Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - XSLT Tranformation --> flattening structure (but with prefixes) >Thread Next - Re: XSLT Tranformation --> flattening structure (but with prefixes) Re: XSLT Tranformation --> flattening structure (but with prefixes)To: NULL Date: 11/5/2007 2:35:00 PM mushimedia@g... wrote: > I have quite a big and complex structure that I need to flatten (the > output of the project requires a flat xml file - don't ask me why) > So I need to flatten the structure, but I also need to use the former > tags-names to name the new structure. Here is an XSLT 1.0 stylesheet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/*"> <xsl:copy> <xsl:apply-templates select=".//*[not(*)]"/> </xsl:copy> </xsl:template> <xsl:template match="*[not(*)]"> <xsl:variable name="new-element-name"> <xsl:for-each select="ancestor-or-self::*[position() != last()]"> <xsl:value-of select="name()"/> <xsl:if test="position() != last()"> <xsl:text>_</xsl:text> </xsl:if> </xsl:for-each> </xsl:variable> <xsl:element name="{$new-element-name}"> <xsl:value-of select="."/> </xsl:element> </xsl:template> </xsl:stylesheet> Result looks like this: <OfferHead> <Offerprice_Quantity_unitCode>m²</Offerprice_Quantity_unitCode> <Offerprice_Quantity_QuantityDescription>ca. vægtet areal</Offerprice_Quantity_QuantityDescription> <Offerprice_Quantity_value>1</Offerprice_Quantity_value> <Offerprice_UnitPrice_Licitation>0</Offerprice_UnitPrice_Licitation> <Offerprice_UnitPrice_Netto>0</Offerprice_UnitPrice_Netto> <Offerprice_UnitPrice_Brutto>0</Offerprice_UnitPrice_Brutto> <Offerprice_UnitPrice_CostSavings>0</Offerprice_UnitPrice_CostSavings> <Offerprice_UnitPrice_ContractPrice>0</Offerprice_UnitPrice_ContractPrice> <Offerprice_Sum_Licitation>0</Offerprice_Sum_Licitation> <Offerprice_Sum_Netto>0</Offerprice_Sum_Netto> <Offerprice_Sum_Brutto>0</Offerprice_Sum_Brutto> <Offerprice_Sum_CostSavings>0</Offerprice_Sum_CostSavings> <Offerprice_Sum_ContractPrice>0</Offerprice_Sum_ContractPrice> <Offerprice_currencyID>DKK</Offerprice_currencyID> <BasisForOffer_BaseIndex>111.19999694824219</BasisForOffer_BaseIndex> <BasisForOffer_CurIndex>111.19999694824219</BasisForOffer_CurIndex> <BasisForOffer_BaseDate>2007-04-10T00:00:00</BasisForOffer_BaseDate> <BasisForOffer_CurDate>2007-04-21T00:00:00</BasisForOffer_CurDate> </OfferHead> which is slightly different from what you posted for the last four elements but I don't understand why you posted e.g. <Offerprice_BasisForOffer_BaseIndex>111.19999694824219</ Offerprice_BasisForOffer_BaseIndex> as those elements do not have a parent element Offerprice. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
