Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put double quotes around a value in XSLT

I have an XSLT template which is outputting address data as CSV. I want to wrap some of the values in double quotes because they contain commas as part of the address data.

My problem is that after the transform, the double quotes are missing. I can get the processor to insert almost anything, but not the double quotes I want.

Here is some sample code:

<xsl:value-of select="./*/*/LotNumber"/>
<xsl:value-of select="$delim"/>
<xsl:value-of select="./*/*/StreetName"/>
<xsl:value-of select="$delim"/>
<xsl:value-of select="./*/*/StreetType"/>

I've previously defined $delim as:

<xsl:param name="delim" select="','" />

So this piece of template will output for example:

10,SMITH,ST

Let's say for arguments sake I want to wrap the street name in double quotes, so that it comes out as:

10,"SMITH",ST

I've tried a variety of things, but they're all variations on the theme of declaring a double quote as:

<xsl:param name="quote">"</xsl:param>

And then wrapping my value as:

<xsl:value-of select="$quote"/>
<xsl:value-of select="./*/*/StreetName"/>
<xsl:value-of select="$quote"/>

But when the transformation runs, I get nothing. It just looks as it did before. Other things I've tried are:

  • Putting the double quotes in the template as literals. Totally screws things up.
  • Using " instead of ". No effect, and I'm outputting as text not html anyway.
  • Using and selecting a variable.

Any suggestions?

like image 901
Mark Micallef Avatar asked Mar 22 '23 18:03

Mark Micallef


1 Answers

In response to your comment, I think the issue is with Microsoft Excel here, not with XSLT. The quotation marks are treated as text-delimiters, and normally you would only use them when you have a field with a comma in, to indicate it is just one single field, and not two. In any case, when you read a CSV file into Microsoft Excel, it does not show any text delimiters (regardless of whether the field contains a comma or not), just the text within them. The quotation marks are just not considered part of the text in the field, but are simply used to delimit the text in the field where it might contain special characters.

Furthermore, when you save the file as CSV file, Excel will only use text-delimiters where required (i.e. when the field contains a comma). If the CSV file had quotation marks around a field which did not contain a comma, like in your example of 10,"SMITH",ST, then these get removed when saving.

To prove this, try renaming your file as a .txt file before serving up in the browser (so it should open up in IE, or maybe notepad), and see if the data is as expected.

If the intention was to have quotation marks as part of the actual field text, and not treated as a delimiter, you will need to output THREE quotation marks around the data

<xsl:param name="delim" select="','" />
<xsl:param name="quote">"</xsl:param>

<xsl:value-of select="./*/*/LotNumber"/>
<xsl:value-of select="$delim"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="./*/*/StreetName"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="$quote"/>
<xsl:value-of select="$delim"/>
<xsl:value-of select="./*/*/StreetType"/>

So, as text, it will be output as follows

10,"""SMITH""",ST

When opened in Excel, you should see a single set of quotation marks around "Smith".

Three quotation marks are needed here because the outermost are the text-delimiters, and then a pair of conecutive quotes then get treated as a single-quote in the actual field.

like image 60
Tim C Avatar answered Apr 07 '23 02:04

Tim C