Question

My Java EE web app uses XSLT transformation to produce CSV reports. If I specify "UTF-16" as the encoding on the XSLT (which seems to be the only way to handle accented characters in the XML) then I get a byte-order-mark character at the start of the CSV output

From what I understand, this is correct behavior. The problem is that this BOM character is visible if the CSV is opened in Excel (appears as: þÿ), but not if I open it in a proper text editor ;)

How can I hide or omit this character so it isn't visible when the file is opened in Excel?

Was it helpful?

Solution

You forgot to specify which version of XSLT you are using.

If you are using XSLT 2.0, you can suppress the BOM using the xsl:output element. For example...

<xsl:output method="xml" byte-order-mark="no" encoding="UTF-16" indent="yes" omit-xml-declaration="yes" />

What if you are using XSLT 1.0?

Here are two options:

  1. Upgrade to XSLT 2.0
  2. Upgrade your Excel to 2007 (See here for why)

OTHER TIPS

if you are writing a CSV file the method should probably not be "xml" as Sean B. Durkin suggests.

i recommend this configuration to make a CSV that will correctly open in Excel

<xsl:output method="text" byte-order-mark="yes" encoding="UTF-16LE" indent="no"/>

Other tips when using XSLT to generate a CSV:

You can use TAB characters even though CSV means "comma separated value"

I find the data i separate is often void of tabs but has the occasional comma.

and <xsl:text>&#xa;</xsl:text> is a way to add a line-break.

you probably want to escape linebreaks that occur in the values you use, like this:

<xsl:value-of select="replace($foo,'\n','\\n')"/>

and of course, you will quickly notice that if you open the file in Excel, it becomes read-only which is a bother when you try to generate the file again while it is still open. unlike most text editors which simply tell you the file has changed, and offer to reload it.

-

I realize this question is dated and answered already, but I hope this helps a future someone.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top