Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 24th, 2005, 08:06 AM
Registered User
 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


 
Old August 25th, 2005, 06:39 AM
Registered User
 
Join Date: Aug 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, it seems the issue wasn't the XSLT at all, but the
xmlns="http://tempuri.org/" attribute in the ArrayOfSearchResult element - once I removed this it was fine. Bizarre.

 
Old August 25th, 2005, 07:30 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Not at all bizarre. Including a default namespace declaration such as xmlns="http://tempuri.org/" effectively changes the name of every element in your source document. A stylesheet that matches an <X> element in no namespace won't match an <X> element in the namespace http://tempuri.org/. To do that you need to write

<xsl:stylesheet
  xmlns:t="http://tempuri.org/"

<xsl:template match="t:X">

Similarly all references to names in path expressions have to specify the namespace.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
XML To CSV with XSLT and Java pcroadkill XSLT 3 February 8th, 2012 11:55 AM
Simple XSLT Problem (HELP PLEASE) gaeawalker XSLT 2 October 10th, 2007 10:27 AM
simple xml / xslt example needed badgolfer ASP.NET 1.0 and 1.1 Basics 2 January 21st, 2005 02:10 AM
Applying an XSLT to an XML document (II) Thodoris XML 1 April 28th, 2004 09:10 AM
Applying an XSLT to an XML document dimondwoof XML 1 June 26th, 2003 01:22 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.