4 INSERT INTO config.upgrade_log (version) VALUES ('0145'); -- miker
6 CREATE FUNCTION version_specific_xpath () RETURNS TEXT AS $wrapper_function$
11 IF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT < 8.3 THEN
12 out_text := 'Creating XPath functions that work like the native XPATH function in 8.3+';
14 EXECUTE $create_82_funcs$
16 CREATE OR REPLACE FUNCTION oils_xpath ( xpath TEXT, xml TEXT, ns ANYARRAY ) RETURNS TEXT[] AS $func$
23 munged_xpath := xpath;
25 IF ns IS NOT NULL AND array_upper(ns, 1) IS NOT NULL THEN
26 FOR namespace IN 1 .. array_upper(ns, 1) LOOP
27 munged_xpath := REGEXP_REPLACE(
29 E'(' || ns[namespace][1] || E'):(\\w+)',
30 E'*[local-name() = "\\2" and namespace-uri() = "' || ns[namespace][2] || E'"]',
35 munged_xpath := REGEXP_REPLACE( munged_xpath, E'\\]\\[(\\D)',E' and \\1', 'g');
38 -- RAISE NOTICE 'munged xpath: %', munged_xpath;
40 node_text := xpath_nodeset(xml, munged_xpath, 'XXX_OILS_NODESET');
41 -- RAISE NOTICE 'node_text: %', node_text;
43 IF munged_xpath ~ $re$/[^/[]*@[^/]+$$re$ THEN
44 node_text := REGEXP_REPLACE(node_text,'<XXX_OILS_NODESET>[^"]+"', '<XXX_OILS_NODESET>', 'g');
45 node_text := REGEXP_REPLACE(node_text,'"</XXX_OILS_NODESET>', '</XXX_OILS_NODESET>', 'g');
48 node_text := REGEXP_REPLACE(node_text,'^<XXX_OILS_NODESET>', '');
49 node_text := REGEXP_REPLACE(node_text,'</XXX_OILS_NODESET>$', '');
51 RETURN STRING_TO_ARRAY(node_text, '</XXX_OILS_NODESET><XXX_OILS_NODESET>');
53 $func$ LANGUAGE PLPGSQL;
55 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS $$SELECT oils_xpath( $1, $2, '{}'::TEXT[] );$$ LANGUAGE SQL;
57 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $$
58 SELECT xslt_process( $1, $2 );
62 ELSIF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT = 8.3 THEN
63 out_text := 'Creating XPath wrapper functions around the native XPATH function in 8.3. contrib/xml2 still required!';
65 EXECUTE $create_83_funcs$
67 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT, ANYARRAY ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML, $3 )::TEXT[];' LANGUAGE SQL;
68 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML )::TEXT[];' LANGUAGE SQL;
70 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $$
71 SELECT xslt_process( $1, $2 );
77 out_text := 'Creating XPath wrapper functions around the native XPATH function in 8.4+, and plperlu-based xslt processor. No contrib/xml2 needed!';
79 EXECUTE $create_84_funcs$
81 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT, ANYARRAY ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML, $3 )::TEXT[];' LANGUAGE SQL;
82 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML )::TEXT[];' LANGUAGE SQL;
84 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $func$
93 # The following approach uses the older XML::LibXML 1.69 / XML::LibXSLT 1.68
94 # methods of parsing XML documents and stylesheets, in the hopes of broader
95 # compatibility with distributions
96 my $parser = $_SHARED{'_xslt_process'}{parsers}{xml} || XML::LibXML->new();
98 # Cache the XML parser, if we do not already have one
99 $_SHARED{'_xslt_process'}{parsers}{xml} = $parser
100 unless ($_SHARED{'_xslt_process'}{parsers}{xml});
102 my $xslt_parser = $_SHARED{'_xslt_process'}{parsers}{xslt} || XML::LibXSLT->new();
104 # Cache the XSLT processor, if we do not already have one
105 $_SHARED{'_xslt_process'}{parsers}{xslt} = $xslt_parser
106 unless ($_SHARED{'_xslt_process'}{parsers}{xslt});
108 my $stylesheet = $_SHARED{'_xslt_process'}{stylesheets}{$xslt} ||
109 $xslt_parser->parse_stylesheet( $parser->parse_string($xslt) );
111 $_SHARED{'_xslt_process'}{stylesheets}{$xslt} = $stylesheet
112 unless ($_SHARED{'_xslt_process'}{stylesheets}{$xslt});
114 return $stylesheet->output_string(
115 $stylesheet->transform(
116 $parser->parse_string($doc)
120 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
128 $wrapper_function$ LANGUAGE PLPGSQL;
130 SELECT version_specific_xpath();
131 DROP FUNCTION version_specific_xpath();
134 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT, TEXT, ANYARRAY ) RETURNS TEXT AS $func$
135 SELECT ARRAY_TO_STRING(
138 CASE WHEN $1 ~ $re$/[^/[]*@[^/]+$$re$ OR $1 ~ $re$text\(\)$$re$ THEN '' ELSE '//text()' END,
146 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT, TEXT ) RETURNS TEXT AS $func$
147 SELECT oils_xpath_string( $1, $2, $3, '{}'::TEXT[] );
150 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT ) RETURNS TEXT AS $func$
151 SELECT oils_xpath_string( $1, $2, '{}'::TEXT[] );
156 CREATE OR REPLACE FUNCTION extract_marc_field ( TEXT, BIGINT, TEXT, TEXT ) RETURNS TEXT AS $$
162 SELECT regexp_replace(
164 $q$ || quote_literal($3) || $q$,
168 $q$ || quote_literal($4) || $q$,
171 FROM $q$ || $1 || $q$
172 WHERE id = $q$ || $2;
174 EXECUTE query INTO output;
176 -- RAISE NOTICE 'query: %, output; %', query, output;