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 May 20th, 2010, 05:55 PM
Authorized User
 
Join Date: Sep 2008
Posts: 37
Thanks: 5
Thanked 0 Times in 0 Posts
Default How to escape the commas in an element node in a CSV?

****Note: title for this question should be...


********************************************
How to escape the commas in an element node in a CSV?
********************************************

I was wondering what the logic for encapsulating element nodes in an XSL is. Specifically, the XSL i wrote creates a CSV file and i'm trying to encapsulate all of the data elements in to double-quotes.

Using: XSLT 1.0, XPath 1.0

Source XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<main>
      <date year="2010" month="3" date="31" day="3"/>
      <time hour="10" minute="38" second="55" timezone="Central"/>
         <person first_name = "Bob" last_name = "Jackson">
          <contents>
               <content text="A very long sentence that will continue going for a very long time, possibly, it could be over a hundred words long and include multiple commas.../>
          </contents>
         </person>
        <person first_name = "David" last_name = "Lee">
          <contents>
               <content text="A very long sentence that will continue going for a very long time, possibly, it could be over a hundred words long and include multiple commas.../>
          </contents>
         </person>
</main>
</xml>
XSL
Code:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
    <xsl:template match="/"> 
    
    <xsl:variable name="delim" select="','"/>

     <outputfile>TEST_FILE.CSV</outputfile>&newline;
    
	<xsl:for-each select="//person">
		<xPerson= concat('@first_name, '  ',@last_name')"/>

		<xsl:value-of select="concat($xPerson, $delim)"/>
		<xsl:for-each select="contents/content">
			<xsl:value-of select="concat(@text,&quot;$delim&quot;)"/>
		</xsl:for-each>
		
		&newline;
	</xsl:for-each>

          </xsl:template>
</xsl:stylesheet>
The &quot; above doesn't seem to be doing the trick.

Additionally, currently the XSL escapes from escaping ampersands (eg. when printing out a some thing like TEST&TEST... it outputs TEST&TEST). Is there a proper encoding that will allow this to happen automatically?

Last edited by vb89; May 24th, 2010 at 02:04 PM..
 
Old May 21st, 2010, 02:33 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try with the below:
Code:
<xsl:output method="xml" indent="no" omit-xml-declaration="yes"/>
    <xsl:template match="/"> 
    
    <xsl:variable name="delim" select="','"/>
 <xsl:variable name="quot">"</xsl:variable>
     <outputfile>TEST_FILE.CSV</outputfile>
    
 <xsl:for-each select="//person">
  <xsl:variable name="xPerson" select="concat(@first_name, '  ',@last_name)"/>
  <xsl:value-of select="concat($xPerson, $delim)"/>
  <xsl:for-each select="contents/content">
   <xsl:value-of select='concat(@text,$quot,$delim,$quot)'/>
  </xsl:for-each>
  
  
 </xsl:for-each>
          </xsl:template>
Hope the above would help.
__________________
Rummy
 
Old May 24th, 2010, 11:20 AM
Authorized User
 
Join Date: Sep 2008
Posts: 37
Thanks: 5
Thanked 0 Times in 0 Posts
Default

So I found a custom built function that will do what I need -- I think

Code:
    <xsl:template name="display_csv_field">
       <xsl:param name="field"/>
		   <xsl:choose>
				 <xsl:when test="contains($field, ',' )">
					 <xsl:text>"</xsl:text>
					 <xsl:value-of select="$field"/>
					<xsl:text>"</xsl:text>
				 </xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="$field" />
				 </xsl:otherwise>
		</xsl:choose>
	</xsl:template>
However, i'm not very familiar with using templates, can someone please help me apply the template above to my existing XSL. I want to apply the above function to each instance of the content element-node in the XML.

Thanks!

Last edited by vb89; May 24th, 2010 at 11:25 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 9: Element[user control] is not a known element Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 20th, 2009 07:31 AM
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Pushing particular Nodes inside an element ROCXY XSLT 2 January 3rd, 2006 12:21 PM
Grouping stand alone nodes inside an element Bala007 XSLT 2 December 30th, 2005 02:25 PM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM





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