Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - How to convert XML to flat file? >Thread Next - Re: How to convert XML to flat file? Re: How to convert XML to flat file?To: NULL Date: 12/14/2007 6:50:00 PM anywherenotes@g... wrote: > Is there a simple (to implement) way of converting XML file to a flat > file. > > I would like to be able to convert something like this: > <first> > <second name="hi">bye</second> > first value > <third><fourth>fourth value</fourth></third> > </first> > > into something similar to: > /first/second/name=hi > /first/second=bye > /first=first value > /first/third/fourth=fourth value > > The important thing is that I want to be able to convert any XML, > without knowing the elments/attributes into a flat file. > To do it in complex way I can use SAX to parse arguments, and push/pop > element names as they are being processed, and once I get to attribute/ > element value, print out the current stack. It's doable, but is there > an easy way to do something maybe using in XSL? Here is an XSLT 1.0 stylesheet that produces the wanted result (besides prefixing attribute name (e.g. name) with '@' (e.g. @name) in the output: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="lb" select="' '"/> <xsl:output method="text"/> <xsl:template match="/"> <xsl:apply-templates select="//@* | //text()[normalize-space()]"/> </xsl:template> <xsl:template match="@*"> <xsl:for-each select="./../ancestor-or-self::*"> <xsl:value-of select="concat('/', name())"/> </xsl:for-each> <xsl:value-of select="concat('/@', name(), '=', ., $lb)"/> </xsl:template> <xsl:template match="text()"> <xsl:for-each select="ancestor::*"> <xsl:value-of select="concat('/', name())"/> </xsl:for-each> <xsl:value-of select="concat('=', normalize-space(), $lb)"/> </xsl:template> </xsl:stylesheet> So result is /first/second/@name=hi /first/second=bye /first=first value /first/third/fourth=fourth value -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
