Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: XSLT Tranformation --> flattening structure (but with prefixes)

From: Martin Honnen <mahotrash@-----.-->
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/


transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent