How do I remove the BOM character from my xml file [duplicate]
Asked Answered
P

6

68

I am using xsl to control the output of my xml file, but the BOM character is being added.

Potshot answered 17/11, 2008 at 12:27 Comment(3)
Unlike on plain text files, a byte order mark on a XML file should never cause any problems, since all XML parsers should be able to deal with it, even if it is the "UTF-8 BOM". In fact, it is even suggested on the XML standard itself as part of <a href="w3.org/TR/2006/REC-xml-20060816/… encoding autodetection</a>.Lurlenelurline
This is not a suggestion, section F is not normative. A UTF-8 BOM is explicitly allowed by the Unicode standard, but is not recommended - en.wikipedia.org/wiki/Byte_order_mark#cite_note-2 - the UTF-8 BOM does not indicate byte order.Argyrol
Doesn't look like a duplicate as the other question mentions specific tool.Distinct
L
165
# vim file.xml
:set nobomb
:wq
Larimore answered 18/11, 2008 at 22:59 Comment(0)
M
2

just need to add this in your xslt file:

<xsl:output method="text"
        encoding="ASCII"/>
Mayolamayon answered 5/10, 2011 at 16:40 Comment(0)
U
1

Just strip first two bytes using any hex editor.

Uncourtly answered 17/11, 2008 at 12:48 Comment(2)
Or 3, depending on UTF flavorSeisin
Or 4, for UTF-32. But it's most likely 3, UTF-8 being the most common encoding for XML.Tussore
C
1

Remove the BOM symbol from string with XSLT is pretty simple:

<xsl:value-of select="translate(StringWithBOM,'','')"/>

Conformable answered 25/4, 2012 at 11:4 Comment(0)
A
0

I was under the impression that XML is encouraged to be written in Unicode, in some Unicode encoding, and that certain Unicode encodings are specified to contain an initial byte-order mark. Without that byte-order mark, your file is no longer correctly encoded in a Unicode encoding and therefore no longer correct XML. XML processors are encouraged to be unforgiving, to fail immediately on the slightest error (such as an incorrect Unicode encoding). What kinds of XML processors are you looking to break?

Obviously, stripping a byte-order mark from a UTF-8 encoded document makes that document appear to be ASCII encoded (not Unicode), and some text processors are capable only of using ASCII encoded documents. Is this what you're working with?

Adamis answered 17/11, 2008 at 12:58 Comment(1)
For XML files which do not specify the encoding and have no BOM, UTF-8 is the default encoding.Argyrol
D
0

What output encoding is your XSL set to use? What encoding is the input document? Where's the input coming from, and where was it saved/uploaded/dowloaded in the meantime?

XML and XSL should default to using UTF-8 if nothing else is specified. But clearly, something's going wrong here.

One thing which might happen is, the XML is being served up by a web server which is set by default to serve in ISO-8859-1, a pretty good default ... pre-Unicode.

Slightly off-topic, but Joel's very instructive article about text encodings was an eye-opener to me. There are a lot of people out there who are otherwise very smart about programming, but who persist in thinking there's such a thing as "plain text" or calling their text "ASCII" or "ANSI". It's an issue you really need to get to grips with if you haven't yet.

Detrition answered 17/11, 2008 at 23:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.