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 December 22nd, 2006, 10:16 AM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML conversion to CSV - Problem with value

I have a XSLT file I'm using to convert XML output to CSV format. It removes extra whitespace and puts in the commas. It expects something like...

<line>
<column>value</column>
<column>value</column>
<column>value</column>
</line>

In one case I need to do some processing on a value so I have (in XML)...

<line>
<column>value</column>
<column>value</column>
<column><type><val>0</val></type></column>
</line>

The XSL looks like...

<xsl:output encoding="iso-8859-1" method="text" indent="yes" standalone="yes"/>
<xsl:strip-space elements='line' />
<xsl:strip-space elements='column' />


<xsl:template match="line">
  <xsl:apply-templates select="column" />
  <xsl:text>
</xsl:text>
</xsl:template>


<xsl:template match="column">
  <xsl:apply-templates select="type" />

  <xsl:value-of select='normalize-space(translate(., "#xA;#xD;", ""))' />
  <xsl:value-of select='../../@csvSeparator' />
</xsl:template>

<xsl:template match="type">
  <xsl:choose>
  <xsl:when test="val=0">dog</xsl:when>
  <xsl:when test="val=1">cat</xsl:when>
  </xsl:choose>
</xsl:template>

Problem = the xsl:choose works properly so it outputs 'dog', but then it immediately output '0' as well. So I get dog0 or cat1. Any idea what could be causing this? Thanks!

 
Old December 22nd, 2006, 10:40 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

What causes the problem is that immediately after doing

<xsl:apply-templates select="type" />

you then do

<xsl:value-of select='normalize-space(translate(., "#xA;#xD;", ""))' />

so you process the contents of the element twice.

Write two separate template rules, one

<xsl:template match="column[type]"

and one

<xsl:template match="column[not(type)]"

to handle the two different formats for column.

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
.mdb to .xls or .csv conversion ramniwas Visual Basic 2005 Basics 5 April 14th, 2008 04:50 PM
.csv to xml conversion balakrishna XML 0 March 5th, 2007 01:07 PM
XML to CSV reloader2 XML 2 September 7th, 2005 08:51 AM
Problem applying simple XSLT to XML to create CSV gregclark XSLT 2 August 25th, 2005 07:30 AM
Problem in Conversion from XML to Excel nabeela_noor XML 1 January 14th, 2005 11:24 AM





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