3 SELECT evergreen.upgrade_deps_block_check('0928', :eg_version);
5 CREATE OR REPLACE FUNCTION oils_xpath_tag_to_table(marc text, tag text, xpaths text[]) RETURNS SETOF record AS $function$
7 -- This function currently populates columns with the FIRST matching value
8 -- of each XPATH. It would be reasonable to add a 'return_arrays' option
9 -- where each column is an array of all matching values for each path, but
10 -- that remains as a TODO
20 FOR i IN 1 .. ARRAY_UPPER(xpaths,1) LOOP
21 IF xpaths[i] = 'null()' THEN
22 select_list := ARRAY_APPEND(select_list, 'NULL::TEXT AS c_' || i );
24 select_list := ARRAY_APPEND(select_list, '(oils_xpath(' ||
27 WHEN xpaths[i] ~ $re$/[^/[]*@[^/]+$$re$ -- attribute
28 OR xpaths[i] ~ $re$text\(\)$$re$
30 ELSE xpaths[i] || '//text()'
32 ) || ', field_marc))[1] AS cl_' || i);
33 -- hardcoded to first value for each path
37 -- run query over tag set
38 q := 'SELECT ' || ARRAY_TO_STRING(select_list, ',')
39 || ' FROM UNNEST(oils_xpath(' || quote_literal('//*[@tag="' || tag
40 || '"]') || ', ' || quote_literal(marc) || ')) AS field_marc;';
41 --RAISE NOTICE '%', q;
43 RETURN QUERY EXECUTE q;
46 $function$ LANGUAGE PLPGSQL;