Problem applying simple XSLT to XML to create CSV
Hi,
I'm very new to XML/XSLT, and am having a problem converting an XML document to CSV format with an XSLT. The XML is (with blank lines added to make it more legible):
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSearchResult xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<SearchResult>
<UserID>111111</UserID>
<AccountNumber>XXXXX</AccountNumber>
<SourceCompany>Manning Ltd</SourceCompany>
<Title>Mr</Title>
<FirstName>John</FirstName>
<MiddleInitial />
<Surname>Manning</Surname>
<Address1>50 Minchon Place #33-00</Address1>
<PostCode>NE12 42E</PostCode>
</SearchResult>
<SearchResult>
<UserID>222222</UserID>
<AccountNumber>YYYYY</AccountNumber>
<SourceCompany>Mica Ltd</SourceCompany>
<Title>Mr</Title>
<FirstName>Mike</FirstName>
<MiddleInitial />
<Surname>Smythe</Surname>
<Address1>3 Portico Place</Address1>
<PostCode>SW17</PostCode>
</SearchResult>
<SearchResult>
<UserID>333333</UserID>
<AccountNumber>ZZZZZ</AccountNumber>
<SourceCompany>Hansons Ltd</SourceCompany>
<Title>Mr</Title>
<FirstName>Chris</FirstName>
<MiddleInitial />
<Surname>Hanson</Surname>
<Address1>5 Lyon Tce</Address1>
<PostCode />
</SearchResult>
</ArrayOfSearchResult>
and the XSLT (basically a modified document of an example I found on the web):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xslutput method="text"/>
<xsl:template match="/ArrayofSearchResult">
<xsl:apply-templates select="SearchResult"/>
</xsl:template>
<xsl:template match="SearchResult">
<xsl:for-each select="*">
<xsl:value-of select="."/>
<xsl:if test="position() != last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
Using XMLSpy Home Ed., if I apply the XSLT to XML, I get all of the data in one long string, e.g:
111111XXXXXManning LtdMrJohnManning50 Minchon Place #33-00NE12 42E222222YYYYYMica LtdMrMikeSmythe3 Portico PlaceSW17333333ZZZZZHansons LtdMrChrisHanson5 Lyon Tce
whereas I'd like (including placeholders for missing data):
111111,XXXXX,Manning Ltd,Mr,John,,Manning,50 Minchon Place #33-00,NE12 42E
222222,YYYYY,Mica Ltd,Mr,Mike,,Smythe,3 Portico Place,SW17
333333,ZZZZZ,Hansons Ltd,Mr,Chris,,Hanson,5 Lyon Tce,
If I make mods to the XSLT (i.e. comment out lines to see if it makes a difference) I don't appear to get any change to the output.
Am I missing something obvious?
Many Thanks
Greg
|