EDIT: By removing xmlns I tried to solve a problem when Mapping Node wouldn't parse Input Message with fields that contain namespace. Input Body is set manually - not from XSD. When namespaces are removed from file manually everything works. But when I do use ESQL script it stops working for some uknown to me reason.
Hello I have XML message from which I need to remove namespaces. What I am trying to do is recursively walk trough XML tree and remove xmlns attribute if exists. Unfortunatelly when I'm trying to do SET element = NULL
my loop won't go to the next element MOVE element NEXTSIBLING
. I tried to do DELETE FIELD element
but that gives same effect.
Here's my full code:
CREATE COMPUTE MODULE test_Compute1
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
DECLARE blobMsg BLOB Environment.BLOB.BLOB ;
CREATE LASTCHILD OF Environment.Variables.inpMsg DOMAIN ('XMLNSC') NAME 'XMLNSC';
CREATE LASTCHILD OF Environment.Variables.inpMsg.XMLNSC PARSE(blobMsg OPTIONS FolderBitStream CCSID InputRoot.Properties.CodedCharSetId FORMAT 'XMLNSC');
SET Environment.Variables.statusRes.statusCode = Environment.Variables.inpMsg.XMLNSC.errorResponse.httpCode;
SET Environment.Variables.statusRes.detail = Environment.Variables.inpMsg.XMLNSC.errorResponse.httpMessage;
SET Environment.Variables.statusRes.additionalStatus.detail = Environment.Variables.inpMsg.XMLNSC.errorResponse.moreInformation;
CALL NavigateTree(Environment.Variables.inpMsg.XMLNSC);
RETURN TRUE;
END;
CREATE PROCEDURE NavigateTree(IN root REFERENCE)
BEGIN
DECLARE element REFERENCE TO root;
DECLARE hint CHARACTER;
DECLARE test CHARACTER;
SET test = '';
SET hint = '';
MOVE element FIRSTCHILD;
-----------
IF LASTMOVE(element) THEN
SET hint = 'has children';
ELSE
IF FIELDNAME(element) = 'xmlns' THEN
DELETE FIELD element;
END IF;
END IF;
WHILE LASTMOVE(element) DO
-- not working awell:
-- DECLARE space1 NAMESPACE 'namespace1';
-- SET element.(XML.NamespaceDecl)* = NULL;
DECLARE nameField2 CHARACTER FIELDNAMESPACE(element);
DECLARE nameField CHARACTER FIELDNAME(element);
DECLARE ifhint CHARACTER;
CALL NavigateTree(element);
MOVE element NEXTSIBLING;
END WHILE;
SET hint = 'finished';
END;
END MODULE;
Do you have any ideas how can I do that?
EDIT:
CREATE COMPUTE MODULE test_Compute1
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN
DECLARE blobMsg BLOB Environment.BLOB.BLOB ;
CREATE LASTCHILD OF Environment.Variables.inpMsg DOMAIN ('XMLNSC') NAME 'XMLNSC';
CREATE LASTCHILD OF Environment.Variables.inpMsg.XMLNSC PARSE(blobMsg OPTIONS FolderBitStream CCSID InputRoot.Properties.CodedCharSetId FORMAT 'XMLNSC');
SET Environment.Variables.statusRes.statusCode = Environment.Variables.inpMsg.XMLNSC.errorResponse.httpCode;
SET Environment.Variables.statusRes.detail = Environment.Variables.inpMsg.XMLNSC.errorResponse.httpMessage;
SET Environment.Variables.statusRes.additionalStatus.detail = Environment.Variables.inpMsg.XMLNSC.errorResponse.moreInformation;
CALL StripNamespaces(Environment.Variables.inpMsg);
RETURN TRUE;
END;
CREATE PROCEDURE StripNamespaces(IN fieldRef REFERENCE)
BEGIN
IF FIELDTYPE(fieldRef) IN (XMLNSC.NamespaceDecl, XMLNSC.SingleNamespaceDecl) THEN
DELETE FIELD fieldRef;
RETURN;
END IF;
DECLARE childRef REFERENCE TO fieldRef;
MOVE childRef FIRSTCHILD;
WHILE LASTMOVE(childRef) DO
DECLARE currentChildRef REFERENCE TO childRef;
MOVE childRef NEXTSIBLING;
CALL StripNamespaces(currentChildRef);
END WHILE;
END;
END MODULE;
XML that is working as an input file that goes to mapping node (manually edited):
<?xml version="1.0" encoding="utf-8"?>
<Receive >
<messageData>
<CD >
<EXP />
<EXAMPLE />
</CD>
<XRP >
<EX1>
<SEG>string</SEG>
<SEG2>integer</SEG2>
</EX1>
<ARRAY>
<AR1>string</AR1>
<AR2 />
</ARRAY>
<ARRAY>
<AR1>integer</AR1>
<AR2 />
</ARRAY>
</XRP>
</messageData>
</Receive>
XML that is not working (mapping node can't process it properly after it goes trough parsing mentioned above):
<?xml version="1.0" encoding="utf-8"?>
<Receive xmlns="namespace">
<messageData>
<CD xmlns="namespace2">
<EXP xmlns="namespace3" />
<EXAMPLE xmlns="namespace3" />
</CD>
<XRP xmlns="namespace2">
<EX1>
<SEG>string</SEG>
<SEG2>integer</SEG2>
</EX1>
<ARRAY>
<AR1>string</AR1>
<AR2 />
</ARRAY>
<ARRAY>
<AR1>integer</AR1>
<AR2 />
</ARRAY>
</XRP>
</messageData>
</Receive>
In both cases debugger shows same tree structure after going trough same parser:
Variables
inpMsg
XMLNSC
Receive
messageData
CD
EXP
EXAMPLE
XRP
EX1
SEG:CHARACTER:string
SEG2:CHARACTER:integer
ARRAY
AR1:CHARACTER:string
AR2
ARRAY
AR1:CHARACTER:integer
AR2
After deletion of the field you have to leave the recursive function.
This is how we do it:
CREATE PROCEDURE StripNamespaces(IN fieldRef REFERENCE)
BEGIN
IF FIELDTYPE(fieldRef) IN (XMLNSC.NamespaceDecl, XMLNSC.SingleNamespaceDecl) THEN
DELETE FIELD fieldRef;
RETURN;
ELSEIF FIELDNAMESPACE(fieldRef) <> '' THEN
SET fieldRef NAMESPACE = '';
END IF;
DECLARE childRef REFERENCE TO fieldRef;
MOVE childRef FIRSTCHILD;
WHILE LASTMOVE(childRef) DO
DECLARE currentChildRef REFERENCE TO childRef;
MOVE childRef NEXTSIBLING;
CALL StripNamespaces(currentChildRef);
END WHILE;
END;