Hi, Enrich mediator inside for-each not working properly :

Please consider what I have tried so far and Please Guide.

I have to add(suffix) a random number, to the values present in below tags in 
input request xml. 1) _JsonReader_PS_id2) _JsonReader_PS_ref
Input xml, Current Output xml,  Expected Output xml are attached.
After executing the sequences  attached in mail (UpdatingIds.xml, 
Sequence.xml), I expected that the value at line 63 in 
foreach_currentOutput.xml would be modified .
<Parent> <_JsonReader_PS_ref>1</_JsonReader_PS_ref></Parent>
It should have got modified to 
<Parent> <_JsonReader_PS_ref>16869</_JsonReader_PS_ref></Parent>
Similarly the value at line 27 , should not have been modified.
I also tried modifying other ids, by calling Sequence.xml from inside "For-each 
in Sequence.xml" if xpath=//Children/Children exists . But that also lead to 
many other wrong updations
Thanks 

    On Tuesday, 16 January 2018 1:59 PM, vikram nayak <[email protected]> 
wrote:
 

 Hi,
Tried to explain little more.I have to append a random number to "too many ids" 
present in my request.
Actual request consists of thousands of lines and there are many ids in it.
some ids are within array , some are direct elements.sample input : {  "id" : 
"1",   {    "id":"2"      }}
I think this could be achieved by doing following for each id present in 
request.
1.  by first acessing the id in a property say property1.2. Then generating the 
random Number in property2.3. Then creating third property, to where i assign 
value of "property1 + property2" to  property3.5. Then assign property 3 to 
first id in request using enrich mediator.
But I think this is not ideal way to do it. Since I have to do it for many id's 
in request.There should be some way to enrich request directly using one enrich 
mediator somewhat  like below
$body.Lookups.$id = $body.Lookups.$id +  get-property('randomNumber');
For below sample input 

Suppose in this loop the randomNumber generated is 100 and kept in property1.
{  "id" : "1",   {    "id":"2"      }   .   .   .   .   "id"="100"}
sample output should be like below :
{  "id" : "1100",   {    "id":"2100"      }   .   .   .   .   "id": "100100"}
i.e. original value + "100"
i.e. I need to add suffix at the end of each id in request.
Please guide.
Thanks,


 

    On Tuesday, 16 January 2018 12:17 PM, vikram nayak <[email protected]> 
wrote:
 

 Hi,
I have to add a random number to too many ids present in my request.
Actual request consists of thousands of lines and there are many ids in it.
some ids are within array , some are direct elements.
{  "id" : "1",   {    "id":"2"   }}
1. I can do this by first aceessing the id in a property say property1.2. Then 
generating the random Number in property2.3. Then creating third property, to 
where i assign value of property1 + property2 to  property3.5. Then assign 
property 3 to first id in request using enrich mediator.
But I think this is not ideal way to do it. Since I have to do it for many id's 
in request.There should be some way to enrich request directly using one enrich 
mediator
$body.Lookups.$id = $body.Lookups.$id +  get-property('randomNumber');
Thanks

   

   
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
	<soapenv:Body>
		<jsonObject>
			<_JsonReader_PS_id>16869</_JsonReader_PS_id>
			<Children>				
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>46869</_JsonReader_PS_id>
				
				<?xml-multiple  Children?>
				<Children>
					<_JsonReader_PS_id>5</_JsonReader_PS_id>					
					<Instance>
						<_JsonReader_PS_id>6</_JsonReader_PS_id>						
						<?xml-multiple  Children?>
						<Children>
							<_JsonReader_PS_id>7</_JsonReader_PS_id>							
							<?xml-multiple  Children?>
							<Children>
								<_JsonReader_PS_id>8</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>10</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>12</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
								</Children>
							<Children>
								<_JsonReader_PS_id>14</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>16</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Parent>
								<_JsonReader_PS_ref>6</_JsonReader_PS_ref>
							</Parent>
						</Children>
						<Parent>
							<_JsonReader_PS_ref>5</_JsonReader_PS_ref>
						</Parent>
						</Instance>
					<Guid>b5c75f37-6381-45f8-806b-c22a49ab6962</Guid>
					<?xml-multiple  Children?>
					<Parent>
						<_JsonReader_PS_ref>4</_JsonReader_PS_ref>
					</Parent>
					</Children>
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>196869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent></Children>
			<Children>
				<_JsonReader_PS_id>216869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
				</Children>
			<Children>
				<_JsonReader_PS_id>236869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
		    </Children>
		</jsonObject>
	</soapenv:Body>
