Author |
Message
|
whydieanut |
Posted: Mon Nov 04, 2013 4:46 am Post subject: Testing for an empty complex element in XML domain |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
I need to check the existence of a complex element in a given XML.
The problem is that the code is pretty old and uses the XML domain.
And the way the data is entered, adds new line characters between elements/tags.
Thus using EXISTS(child of field to test) returns true if the element is empty with a new line separating the open and close tags.
Is the only option, to move through all children of <field_to_test> and test if their FIELDNAME isn't empty? Like so:
Code: |
IF EXISTS(InRef.parent.field_to_test[]) THEN
MOVE cursor TO InRef.parent.field_to_test.*[];
WHILE LASTMOVE(cursor) DO
IF (FIELDNAME(cursor) <> '') THEN
SET Flag = 'YES';
-- Somehow break out of the WHILE loop
END IF;
END WHILE;
END IF; |
In the below examples, I need to check for the field "<field_to_test>" to be present and have other tags within it (these tags aren't fixed).
What is considered valid is:
Code: |
1.
<parent>
<field_to_test>
<child1>value1</child1>
<child2>value2</child2>
</field_to_test>
<other_field>value3<other_field>
</parent>
|
What is considered invalid is:
(The problematic scenario is 1.)
Code: |
1.
<parent>
<field_to_test>
</field_to_test>
<other_field>value3<other_field>
</parent>
2.
<parent>
<field_to_test></field_to_test>
<other_field>value3<other_field>
</parent>
3.
<parent>
<field_to_test/>
<other_field>value3<other_field>
</parent>
4.
<parent>
<other_field>value3<other_field>
</parent>
|
|
|
Back to top |
|
 |
Esa |
Posted: Mon Nov 04, 2013 11:00 am Post subject: Re: Testing for an empty complex element in XML domain |
|
|
 Grand Master
Joined: 22 May 2008 Posts: 1387 Location: Finland
|
whydieanut wrote: |
I need to check the existence of a complex element in a given XML.
The problem is that the code is pretty old and uses the XML domain.
And the way the data is entered, adds new line characters between elements/tags.
Thus using EXISTS(child of field to test) returns true if the element is empty with a new line separating the open and close tags.
Is the only option, to move through all children of <field_to_test> and test if their FIELDNAME isn't empty? |
If you will have to change the code anyway, have you considered migrating to XMLNSC? |
|
Back to top |
|
 |
kimbert |
Posted: Mon Nov 04, 2013 12:44 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
You are trying to find out whether the tag contains any child tags. So this should do it:
Code: |
IF EXISTS(InRef.parent.field_to_test.(XML.element)*[])) THEN... |
You may also need to check for child attributes as well - your examples did not have any tests for that, so maybe the tag does not have any attributes? _________________ Before you criticize someone, walk a mile in their shoes. That way you're a mile away, and you have their shoes too. |
|
Back to top |
|
 |
fjb_saper |
Posted: Mon Nov 04, 2013 8:26 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
kimbert wrote: |
You are trying to find out whether the tag contains any child tags. So this should do it:
Code: |
IF EXISTS(InRef.parent.field_to_test.(XML.element)*[])) THEN... |
You may also need to check for child attributes as well - your examples did not have any tests for that, so maybe the tag does not have any attributes? |
What about checking for child namespace declarations ?  _________________ MQ & Broker admin |
|
Back to top |
|
 |
dogorsy |
Posted: Tue Nov 05, 2013 12:26 am Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
fjb_saper wrote: |
kimbert wrote: |
You are trying to find out whether the tag contains any child tags. So this should do it:
Code: |
IF EXISTS(InRef.parent.field_to_test.(XML.element)*[])) THEN... |
You may also need to check for child attributes as well - your examples did not have any tests for that, so maybe the tag does not have any attributes? |
What about checking for child namespace declarations ?  |
Namespaces not supported by the XML parser. Need to use either XMLNS or XMLNSC for that |
|
Back to top |
|
 |
dogorsy |
Posted: Tue Nov 05, 2013 12:29 am Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
kimbert wrote: |
You are trying to find out whether the tag contains any child tags. So this should do it:
Code: |
IF EXISTS(InRef.parent.field_to_test.(XML.element)*[])) THEN... |
You may also need to check for child attributes as well - your examples did not have any tests for that, so maybe the tag does not have any attributes? |
:
Code: |
IF EXISTS(InRef.parent.field_to_test.*[])) THEN... |
will detect child attributes as well. |
|
Back to top |
|
 |
whydieanut |
Posted: Tue Nov 05, 2013 12:52 am Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
Quote: |
If you will have to change the code anyway, have you considered migrating to XMLNSC? |
We are using XMLNSC for any new developments, but moving existing code to XMLNSC has too much dependency/regression testing needed, that the team can't afford at the moment. So we have to work with XML.
I know it's less than ideal, but that's how it is, for now.
Quote: |
What about checking for child namespace declarations ? |
No namespaces are being used.
Quote: |
You are trying to find out whether the tag contains any child tags. So this should do it:
Code:
IF EXISTS(InRef.parent.field_to_test.(XML.element)*[])) THEN...
You may also need to check for child attributes as well - your examples did not have any tests for that, so maybe the tag does not have any attributes? |
The field_to_test may or may not have attributes, am not very sure. Will need to check if it does, and also if the existence of attribute is a positive case or not.
Now, assuming there are no attributes, I need to test for the existence of the <field_to_test> itself AND any child TAGS (and not text nodes because of the \n).
So specifying XML.Element will suffice, meaning it'll leave out any text nodes (caused by \n)?
Don't have access to a system to test it right now, hence asking here itself  |
|
Back to top |
|
 |
dogorsy |
Posted: Tue Nov 05, 2013 12:54 am Post subject: |
|
|
Knight
Joined: 13 Mar 2013 Posts: 553 Location: Home Office
|
as said above
Code: |
IF EXISTS(InRef.parent.field_to_test.*[])) THEN... |
is what you need. |
|
Back to top |
|
 |
whydieanut |
Posted: Tue Nov 05, 2013 2:33 am Post subject: |
|
|
 Disciple
Joined: 02 Apr 2010 Posts: 186
|
This returns true even for non element text nodes (like \n)
Will test if XML.Element solves this. |
|
Back to top |
|
 |
|