p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: XSLT Programmer's Reference, 2nd Edition (http://p2p.wrox.com/book-xslt-programmers-reference-2nd-edition-52/)
-   -   escape double quote into two double quotes (http://p2p.wrox.com/book-xslt-programmers-reference-2nd-edition/93428-escape-double-quote-into-two-double-quotes.html)

JohnBampton August 20th, 2014 12:04 AM

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.

JohnBampton August 20th, 2014 01:01 AM

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"/>

mhkay August 20th, 2014 03:37 AM

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.

All times are GMT -4. The time now is 09:00 PM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.