</soapenv:Envelope>
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
	<soapenv:Body>
		<jsonObject>
			<_JsonReader_PS_id>16869</_JsonReader_PS_id>
			<Children>				
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>46869</_JsonReader_PS_id>
				
				<?xml-multiple  Children?>
				<Children>
					<_JsonReader_PS_id>56869</_JsonReader_PS_id>					
					<Instance>
						<_JsonReader_PS_id>66869</_JsonReader_PS_id>						
						<?xml-multiple  Children?>
						<Children>
							<_JsonReader_PS_id>76869</_JsonReader_PS_id>							
							<?xml-multiple  Children?>
							<Children>
								<_JsonReader_PS_id>86869</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>106869</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>126869</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
								</Children>
							<Children>
								<_JsonReader_PS_id>146869</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>166869</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>76869</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Parent>
								<_JsonReader_PS_ref>66869</_JsonReader_PS_ref>
							</Parent>
						</Children>
						<Parent>
							<_JsonReader_PS_ref>56869</_JsonReader_PS_ref>
						</Parent>
						</Instance>
					<Guid>b5c75f37-6381-45f8-806b-c22a49ab6962</Guid>
					<?xml-multiple  Children?>
					<Parent>
						<_JsonReader_PS_ref>46869</_JsonReader_PS_ref>
					</Parent>
					</Children>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>196869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent></Children>
			<Children>
				<_JsonReader_PS_id>216869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
				</Children>
			<Children>
				<_JsonReader_PS_id>236869</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>16869</_JsonReader_PS_ref>
				</Parent>
		    </Children>
		</jsonObject>
	</soapenv:Body>
</soapenv:Envelope>
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/";>
	<soapenv:Body>
		<jsonObject>
			<_JsonReader_PS_id>1</_JsonReader_PS_id>
			<Children>				
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>4</_JsonReader_PS_id>
				
				<?xml-multiple  Children?>
				<Children>
					<_JsonReader_PS_id>5</_JsonReader_PS_id>					
					<Instance>
						<_JsonReader_PS_id>6</_JsonReader_PS_id>						
						<?xml-multiple  Children?>
						<Children>
							<_JsonReader_PS_id>7</_JsonReader_PS_id>							
							<?xml-multiple  Children?>
							<Children>
								<_JsonReader_PS_id>8</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>10</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>12</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
								</Children>
							<Children>
								<_JsonReader_PS_id>14</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Children>
								<_JsonReader_PS_id>16</_JsonReader_PS_id>								
								<?xml-multiple  Children?>
								<Parent>
									<_JsonReader_PS_ref>7</_JsonReader_PS_ref>
								</Parent>
							</Children>
							<Parent>
								<_JsonReader_PS_ref>6</_JsonReader_PS_ref>
							</Parent>
						</Children>
						<Parent>
							<_JsonReader_PS_ref>5</_JsonReader_PS_ref>
						</Parent>
						</Instance>
					<Guid>b5c75f37-6381-45f8-806b-c22a49ab6962</Guid>
					<?xml-multiple  Children?>
					<Parent>
						<_JsonReader_PS_ref>4</_JsonReader_PS_ref>
					</Parent>
					</Children>
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent>
			</Children>
			<Children>
				<_JsonReader_PS_id>19</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent></Children>
			<Children>
				<_JsonReader_PS_id>21</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent>
				</Children>
			<Children>
				<_JsonReader_PS_id>23</_JsonReader_PS_id>				
				<?xml-multiple  Children?>
				<Parent>
					<_JsonReader_PS_ref>1</_JsonReader_PS_ref>
				</Parent>
		    </Children>
		</jsonObject>
	</soapenv:Body>
</soapenv:Envelope>
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="Sequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse";>
    <log level="full">
        <property name="property_name" value="Full logs -------------------------------"/>
    </log>
    
    <enrich>
        <source clone="true" xpath="fn:concat(//Children/_JsonReader_PS_id,get-property('randomNumber'))"/>
        <target xpath="//Children/_JsonReader_PS_id"/>
    </enrich>
    <enrich>
        <source clone="true" xpath="fn:concat(//Children/Parent/_JsonReader_PS_ref,get-property('randomNumber'))"/>
        <target xpath="//Children/Parent/_JsonReader_PS_ref"/>
    </enrich>      
   
</sequence>
<?xml version="1.0" encoding="UTF-8"?>
<sequence name="UpdatingIds" trace="disable" xmlns="http://ws.apache.org/ns/synapse";>
    <script language="js"><![CDATA[var randomNum,log ;
var log = mc.getServiceLog();        
log.info("Logging inside Script Mediator");
randomNum = Math.floor(Math.random()*10000);
log.info("Logging inside Script Mediator" + randomNum );
mc.setProperty("randomNumber", randomNum);]]></script>
    <!--   <property name="messageType" scope="axis2" type="STRING" value="application/json"/>  -->
    <enrich>
        <source clone="true" xpath="fn:concat(//jsonObject/_JsonReader_PS_id,get-property('randomNumber'))"/>
        <target xpath="//jsonObject/_JsonReader_PS_id"/>
    </enrich>
    <filter description="" xpath="//jsonObject/Parent">
        <then>
            <enrich>
                <source clone="true" xpath="fn:concat(//jsonObject/Parent/_JsonReader_PS_ref,get-property('randomNumber'))"/>
                <target xpath="//jsonObject/Parent/_JsonReader_PS_ref"/>
            </enrich>
        </then>
        <else/>
    </filter>
    <foreach expression="//jsonObject/Children" id="loop">
        <sequence>
            <sequence key="Sequence"/>
        </sequence>
    </foreach>
    <log level="full">
        <property name="property_name" value="test********************************************"/>
    </log>
    <log level="custom">
        <property expression="get-property('MessageID')" name="MessageID"/>
        <property expression="get-property('randomNumber')" name="randomNumber"/>
    </log>
</sequence>
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to