3

我有一个从 XML 数据源返回的哈希,如下所示:

{...,
  'records' :{
    'record' :[
      {'availability' :{'$t' :'available'}, ...},
      {'availability' :{'$t' :'available'}, ...}
    ]
  }
};

我想把所有的record散列放到一个数组中,这样我就可以filter()做一些其他的操作了。但是,当我在我的pre块中有这个声明时,

raw_records = raw.pick("$..record");

返回的数组是两个空字符串的数组:

var raw_records = ['', ''];

奇怪的是,我可以挑选出availability预期的结果:

availability = raw.pick("$..availability.$t");

生产

var availability = ['available', 'available'];

我的第一个怎么了pick()

编辑:这是一个更完整的版本,应该有助于重现问题。略有不同,因为我现在使用的是 JSON 版本的 Web 服务:

global {
    datasource hbll <- "https://svc.lib.byu.edu/services/catalog/v1/search/?field=isbn&format=json&terms=";
}

rule new_rule {
    select when pageview "amazon.com/.*/?dp/(.*)/" setting (isbn)
    pre {
        //This is the array with two empty strings...
        raw = datasource:hbll(isbn);
        myfilter = function(x) { x.pick("availability") eq "available"; };
        records = raw.filter(myfilter);
        len = records.length();
        availability = records.pick("$..availability");
        middleman = len > 1 => availability[0] | availability;
        available = middleman eq "available" => true | false;
        url_list = records.pick("$..url");
        url = len > 1 => url_list[0] | url_list;
        msg = <<
            <p>This book is available for checkout at the BYU Library.</p>
            <a href="#{url}">More information</a>
        >>;
    }
    notify("BYU Harold B. Lee Library", msg) with sticky=true;
}
4

1 回答 1

1

我需要一个更完整的例子。我得到的测试应用程序和结果如下:

ruleset a8x167 {
    meta {
        name "Pick - Array of Hashes"
        description <<
            Testing
        >>
        author "Sam Curren"
        logging on
    }

    dispatch {}

    global {
        raw = {
          'records' :{
            'record' :[
              {'availability' :{'$t' :'available'}},
              {'availability' :{'$t' :'available'}}
            ]
          }
        };

    }

    rule test {
        select when pageview ".*" setting ()
        pre {
            raw_records = raw.pick("$..record");
            availability = raw.pick("$..availability.$t");
        }
        notify("Hello World", "This is a sample rule.");
    }
}

结果:

var raw_records = [{'availability' :{'$t' :'available'}}, {'availability' :{'$t' :'available'}}];
var availability = ['available', 'available'];
于 2011-04-25T21:23:35.640 回答