Can I add an XML array to the Properties of the JavaScript Policy?

ayheber
Participant I

Can I add an XML array to the Properties of the JavaScript Policy?

I want to add a list of variables (e.g. "Labels"), to use within the JS code.

I know I can add:

<Properties> 
	<Property name="labels">label1=val1, label2=val2</Property> 
</Properties>

But I want to do it as:

<Properties>
	<Property name="labels">
		<Label name="label1">val1</Label>
		<Label name="label2">val2</Label>
	</Property>
</Properties>

Is this possible?

Solved Solved
0 1 182
1 ACCEPTED SOLUTION

Mmmm, I haven't tried that but I suppose it will work. Let me try.

EDIT. Ok here's what I found. I was not able to get access to a property that was formatted in XML. I had to use a CDATA section to get it to work.

<Javascript name='JS-ParseXmlProperty'>
  <Properties>
    <Property name='labels'><![CDATA[


      <Labels>
        <Label name="label1">val1</Label>
        <Label name="label2">val2</Label>
      </Labels>


    ]]>
    </Property>
  </Properties>
  <ResourceURL>jsc://parseXmlProperty.js</ResourceURL>
</Javascript>


With the property formatted that way I was able to use E4X to iterate through the properties. Like this:

var labelsProp = properties.labels.trim();

var xml = new XML(labelsProp);
var labelList = new XMLList(xml.Label);
for each(var i in labelList) {
   if (i.nodeKind() == 'element') {
     context.setVariable('Label.' + i.@name + '.text', i.text());
   }
}

If I try it without the CDATA, then ... the JS logic does not get the XML "outer text" of the labels property.

A different way to do it would be to refer to a VARIABLE in the property. Like this:

<Javascript name='JS-ParseXmlProperty'>
  <Properties>
    <Property name='labels-variable'>variable-name-here</Property>
  </Properties>
  <ResourceURL>jsc://parseXmlProperty.js</ResourceURL>
</Javascript>
 

and then the JS looks like this:

var varname = properties['labels-variable'];
var labelsProp = context.getVariable(varName).trim();

...rest of script is the same....

And of course, if you take this approach, you must have the XML in the named variable, before the proxy executes the JS step.

Here is a working proxy that demonstrates various cases. Case t2 and t4 "work". t1 and t3 do not use CDATA and they don't "work".

apiproxy-js-xml-in-property-20210414-185303.zip

View solution in original post

1 REPLY 1

Mmmm, I haven't tried that but I suppose it will work. Let me try.

EDIT. Ok here's what I found. I was not able to get access to a property that was formatted in XML. I had to use a CDATA section to get it to work.

<Javascript name='JS-ParseXmlProperty'>
  <Properties>
    <Property name='labels'><![CDATA[


      <Labels>
        <Label name="label1">val1</Label>
        <Label name="label2">val2</Label>
      </Labels>


    ]]>
    </Property>
  </Properties>
  <ResourceURL>jsc://parseXmlProperty.js</ResourceURL>
</Javascript>


With the property formatted that way I was able to use E4X to iterate through the properties. Like this:

var labelsProp = properties.labels.trim();

var xml = new XML(labelsProp);
var labelList = new XMLList(xml.Label);
for each(var i in labelList) {
   if (i.nodeKind() == 'element') {
     context.setVariable('Label.' + i.@name + '.text', i.text());
   }
}

If I try it without the CDATA, then ... the JS logic does not get the XML "outer text" of the labels property.

A different way to do it would be to refer to a VARIABLE in the property. Like this:

<Javascript name='JS-ParseXmlProperty'>
  <Properties>
    <Property name='labels-variable'>variable-name-here</Property>
  </Properties>
  <ResourceURL>jsc://parseXmlProperty.js</ResourceURL>
</Javascript>
 

and then the JS looks like this:

var varname = properties['labels-variable'];
var labelsProp = context.getVariable(varName).trim();

...rest of script is the same....

And of course, if you take this approach, you must have the XML in the named variable, before the proxy executes the JS step.

Here is a working proxy that demonstrates various cases. Case t2 and t4 "work". t1 and t3 do not use CDATA and they don't "work".

apiproxy-js-xml-in-property-20210414-185303.zip