Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > BOOK: XSLT Programmer's Reference, 2nd Edition
BOOK: XSLT Programmer's Reference, 2nd Edition
This is the forum to discuss the Wrox book XSLT: Programmer's Reference, 2nd Edition by Michael Kay; ISBN: 9780764543814
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: XSLT Programmer's Reference, 2nd Edition 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 20th, 2014, 12:04 AM
Friend of Wrox
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts
Default escape double quote into two double quotes

I need to create a CSV file from some XML. I can use XSLT 2.0.

I am placing quotes around my fields as some of them contain commas.

But in some of my XML fields I have XML like this ->

<linkHtml href="#TRIALS">CLINICAL EFFICACY TRIALS</linkHtml>

I need to somehow escape each double quote into two double quotes or similar so that Excel will read the CSV file properly and not mess up all my columns.

This is the XSLT code that matches on the element.

<xsl:template match="hl7:linkHtml | linkHtml">
<a href="{@href}">

Does anyone have a suggestion on how to approach this?

Regards, John.
The Following User Says Thank You to JohnBampton For This Useful Post:
Old August 20th, 2014, 01:01 AM
Friend of Wrox
Join Date: Feb 2009
Posts: 119
Thanks: 25
Thanked 3 Times in 3 Posts

I figured it out myself. The code was like this:

<xsl:character-map name="a">
<xsl:output-character character="&lt;" string="&lt;"/>
<xsl:output-character character="&gt;" string="&gt;"/>

<xsl:output omit-xml-declaration="yes" indent="no" encoding="UTF-16" use-character-maps="a"></xsl:output>

<xsl:template match="hl7:linkHtml | linkHtml">
<xsl:for-each select="attribute::*">
<xsl:text> </xsl:text>
<xsl:value-of select="name(.)"></xsl:value-of>
<xsl:value-of select="."></xsl:value-of>
<xsl:apply-templates mode="serialize"/>
Old August 20th, 2014, 03:37 AM
mhkay's Avatar
Wrox Author
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts

Why are you using the XML output method to generate CSV? If you used the text output method, you wouldn't need the character maps, because angle brackets in the result tree wouldn't be escaped. Add method="text" to your xsl:output.
Michael Kay
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference

Similar Threads
Thread Thread Starter Forum Replies Last Post
why extra double quote and dot ? help me ! sandeepgreaternoida BOOK: Professional PHP 5 ISBN: 978-0-7645-7282-1 1 May 8th, 2009 03:18 PM
escape double quote inside JavaScript mister_mister XSLT 5 January 23rd, 2008 01:56 PM
escape character for double quotes Andy dg C# 2005 3 March 15th, 2007 10:51 PM
displaying single quotes and double quotes ren_123 Classic ASP Databases 2 February 22nd, 2006 02:17 PM
Double Quotes and Single Quotes Problem phungleon Classic ASP Basics 7 May 27th, 2004 01:44 PM

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