KRL 和雅虎本地搜索
我正在尝试在 Kynetx 应用程序中使用雅虎本地搜索。
ruleset avogadro {
meta {
name "yahoo-local-ruleset"
description "use results from Yahoo local search"
author "randall bohn"
key yahoo_local "get-your-own-key"
}
dispatch { domain "example.com"}
global {
datasource local:XML <- "http://local.yahooapis.com/LocalSearchService/V3/localsearch";
}
rule add_list {
select when pageview ".*" setting ()
pre {
ds = datasource:local("?appid=#{keys:yahoo_local()}&query=pizza&zip=#{zip}&results=5");
rs = ds.pick("$..Result");
}
append("body","<ul id='my_list'></ul>");
always {
set ent:pizza rs;
}
}
rule add_results {
select when pageview ".*" setting ()
foreach ent:pizza setting pizza
pre {
title = pizza.pick("$..Title");
}
append("#my_list", "<li>#{title}</li>");
}
}
我最终得到的列表是
. [object Object]
“标题”,
{'$t' => 'Pizza Shop 1'}
我不知道如何获取标题。看起来原始 XML 文件中的“文本内容”变成了 {'$t' =>; 'text content'} 和 '$t' 给 pick() 带来了问题。
I'm trying to use Yahoo Local Search in a Kynetx Application.
ruleset avogadro {
meta {
name "yahoo-local-ruleset"
description "use results from Yahoo local search"
author "randall bohn"
key yahoo_local "get-your-own-key"
}
dispatch { domain "example.com"}
global {
datasource local:XML <- "http://local.yahooapis.com/LocalSearchService/V3/localsearch";
}
rule add_list {
select when pageview ".*" setting ()
pre {
ds = datasource:local("?appid=#{keys:yahoo_local()}&query=pizza&zip=#{zip}&results=5");
rs = ds.pick("$..Result");
}
append("body","<ul id='my_list'></ul>");
always {
set ent:pizza rs;
}
}
rule add_results {
select when pageview ".*" setting ()
foreach ent:pizza setting pizza
pre {
title = pizza.pick("$..Title");
}
append("#my_list", "<li>#{title}</li>");
}
}
The list I wind up with is
. [object Object]
and 'title' has
{'$t' => 'Pizza Shop 1'}
I can't figure out how to get just the title. It looks like the 'text content' from the original XML file turns into {'$t' => 'text content'} and the '$t' give problems to pick().
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当 XML 数据源和数据集转换为 JSON 时,XML 节点中的文本值将分配给 $t。您可以通过将 pre 块中的 pick 语句更改为“尝试一下”来选择标题文本
,看看是否能解决您的问题。
关于与您要考虑的问题无关的事项的旁注:
1)感谢您分享整个规则集、您遇到的问题以及您的期望。使回答您的问题变得更加容易。
2) 不应更改 AppBuilder 或命令行 gem 为您生成的规则集标识符。当前的标识符
应该看起来更像
规则集 a60x304 {
3) 您不需要
在 select 语句中使用 ,除非您的正则表达式中有一个捕获组
When XML datasources and datasets get converted into JSON, the text value within an XML node gets assigned to $t. You can pick the text of the title by changing your pick statement in the pre block to
Try that and see if that solves your problem.
Side notes on things not related to your question to consider:
1) Thank you for sharing the entire ruleset, what problem you were seeing and what you expected. Made answering your question much easier.
2) The ruleset identifier should not be changed from what AppBuilder or the command-line gem generate for you. Your identifier that is currently
should look something more like
ruleset a60x304 {
3) You don't need the
in the select statement unless you have a capture group in your regular expression
事实证明 pick("$..Title.$t") 确实有效。它看起来很有趣,但确实有效。我猜比小丑帽还没有那么有趣。
希望我现在就吃点披萨!
Turns out that pick("$..Title.$t") does work. It looks funny but it works. Less funny than a clown hat I guess.
Wish I had some pizza right now!