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

xpath returns charlist instead of integer #33

Closed
Fnux opened this issue Jul 22, 2016 · 2 comments
Closed

xpath returns charlist instead of integer #33

Fnux opened this issue Jul 22, 2016 · 2 comments

Comments

@Fnux
Copy link

Fnux commented Jul 22, 2016

Well, this is pretty weird... Is there any reason I get '0' instead of 0 ?

iex(33)> count = "<posts count=\"0\" offset=\"0\"/>" |> xpath(~x"//posts/@count"l)
['0']
iex(34)> List.first count
'0'
iex(35)> i List.first count
Term
  '0'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent valid
  ASCII characters. Conventionally, such lists of integers are referred to as
  "charlists" (more precisely, a charlist is a list of Unicode codepoints,
  and ASCII is a subset of Unicode).
Raw representation
  [48]
Reference modules
  List
iex(36)> 

@Fnux Fnux changed the title xpath returns ['0'] (charlist?) instead of [0] xpath returns charlist instead of integer Jul 22, 2016
@seantanly
Copy link
Contributor

@Fnux just noticed this and thought I could shed some light.

xpath defaults to returning char list and therefore the above behavior is correct.

To get integer, you have to explicitly cast it.

iex> count = "<posts count=\"0\" offset=\"0\"/>" |> xpath(~x"//posts/@count"li)
> [0]
iex> List.first count
> 0

If you're just trying to get the value of attribute count, simply do

iex> count = "<posts count=\"0\" offset=\"0\"/>" |> xpath(~x"//posts/@count"i)
> 0

Hope this helps. :)

@Fnux Fnux closed this as completed Feb 3, 2017
@Fnux
Copy link
Author

Fnux commented Feb 3, 2017

Old issue I forgot to close ! Thank you @seantanly

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

No branches or pull requests

2 participants