Open
Description
Just sharing a bit of code I'm using. For some context, I'm starting to use Saxy to build XML queries during testing. I must compare them to a baseline template, to ensure I used the Saxy.XML
API correctly. These templates include newlines for human readability (indented/pretty-printed).
To make the comparison automated, I'm removing the non-significant newlines with the following code so far:
defmodule Stripper do
def filter_newlines_from_model({element, tags, children}) do
{
element,
tags,
children
|> Enum.map(&filter_newlines_from_model/1)
|> Enum.reject(&is_nil/1)
}
end
def filter_newlines_from_model(content) when is_binary(content) do
if content |> String.trim() == "", do: nil, else: content
end
end
I can use this like this afterwards:
{:ok, parsed_request} = Saxy.SimpleForm.parse_string(xml, cdata_as_characters: false)
IO.inspect(Stripper. filter_newlines_from_model(parsed_request))
This makes it easier to compare this using ExUnit
assert x == y
, for instance.
I wonder if:
- this is worth adding to the documentation as an example
- or if it could be an interesting option for
Saxy.SimpleForm.parse_string
(less sure at this point, we can mull over this!)
Let me know what you think!