Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: How to convert XML to flat file?

From: Martin Honnen <mahotrash@-----.-->
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="'&#13;&#10;'"/>

   <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/


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