[英]Passing arguments and parsing xml in Erlang
Howdy I'm still learning Erlang and I'm trying to pass more then one argument through functions but I'm hitting a wall. Howdy我仍在学习Erlang,我试图通过函数传递不止一个参数,但我遇到了麻烦。
To start I'm using xmerl.hrl to parse the xml. 首先,我使用xmerl.hrl解析xml。 I call process_xml(Doc) through the command line with erlfile:process_xml("filename.xml").
我通过erlfile:process_xml(“ filename.xml”)在命令行中调用process_xml(Doc)。
process_xml(Doc) ->
makeBottom1(Doc,"yup got here").
From here the process calls makeBottom1 passing in the doc and a string to test output later on. 从这里开始,过程调用makeBottom1传递文档和一个字符串以稍后测试输出。
makeBottom1(E = #xmlElement{name='resource'}, Derp) -> "Got to resource";
makeBottom1(E, Derp) -> ["<h1>BURP</h1>", Derp, built_in_rules( fun makeBottom1/2 , [E, Derp] ), "-" ].
I expect it to return "Got to resource" loads of times but it never seems to trip that function call. 我希望它返回“获取资源”负载的时间,但似乎永远不会触发该函数调用。
What it does return is this: 它返回的结果是:
["<h1>BURP</h1>","yup got here",[],"-"]
Should I use a different xml parser for the functionality I want? 是否应该为我想要的功能使用其他xml解析器? Am I just making a rookie mistake?
我只是在犯一个菜鸟错误吗?
The function built_in_rules
expects 2 arguments: A function with arity 1 (one argument), and an XML node. 函数
built_in_rules
需要2个参数:具有arity 1(一个参数)的函数和一个XML节点。 Since your arguments do not fit the patterns described by the function clauses , you're triggering a catch-all condition which simply returns an empty list. 由于您的参数不符合function子句描述的模式,因此您将触发一个包罗万象的条件,该条件只会返回一个空列表。
To work around this issue, you need to change the way you're using built_in_rules
. 要变通解决此问题,您需要更改使用
built_in_rules
。 Try the following: 请尝试以下操作:
built_in_rules(fun(Child) -> makeBottom1(Child, Derp) end, E)
Instead of trying to pass in a function with arity of two and expecting built_in_rules
to know what to do with it, we instead pass in a function with arity 1, which built_in_rules
knows exactly how to use, and use a closure to add our second argument. 与其尝试传递
built_in_rules
为2的函数并期望built_in_rules
知道如何处理它, built_in_rules
传递一个built_in_rules
1的函数, built_in_rules
确切地知道如何使用,并使用闭包添加第二个参数。 We also amend the second argument to the function call so that we're only passing in the current XML node. 我们还将第二个参数修改为函数调用,以便仅传递当前的XML节点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.