I need transform the following XML to CSV. Can some one suggest how do i write an XSLT to transform.


XML is

<Transaction TransactionID="60" EventDate="2009-08-17">
<VehicleBuilt InstallationType="factory">
<Vehicle>
<Receiver RSN="489823">
<Module>
<AddressableElement ESN="99409230923">
<Capability Type="Audio">
<Subscription>
<SalesCode>DCC12MO</SalesCode>
<Type>Audio</Type>
</Subscription>
</Capability>
</AddressableElement>
<AddressableElement ESN="12192019">
<Capability Type="Traffic">
</Capability>
</AddressableElement>
<AddressableElement ESN="1209010290192">
<Capability Type="Video">
</Capability>
</AddressableElement>
</Module>
</Receiver>
<VIN>1209091029012</VIN>
<Make>DODGE</Make>
<Model>98</Model>
<Year>2009</Year>
<ProductionDate>2009-07-02</ProductionDate>
<IntendedCountry>US</IntendedCountry>
<BodyStyleDescription>Ram1500 Crew Cab 4x2</BodyStyleDescription>
</Vehicle>
</VehicleBuilt>
</Transaction>

Are you trying to output all fields in this XML formatted document to CSV?

Yes, i am trying to get all the fields.

Yes, i am trying to get all the fields.

What XSLT transform you using? What language (i.e Java, Perl, .NET, etc..)?

What XSLT transform you using? What language (i.e Java, Perl, .NET, etc..)?

I am using Java, but as an initail conversion i am using XML Spy to do the conversion. Your hep will be of great use.

How do you handle the AddressableElement ? In your example there are two. Are there always two and appear both on one line ? Or are other situations possible.

Anyway, start with this:

<xsl:template match="Transaction">
  <xsl:value-of select="@TransactionID" disable-output-escaping="yes" />,
  <xsl:value-of select="@EventDate" disable-output-escaping="yes" />,
</xsl:template>

How do you handle the AddressableElement ? In your example there are two. Are there always two and appear both on one line ? Or are other situations possible.

Anyway, start with this:

<xsl:template match="Transaction">
  <xsl:value-of select="@TransactionID" disable-output-escaping="yes" />,
  <xsl:value-of select="@EventDate" disable-output-escaping="yes" />,
</xsl:template>

Thanks for your response @Pritaeas.

The AddressableElement tag may be 1/2/3 depending on the data, so no fixed thing that it would come in. Same is the case with the Capability tag.

I meant AddressableElement would be repeated multiple times but not greater than 3 times.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="text"/>
	<xsl:template match="/">
		<xsl:apply-templates select="Transaction"/>
	</xsl:template>
	<xsl:template match="Transaction">
		<xsl:value-of select="concat(@TransactionID,',',@EventDate,',')"/>
		<xsl:apply-templates select="VehicleBuilt/Vehicle"/>
	</xsl:template>	
	<xsl:template match="Vehicle">
		<xsl:apply-templates select="Receiver"/>
		<xsl:value-of select="concat(VIN,',',Make,',',Model,',',Year,',',ProductionDate,',',IntendedCountry,',',BodyStyleDescription)"/>
	</xsl:template>
	<xsl:template match="Receiver">
		<xsl:value-of select="concat(@RSN,',')"/>
		<xsl:apply-templates select="Module"/>
	</xsl:template>
	<xsl:template match="Module">
		<xsl:apply-templates select="AddressableElement"/>
	</xsl:template>
	<xsl:template match="AddressableElement">
		<xsl:value-of select="concat(@ESN, ',',Capability/@Type,',',Capability/Subscription/SalesCode,',',Capability/Subscription/Type,',')"/>
	</xsl:template>
</xsl:stylesheet>

output

60,2009-08-17,489823,99409230923,Audio,DCC12MO,Audio,12192019,Traffic,,,1209010290192,Video,,,1209091029012,DODGE,98,2009,2009-07-02,US,Ram1500 Crew Cab 4x2
This article has been dead for over six months. Start a new discussion instead.