Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir and Erlang Records Pattern Matching

Tags:

xml

erlang

elixir

How to make this kind of record pattern matching in Elixir?

[ #xmlText{value=Rank} ]  = xmerl_xpath:string("//SalesRank/text()", Xml),

Bonus: rewrite this example from Dave Thomas's blog in Elixir.


Update:

found what was my problem. You have to use

defrecord :xmlText, Record.extract(:xmlText, from_lib: 'xmerl/include/xmerl.hrl')

to extract the record from XMerL lib into your program as stated here. Then the .value syntax works and the line can be written as follows:

rank = Enum.first(xmerl_xpath.string('//SalesRank/text()', xml)).value
like image 965
Henry Mazza Avatar asked Jun 27 '13 14:06

Henry Mazza


1 Answers

See question for answer. (This is at the top of the unanswered list)

Otherwise I found the answer during my investigations in a blog post on elixir-lang.org

For the most commonly used records:

defrecord :xmlElement, Record.extract(:xmlElement, from_lib: "xmerl/include/xmerl.hrl")
defrecord :xmlText, Record.extract(:xmlText, from_lib: "xmerl/include/xmerl.hrl")
like image 144
expelledboy Avatar answered Sep 28 '22 04:09

expelledboy