Remove xml declaration in response in elixir and phoenix

remove xml declaration in payload response in elixir and phoenix.

I want to remove the below from xml response, can someone please guide how to do it?

<?xml version="1.0" encoding="UTF-8"?>

How are you generating this response?

1 Like

Thanks Ben for quick reply. Steps below-

xml = “my request xml here”
wsdl_url = “https://sandbox.ws-idu.tracesmart.co.uk/v5.6/?wsdl”
headers = [{“content-type”, “text/xml”}]

{:ok, %HTTPoison.Response{status_code: 200, body: body}} = HTTPoison.post(wsdl_url, xml, headers)

Are you parsing the XML response?

To look into the response, I am doing

IO.puts("#{body}")

I am getting ‘<?xml version="1.0" encoding="UTF-8"?>’ before the actual response, which I want to remove Ben.

No parsing, just this much as above. I just want to remove the header from the response.

I can send the request xml as well, if you need.

The issue is resolved now.

How? You should leave some info for future readers.

I could not get any information on removing the xml declaration. I could not remove that till now.
What I could resolve was my issue of storing the response xml data in database.

But why would you want to remove the XML header? That makes the XML invalid.

Without the declaration its still well-formed.

And even if a doctype is specified, the XML declaration doesn’t affect validity of the document against the declared doctype(s).

1 Like

Well-formed, maybe, but a lot of parsers outright refuse to parse an XML without its header.

(EDIT: I was wrong, details below.)

https://www.w3.org/TR/2008/REC-xml-20081126/#sec-prolog-dtd

XML documents should begin with an XML declaration which specifies the version of XML being used.

“should” means, it is prefered to see it, but it is not necessary.

Of course, if you want to set up something that is different than the defaults, then you need it. But it is not required, not even for valid documents.

Yep, I know that. Last time I had to grapple with 20+ XML parsers (about 1.5 years ago) I do remember like half of them refused to parse without the header (written in several languages, not just Erlang/Elixir).

Just re-checked. :xmerl doesn’t require it at least. :slight_smile:

EDIT: Now that I think of it in more detail and reopened the last Elixir project where I had to deal with XML, I might have mistook the lack of the header with the lack of a root element, which is an invalid XML. The people there wanted an XML with several root elements for some reason. Yep, my mistake, the header is quite optional indeed and it’s just that some parsers choose to blow up without it.