@@ -16,6 +16,13 @@ create or replace package body ut_utils is
1616 limitations under the License.
1717 */
1818
19+ /**
20+ * Constants regex used to validate XML name
21+ */
22+ gc_invalid_first_xml_char constant varchar2(50) := '[^_a-zA-Z]';
23+ gc_invalid_xml_char constant varchar2(50) := '[^_a-zA-Z0-9\.-]';
24+ gc_full_valid_xml_name constant varchar2(50) := '^([_a-zA-Z])([_a-zA-Z0-9\.-])*$';
25+
1926 function surround_with(a_value varchar2, a_quote_char varchar2) return varchar2 is
2027 begin
2128 return case when a_quote_char is not null then a_quote_char||a_value||a_quote_char else a_value end;
@@ -749,8 +756,47 @@ create or replace package body ut_utils is
749756 ,modifier => 'm');
750757 return l_caller_stack_line;
751758 end;
752-
753-
759+
760+ /**
761+ * Change string into unicode to match xmlgen format _00<unicode>_
762+ * https://docs.oracle.com/en/database/oracle/oracle-database/12.2/adxdb/generation-of-XML-data-from-relational-data.html#GUID-5BE09A7D-80D8-4734-B9AF-4A61F27FA9B2
763+ * secion 8.2.1.1
764+ */
765+ function char_to_xmlgen_unicode(a_character varchar2) return varchar2 is
766+ begin
767+ return '_x00'||rawtohex(utl_raw.cast_to_raw(a_character))||'_';
768+ end;
769+
770+ /**
771+ * Build valid XML column name as element names can contain letters, digits, hyphens, underscores, and periods
772+ */
773+ function build_valid_xml_name(a_preprocessed_name varchar2) return varchar2 is
774+ l_post_processed varchar2(4000);
775+ begin
776+ for i in (select regexp_substr( a_preprocessed_name ,'(.{1})', 1, level, null, 1 ) AS string_char,level level_no
777+ from dual connect by level <= regexp_count(a_preprocessed_name, '(.{1})'))
778+ loop
779+ if i.level_no = 1 and regexp_like(i.string_char,gc_invalid_first_xml_char) then
780+ l_post_processed := l_post_processed || char_to_xmlgen_unicode(i.string_char);
781+ elsif regexp_like(i.string_char,gc_invalid_xml_char) then
782+ l_post_processed := l_post_processed || char_to_xmlgen_unicode(i.string_char);
783+ else
784+ l_post_processed := l_post_processed || i.string_char;
785+ end if;
786+ end loop;
787+ return l_post_processed;
788+ end;
789+
790+ function get_valid_xml_name(a_name varchar2) return varchar2 is
791+ l_valid_name varchar2(4000);
792+ begin
793+ if regexp_like(a_name,gc_full_valid_xml_name) then
794+ l_valid_name := a_name;
795+ else
796+ l_valid_name := build_valid_xml_name(a_name);
797+ end if;
798+ return l_valid_name;
799+ end;
754800
755801end ut_utils;
756802/
0 commit comments