I have extracted a value from an XML via XPath and set it to a variable in an Extract Variable Policy.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-Value-1">
<DisplayName>EV-Value-1</DisplayName>
<Source>request</Source>
<XMLPayload stopPayloadProcessing="false">
<Namespaces>
<Namespace prefix="ns0">abc:def:xyz</Namespace>
</Namespaces>
<Variable name="foo" type="string">
<XPath>//ns0:Params//ns0:var1</XPath>
</Variable>
</XMLPayload>
</ExtractVariables>
So variable "foo" now has a value "XY".
How do I split "XY" into "X" and "Y" and assign to two different variables?
I'm using Assign Message Policy, but for some reason substring doesn't work as expected.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage continueOnError="false" enabled="true" name="AM-SplitValues">
<DisplayName>AM-SplitValues</DisplayName>
<AssignVariable>
<Name>VarX</Name>
<Ref>foo</Ref>
<Template>{substring(foo,0,1)}</Template>
</AssignVariable>
<AssignVariable>
<Name>VarY</Name>
<Ref>foo</Ref>
<Template>{substring(foo,1,2)}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
VarX and VarY are empty after this
However, in trace I can see the variable "foo" has the value "XY".
I referred to
https://www.googlecloudcommunity.com/gc/Apigee/AssignMessage-substring-not-working/td-p/73601
and
https://docs.apigee.com/api-platform/reference/message-template-intro
Can anyone please suggest on how to best handle this?
@dchiesa1 @API-Evangelist @sidd-harth Please advise
Solved! Go to Solution.
You don't use Ref. If variable foo has been previously set, then you can do this:
<!-- REQUIRED -->
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignVariable>
<Name>VarX</Name>
<!-- OMIT
<Ref>foo</Ref>
-->
<Template>{substring(foo,0,1)}</Template>
</AssignVariable>
BTW, just for the record, I am repeating myself. I posted this exact information in my original response.
HAVE YOU TRIED THIS?
This works for me,
<AssignMessage name='AM-Assign-Variables-via-Substring'>
<!-- To use numeric values within substring, -->
<!-- specify IgnoreUnresolvedVariables as true, -->
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignVariable>
<Name>foo</Name>
<Value>XY</Value>
</AssignVariable>
<AssignVariable>
<!-- when using Template, omit the Ref element -->
<Name>part1</Name>
<Template>{substring(foo,0,1)}</Template>
</AssignVariable>
<AssignVariable>
<Name>part2</Name>
<Template>{substring(foo,1,2)}</Template>
</AssignVariable>
</AssignMessage>
If you want to avoid using IgnoreUnresolvedVariables , then you can use symbolic names.
<AssignMessage name='AM-Assign-Variables-via-Substring-2'>
<!-- You can use variable names within substring. -->
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignVariable>
<Name>foo</Name>
<Value>XY</Value>
</AssignVariable>
<AssignVariable>
<Name>zero</Name>
<Value>0</Value>
</AssignVariable>
<AssignVariable>
<Name>one</Name>
<Value>1</Value>
</AssignVariable>
<AssignVariable>
<Name>two</Name>
<Value>2</Value>
</AssignVariable>
<AssignVariable>
<!-- when using Template, omit the Ref element -->
<Name>part1</Name>
<Template>{substring(foo,zero,one)}</Template>
</AssignVariable>
<AssignVariable>
<Name>part2</Name>
<Template>{substring(foo,one,two)}</Template>
</AssignVariable>
</AssignMessage>
Hi @dchiesa1
What if I had to use a variable that has been set in a previous policy?
A context variable is just a context variable. It should just work. Have you tried it?
How to read the variable that is set in EV policy, inside an AM policy?
Is there anything wrong with the below usage of Ref and Template?
<AssignVariable>
<Name>VarX</Name>
<Ref>foo</Ref>
<Template>{substring(foo,0,1)}</Template>
</AssignVariable>
You don't use Ref. If variable foo has been previously set, then you can do this:
<!-- REQUIRED -->
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignVariable>
<Name>VarX</Name>
<!-- OMIT
<Ref>foo</Ref>
-->
<Template>{substring(foo,0,1)}</Template>
</AssignVariable>
BTW, just for the record, I am repeating myself. I posted this exact information in my original response.
HAVE YOU TRIED THIS?
Thanks a ton @dchiesa1
This worked... the actual problem I had was with my request node having a comment tag
<P01>
<!-- mandatory -->XY
</P01>
When I changed this to
<P01>XY</P01>
it worked as expected