Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,6 @@ defmodule SweetXml do
get_current_entities(parent, spec) |> spec.transform_fun.()
end


def xpath(parent, sweet_xpath, subspec) do
if sweet_xpath.is_list do
current_entities = xpath(parent, sweet_xpath)
Expand Down Expand Up @@ -554,6 +553,26 @@ defmodule SweetXml do
%{sweet_xpath | transform_fun: fun}
end

@doc ~S"""
Convert `doc` to XML string

## Examples

iex> import SweetXml
iex> "<h1><a>Some linked title</a></h1>"
...> |> parse()
...> |> doc_to_xml()
"<?xml version=\"1.0\"?><h1><a>Some linked title</a></h1>"
iex> "<body><header><ul><li>One</li><li><a>Two</a></li></ul></header></body>"
...> |> parse()
...> |> xpath(~x"/body/header/ul"e)
...> |> doc_to_xml()
"<?xml version=\"1.0\"?><ul><li>One</li><li><a>Two</a></li></ul>"
"""
def doc_to_xml(doc) do
:xmerl.export([doc], :xmerl_xml) |> List.flatten() |> List.to_string()
end

defp _value(entity) do
cond do
is_record? entity, :xmlText ->
Expand Down