Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to return values as strings. #8

Merged
merged 1 commit into from
Aug 10, 2015

Conversation

viniciussbs
Copy link
Contributor

Values are char list as default. If we encode the response as JSON, it will be encoded as an array instead of a string. With values as strings, we can properly encode response as JSON. To ask for values as strings, just use the modifier s:

iex> doc = "<foo>Bar</foo>"
"<foo>Bar</foo>"
iex> doc |> xpath(~x"/foo/text()") |> Poison.encode
{:ok, "[66,97,114]"}
iex> doc |> xpath(~x"/foo/text()"s) |> Poison.encode
{:ok, "\"Bar\""}
iex> doc |> xpath(~x"/foo/text()"l) |> Poison.encode
{:ok, "[[66,97,114]]"}
iex> doc |> xpath(~x"/foo/text()"ls) |> Poison.encode
{:ok, "[\"Bar\"]"}

Values are char list as default. If we encode the response as JSON,
it will be encoded as an array instead of a string.

Now, with values as strings, we can easily encode response as JSON.
@Jeweller-Tsai
Copy link

@viniciussbs 👍

@patrickbrown-dev
Copy link

@viniciussbs, does this also solve the issue of xmerl returning an array of char lists when it hit's certain characters? This occurs especially often in escaped HTML.

For example:

iex> doc = "<foo>&lt;img src=\"/img/pic.jpg\"/&gt;</foo>"
"<foo>&lt;img src=\"/img/pic.jpg\"/&gt;</foo>"
iex> doc |> xpath(~x"//foo/text()"l)
['<img src="/img/pic.jpg"/', '>']

@viniciussbs
Copy link
Contributor Author

@ptrckbrwn No, it doesn't. See:

iex> import SweetXml
nil
iex> doc = "<foo>&lt;img src=\"/img/pic.jpg\"/&gt;</foo>"
"<foo>&lt;img src=\"/img/pic.jpg\"/&gt;</foo>"
iex> doc |> xpath(~x"//foo/text()"s)
"<img src=\"/img/pic.jpg\"/"
iex> doc |> xpath(~x"//foo/text()"ls)
["<img src=\"/img/pic.jpg\"/", ">"]

You could do something like this, instead:

iex> doc |> xpath(~x"//foo/text()"ls) |> Enum.reduce(&(&2 <> &1))
"<img src=\"/img/pic.jpg\"/>"

@viniciussbs
Copy link
Contributor Author

@ptrckbrwn Without this new sigil, the code abouve won't work:

ex> doc |> xpath(~x"//foo/text()"l) |> Enum.reduce(&(&2 <> &1))
** (ArgumentError) argument error

@patrickbrown-dev
Copy link

Hmm, okay. I'll open another issue.

On Mon, Aug 3, 2015 at 10:36 AM Vinícius Sales [email protected]
wrote:

@ptrckbrwn https://github.com/ptrckbrwn Without this new sigil, the
code abouve won't work:

ex> doc |> xpath(~x"//foo/text()"l) |> Enum.reduce(&(&2 <> &1))
** (ArgumentError) argument error


Reply to this email directly or view it on GitHub
#8 (comment).

gniquil added a commit that referenced this pull request Aug 10, 2015
Add support to return values as strings.
@gniquil gniquil merged commit 7c2c162 into kbrw:master Aug 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants