This is a followup to Extract value from XML with "unknown" namespace
Now it's the other way round. I need to put my XML into a Container-XML. Same issue: there is a different container for each of my XML-Types. My idea is that I create a table with namespace for my XML and the corresponding container. Then I can replace the values.
I need to fill the Creation Date (CreDtTime), the Hash (HashValue) and of course my document (Document). The element named <MsgPain008> differs according to the container-type, e.g. it can be <MsgPain001>. If it is not possible to make it work for different MsgPain… elements then it can be dynamic SQL
I know this code does not work but my XQuery skills are only rudimentary.
DECLARE
containerXML XMLTYPE := XMLTYPE(
'<?xml version="1.0" encoding="UTF-8"?>
<conxml xmlns="urn:conxml:xsd:container.nnn.001.GBIC4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:conxml:xsd:container:nnn.001.GBIC4 container.nnn.001.GBIC4.xsd">
<CreDtTm></CreDtTm>
<MsgPain008>
<HashValue></HashValue>
<HashAlgorithm>SHA256</HashAlgorithm>
<Document/>
</MsgPain008>
</conxml>'
);
documentXML XMLTYPE := XMLTYPE('<Document><someElements>hml</someElements></Document>');
v_clob CLOB;
t XMLTYPE;
BEGIN
SELECT XMLQuery(
'copy $i := $p1 modify
((for $j in $i/conxml/CreDtTm
return replace value of node $j with $p2),
(for $j in $i//HashValue
return replace value of node $j with $p3),
(for $j in $i//Document
return replace node $j with $p4))
return $i'
PASSING containerXML AS "p1",
TO_CHAR(SYSDATE,'yyyy-mm-dd') AS "p2",
'hash_4356hbthlvmbh' AS "p3",
documentXML AS "p4"
RETURNING CONTENT)
INTO t
FROM dual;
v_clob := t.getClobVal();
dbms_output.put_line(v_clob);
END;
/
The expected output
<?xml version="1.0" encoding="UTF-8"?>
<conxml xmlns="urn:conxml:xsd:container.nnn.001.GBIC4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:conxml:xsd:container:nnn.001.GBIC4 container.nnn.001.GBIC4.xsd">
<CreDtTm>2025-11-03</CreDtTm>
<MsgPain008>
<HashValue>hash_4356hbthlvmbh</HashValue>
<HashAlgorithm>SHA256</HashAlgorithm>
<Document><someElements>hml</someElements></Document>
</MsgPain008>
</conxml>