Author |
Message
|
chris boehnke |
Posted: Thu May 28, 2015 11:20 am Post subject: check for existence of an element |
|
|
 Partisan
Joined: 25 Jul 2006 Posts: 369
|
Hi All,
How do we check for existence of XML tag in a incoming XML element.
Below is the sample xml. I want to check if "Test2" tag exists or not.
<Test1>
<Test2>
<value>556</value>
<description>hfsdkh</description>
</Test2>
</Test1> |
|
Back to top |
|
 |
Vitor |
Posted: Thu May 28, 2015 11:22 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Do you mean you want to test to see if you have:
Code: |
<Test1>
<Test2>
<value>556</value>
<description>hfsdkh</description>
</Test2>
</Test1>
|
or this:
Code: |
<Test1>
<value>556</value>
<description>hfsdkh</description>
</Test1>
|
_________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
chris boehnke |
Posted: Thu May 28, 2015 11:26 am Post subject: |
|
|
 Partisan
Joined: 25 Jul 2006 Posts: 369
|
I want to check if I have "Test2" xml element |
|
Back to top |
|
 |
Vitor |
Posted: Thu May 28, 2015 11:31 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
chris boehnke wrote: |
I want to check if I have "Test2" xml element |
But why?
If you want know if you have it, you can test directly; if you want to know if you have it because you want to access the value & description elements you can access them anonymously.
In XML terms, are you interested in the existence or not of the complex element Test2, or interested in the child elements?
It only matters because it changes the code you need to do it. _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
Vitor |
Posted: Thu May 28, 2015 11:32 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
Consider:
Code: |
<Test1>
<Test2>
<value>556</value>
<description>hfsdkh</description>
</Test2>
<Test3>
<value>557</value>
<description>asadsd</description>
</Test3>
</Test1>
|
Still interested in the existence of Test2? Or the number of descriptions you have? _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
chris boehnke |
Posted: Thu May 28, 2015 11:44 am Post subject: |
|
|
 Partisan
Joined: 25 Jul 2006 Posts: 369
|
I still need to test the existence of XML element to do the transformation. |
|
Back to top |
|
 |
Vitor |
Posted: Thu May 28, 2015 11:55 am Post subject: |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
chris boehnke wrote: |
I still need to test the existence of XML element to do the transformation. |
Then you need something like:
Code: |
IF InputRoot.XMLNSC.Test1.Test2 IS NOT NULL THEN
|
or
Code: |
MOVE someReference TO InputRoot.XMLNSC.Test1.Test2;
IF LASTMOVE(someReference) THEN
|
or
Code: |
MOVE someReference TO InputRoot.XMLNSC.Test1;
MOVE someReference FIRSTCHILD;
IF FIELDNAME(someReference) = 'Test2' THEN
|
or
... _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
fjb_saper |
Posted: Thu May 28, 2015 12:05 pm Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
And think about the odd case
Code: |
<test1>
<test2 xsi:nil="true"/>
</test1> |
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
chris boehnke |
Posted: Fri May 29, 2015 7:58 am Post subject: |
|
|
 Partisan
Joined: 25 Jul 2006 Posts: 369
|
Thanks Vitor and fjb!!
Code: |
IF InputRoot.XMLNSC.Test1.Test2 IS NOT NULL THEN |
Doesnt work as expected. This statement checks for the value of Test2.
I tried the below one and it works.
Code: |
MOVE someReference TO InputRoot.XMLNSC.Test1.Test2;
IF LASTMOVE(someReference) THEN |
Thanks!! |
|
Back to top |
|
 |
inMo |
Posted: Fri May 29, 2015 9:25 am Post subject: |
|
|
 Master
Joined: 27 Jun 2009 Posts: 216 Location: NY
|
To be sure, your solution will check for the presence of Test2. If Test2 is present, but empty, your test will still prove true. As long as your intention is to check for the absence of the element Test2 your code is fine. If however your requirement is such that there is no difference between the absence of Test2 and the presence of an empty Test2, you will need to do something different. |
|
Back to top |
|
 |
mqjeff |
Posted: Fri May 29, 2015 9:35 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
I think I have had some success testing for a missing element by testing if the FIELDNAME is null. |
|
Back to top |
|
 |
rekarm01 |
Posted: Sat May 30, 2015 1:47 am Post subject: Re: check for existence of an element |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 1415
|
chris boehnke wrote: |
How do we check for existence of XML tag in a incoming XML element. |
Aside from some of the suggestions already given, there is also the EXISTS() function:
Code: |
IF EXISTS(InputRoot.XMLNSC.Test1.Test2[]) THEN ... |
|
|
Back to top |
|
 |
|