From 95a36efb25aa85a9b533ede573fd89633fb59fd6 Mon Sep 17 00:00:00 2001 From: datRedHeadedGuy Date: Thu, 11 Oct 2018 16:21:42 -0500 Subject: [PATCH 1/5] Update ora/xml_util_pkg.pkb Modified the xml_util_pkg and changed the use of XMLType.extract(xpath).getStringVal() to EXTRACTVALUE(XMLType, xpath) so that special characters are not escaped. --- ora/xml_util_pkg.pkb | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/ora/xml_util_pkg.pkb b/ora/xml_util_pkg.pkb index 805fad8..0ffa6c4 100755 --- a/ora/xml_util_pkg.pkb +++ b/ora/xml_util_pkg.pkb @@ -303,11 +303,14 @@ begin Who Date Description ------ ---------- ------------------------------------- MBR 07.12.2010 Created + JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() + to EXTRACTVALUE(XMLType, xpath) so that special characters + are not escaped */ begin - select xmltype(p_tag).extract('//@' || p_attr_name).getstringval() + select extractvalue( xmltype(p_tag), '//@' || p_attr_name ) into l_returnvalue from dual; exception @@ -339,11 +342,16 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created + JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() + to EXTRACTVALUE(XMLType, xpath) so that special characters + are not escaped */ begin - l_returnvalue := p_xml.extract(p_xpath, p_namespace).getstringval(); + select extractvalue( p_xml, p_xpath, p_namespace ) + into l_returnvalue + from dual; exception when others then l_returnvalue := p_default_value; @@ -372,14 +380,21 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created + JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() + to EXTRACTVALUE(XMLType, xpath) so that special characters + are not escaped */ begin if p_date_format is not null then - l_returnvalue := to_date(p_xml.extract(p_xpath, p_namespace).getstringval(), p_date_format); + select to_date( extractvalue( p_xml, p_xpath, p_namespace ), p_date_format ) + into l_returnvalue + from dual; else - l_returnvalue := to_date(substr(p_xml.extract(p_xpath, p_namespace).getstringval(), 1, 19), 'YYYY-MM-DD"T"hh24:mi:ss'); + select to_date( substr( extractvalue( p_xml, p_xpath, p_namespace ), 1, 19 ), 'YYYY-MM-DD"T"hh24:mi:ss' ) + into l_returnvalue + from dual; end if; exception when others then @@ -408,11 +423,16 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created + JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() + to EXTRACTVALUE(XMLType, xpath) so that special characters + are not escaped */ begin - l_returnvalue := to_number(p_xml.extract(p_xpath, p_namespace).getstringval()); + select to_number( extractvalue( p_xml, p_xpath, p_namespace ) ) + into l_returnvalue + from dual; exception when others then l_returnvalue := p_default_value; From 59dd71455bf655ce13e8eae6cafc1213928af903 Mon Sep 17 00:00:00 2001 From: datRedHeadedGuy Date: Fri, 7 Nov 2025 19:26:18 -0600 Subject: [PATCH 2/5] Update xml_util_pkg.pkb Removed original changes and replaced them with a new package function, get_unescaped_string(). --- ora/xml_util_pkg.pkb | 108 ++++++++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 26 deletions(-) diff --git a/ora/xml_util_pkg.pkb b/ora/xml_util_pkg.pkb index 0ffa6c4..81b83c9 100755 --- a/ora/xml_util_pkg.pkb +++ b/ora/xml_util_pkg.pkb @@ -159,6 +159,76 @@ begin end get_string; +function get_unescaped_string( p_escaped in varchar2 ) return varchar2 +as + l_returnvalue string_util_pkg.t_max_pl_varchar2; + l_pos pls_integer; + l_end pls_integer; + l_entity varchar2(50); + l_code pls_integer; + l_char varchar2(10); +begin + + /* + + Purpose: get unescaped string + + Remarks: + + Who Date Description + ------ ---------- -------------------------------- + JMW 07.11.2025 Created + + */ + + l_returnvalue := p_escaped; + + -- Replace standard XML entities + l_returnvalue := replace(l_returnvalue, '&', '&'); + l_returnvalue := replace(l_returnvalue, '<', '<'); + l_returnvalue := replace(l_returnvalue, '>', '>'); + l_returnvalue := replace(l_returnvalue, '"', '"'); + l_returnvalue := replace(l_returnvalue, ''', ''''); + + -- Handle numeric character references (decimal and hex) + l_pos := instr(l_returnvalue, '&#'); + while l_pos > 0 loop + l_end := instr(l_returnvalue, ';', l_pos); + exit when l_end = 0; + + l_entity := substr(l_returnvalue, l_pos + 2, l_end - l_pos - 2); + + begin + if lower(substr(l_entity, 1, 1)) = 'x' then + -- Hexadecimal reference + l_code := to_number(substr(l_entity, 2), 'xxxx'); + else + -- Decimal reference + l_code := to_number(l_entity); + end if; + + -- Convert code point to character safely + if l_code between 0 and 255 then + l_char := chr(l_code); + else + -- Use Unicode escape for high code points + l_char := unistr('\'+lpad(to_char(l_code, 'xxxx'),4,'0')); --' + end if; + + -- Replace the entity in the string + l_returnvalue := substr(l_returnvalue, 1, l_pos - 1) || l_char || substr(l_returnvalue, l_end + 1); + exception + when others then + -- If numeric conversion fails, leave entity as-is + l_end := l_pos; + end; + l_pos := instr(l_returnvalue, '&#', l_pos + length(l_char)); + end loop; + + return l_returnvalue; +end get_unescaped_string; + + function tag_str (p_str in varchar2, p_tag_name in varchar2) return varchar2 as @@ -303,14 +373,14 @@ begin Who Date Description ------ ---------- ------------------------------------- MBR 07.12.2010 Created - JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() - to EXTRACTVALUE(XMLType, xpath) so that special characters - are not escaped + JMW 07.11.2025 Added get_unescaped_string() + so that special characters + in the return value are not escaped */ begin - select extractvalue( xmltype(p_tag), '//@' || p_attr_name ) + select xmltype(p_tag).extract('//@' || p_attr_name).getstringval() into l_returnvalue from dual; exception @@ -318,7 +388,7 @@ begin l_returnvalue := null; end; - l_returnvalue := nvl(l_returnvalue, p_default_value); + l_returnvalue := nvl(get_unescaped_string(l_returnvalue), p_default_value); return l_returnvalue; @@ -342,16 +412,14 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created - JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() - to EXTRACTVALUE(XMLType, xpath) so that special characters - are not escaped + JMW 07.11.2025 Added get_unescaped_string() + so that special characters + in the return value are not escaped */ begin - select extractvalue( p_xml, p_xpath, p_namespace ) - into l_returnvalue - from dual; + l_returnvalue := get_unescaped_string(p_xml.extract(p_xpath, p_namespace).getstringval()); exception when others then l_returnvalue := p_default_value; @@ -380,21 +448,14 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created - JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() - to EXTRACTVALUE(XMLType, xpath) so that special characters - are not escaped */ begin if p_date_format is not null then - select to_date( extractvalue( p_xml, p_xpath, p_namespace ), p_date_format ) - into l_returnvalue - from dual; + l_returnvalue := to_date(p_xml.extract(p_xpath, p_namespace).getstringval(), p_date_format); else - select to_date( substr( extractvalue( p_xml, p_xpath, p_namespace ), 1, 19 ), 'YYYY-MM-DD"T"hh24:mi:ss' ) - into l_returnvalue - from dual; + l_returnvalue := to_date(substr(p_xml.extract(p_xpath, p_namespace).getstringval(), 1, 19), 'YYYY-MM-DD"T"hh24:mi:ss'); end if; exception when others then @@ -423,16 +484,11 @@ begin Who Date Description ------ ---------- -------------------------------- MBR 27.01.2011 Created - JMW 12.04.2018 Changed the use of XMLType.extract(xpath).getStringVal() - to EXTRACTVALUE(XMLType, xpath) so that special characters - are not escaped */ begin - select to_number( extractvalue( p_xml, p_xpath, p_namespace ) ) - into l_returnvalue - from dual; + l_returnvalue := to_number(p_xml.extract(p_xpath, p_namespace).getstringval()); exception when others then l_returnvalue := p_default_value; From 3ac2f07273cfd84cd1bcc961eb8319c20c143476 Mon Sep 17 00:00:00 2001 From: datRedHeadedGuy Date: Fri, 7 Nov 2025 19:29:23 -0600 Subject: [PATCH 3/5] Update xml_util_pkg.pks Added new package function, get_unescaped_string(). --- ora/xml_util_pkg.pks | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ora/xml_util_pkg.pks b/ora/xml_util_pkg.pks index 15e2d6e..bc30417 100755 --- a/ora/xml_util_pkg.pks +++ b/ora/xml_util_pkg.pks @@ -37,12 +37,15 @@ as p_name in varchar2, p_regional_settings in t_regional_settings := null, p_raise_error_if_parse_error in boolean := false) return number; - + -- return a string from DOM node function get_string (p_node in dbms_xmldom.domnode, p_name in varchar2, p_trim_str in boolean := true) return varchar2; - + + -- get unescaped string + function get_unescaped_string( p_escaped in varchar2 ) return varchar2; + -- build tagged string function tag_str (p_str in varchar2, p_tag_name in varchar2) return varchar2; @@ -81,3 +84,4 @@ as end xml_util_pkg; / + From 59bb5159c5bf683cfa56ead695d0fe42b3858cdc Mon Sep 17 00:00:00 2001 From: datRedHeadedGuy Date: Fri, 7 Nov 2025 19:52:38 -0600 Subject: [PATCH 4/5] Update xml_util_pkg.pkb --- ora/xml_util_pkg.pkb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ora/xml_util_pkg.pkb b/ora/xml_util_pkg.pkb index 81b83c9..4071408 100755 --- a/ora/xml_util_pkg.pkb +++ b/ora/xml_util_pkg.pkb @@ -10,6 +10,9 @@ as Who Date Description ------ ---------- ------------------------------------- FDL 03.05.2007 Created + JMW 07.11.2025 Added get_unescaped_string() + so that special characters + in the return value are not escaped */ @@ -502,3 +505,4 @@ end extract_value_number; end xml_util_pkg; / + From bece61135da85e7845e9ade12d27d14afc7ac768 Mon Sep 17 00:00:00 2001 From: datRedHeadedGuy Date: Fri, 7 Nov 2025 19:53:02 -0600 Subject: [PATCH 5/5] Update xml_util_pkg.pks --- ora/xml_util_pkg.pks | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ora/xml_util_pkg.pks b/ora/xml_util_pkg.pks index bc30417..03ef838 100755 --- a/ora/xml_util_pkg.pks +++ b/ora/xml_util_pkg.pks @@ -10,6 +10,9 @@ as Who Date Description ------ ---------- ------------------------------------- FDL 03.05.2007 Created + JMW 07.11.2025 Added get_unescaped_string() + so that special characters + in the return value are not escaped */ @@ -85,3 +88,4 @@ end xml_util_pkg; / +