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 01: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}">
<xsl:apply-templates></xsl:apply-templates>
</a>
</xsl:template>

Does anyone have a suggestion on how to approach this?

Regards, John.

JohnBampton August 20th, 2014 02: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:character-map>

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


<xsl:template match="hl7:linkHtml | linkHtml">
<xsl:text>&lt;a</xsl:text>
<xsl:for-each select="attribute::*">
<xsl:text> </xsl:text>
<xsl:value-of select="name(.)"></xsl:value-of>
<xsl:text>=""</xsl:text>
<xsl:value-of select="."></xsl:value-of>
<xsl:text>""</xsl:text>
</xsl:for-each>
<xsl:text>&gt;</xsl:text>
<xsl:apply-templates mode="serialize"/>
<xsl:text>&lt;/a</xsl:text>
<xsl:text>&gt;</xsl:text>
</xsl:template>

mhkay August 20th, 2014 04: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 11:14 AM.

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