How to use ExtractVariable policy to extract custom attribute values after AccessEntity policy resulting XML ?

Not applicable

I am trying to extract custom attributes of developer using AccessEntity policy followed by ExtractVariable policy.

My AccessEntity policy looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AccessEntity name="GetDeveloperProfile">
    <DisplayName>GetDeveloperProfile</DisplayName>
    <EntityType value="developer"/>
    <EntityIdentifier ref="request.queryparam.apikey" type="consumerkey"/>
</AccessEntity>

And just after this policy I have added ExtractVariable policy which looks like this :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-Variables-1">
    <DisplayName>Extract Variables-1</DisplayName>
    <Source>AccessEntity.GetDeveloperProfile</Source>
    <VariablePrefix>developer</VariablePrefix>
    <XMLPayload>
        <Variable name="rolevariable" type="string">
            <XPath>/Developer/Attributes/Attribute[Name='role']/Value/text()</XPath>
        </Variable>
    </XMLPayload>
</ExtractVariables>

While testing the policy, I am sending the consumer key in the query parameter and in the trace window I can see that Developer profile is getting extracted in the form of XML after AccessEntity policy gets executed, but then ExtractVariable policy is not able to extract the value of 'role' in the rolevariable.

There is nothing coming in the rolevariable.

But, I am able to extract the value by

AccessEntity.ChildNodes.GetDeveloperProfile.Developer.Attributes.Attribute.2.Value

this variable. There are 2 entries in the custom attributes of the developer. I am trying to refer the value of second entry.

Also,

Value of AccessEntity.ChildNodes.GetDeveloperProfile.Developer.Apps is

<?xml version="1.0" encoding="UTF-8"?><Apps><App>MyNewApp</App><App>MyADapp</App></Apps>

but value of AccessEntity.ChildNodes.GetDeveloperProfile.Developer.Apps.App is

MyADapp

Ideally, it should be MyADapp and MyNewApp, since the developer is registered to two apps.

I don't know what can be the reason behind it. Any help will be appreciated ?

2 4 928
4 REPLIES 4

Not applicable

@Dino

@Anil Sagar

Can you help here, I'll be thankful.

Your XPath needs some correction; please use

<Variable name="rolevariable" type="string">
    <XPath>/Developer/Attributes/Attribute[contains(Name,'role')]/Value/text()</XPath>
</Variable>

This will assign desired value to variable.

For AccessEntity.ChildNodes.GetDeveloperProfile.Developer.Apps.App apigee only assigns last app name.

i tried using XPath to get list of all apps , but XPath functions are not supported by apigee at full extend , something like below would have helped you but its not working ,

<Variable name="apps">
            <XPath>string-join(/Developer/Apps/App/normalize-space(), ',')</XPath>
</Variable>

I suggest to use XMLtoJSON policy on AccessEntity, that way you can access any element you like easily without using extract variable , using simple javascript or java code.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XMLToJSON async="false" continueOnError="false" enabled="true" name="XML-to-JSON-1">
    <DisplayName>XML to JSON-1</DisplayName>
    <Properties/>
    <Format>yahoo</Format>
    <OutputVariable>AccessEntityJSON</OutputVariable>
    <Source>AccessEntity.AccessEntity</Source>
</XMLToJSON>

It will give JSON like below ,

{
	"Developer": {
		"Apps": {
			"App": ["Sample App", "helloApp"]
		},
		"Companies": {},
		"Email": "helloworld@apigee.com",
		"DeveloperId": "6e9df4d6-ed15-45a8-a1e4-26c2f3e8cf64",
		"FirstName": "helloworld",
		"LastName": "dev",
		"UserName": "helloworld",
		"OrganizationName": "****",
		"Status": "active",
		"Attributes": {
			"Attribute": [{
				"Name": "role",
				"Value": "Admin"
			}, {
				"Name": "Valor",
				"Value": "Morgolis"
			}]
		},
		"CreatedAt": 1521443127312,
		"CreatedBy": "accounts_apigee_admin@google.com",
		"LastModifiedAt": 1522324350867,
		"LastModifiedBy": "*****"
	}
}

One additional suggestion: AccessEntity is a heavyweight policy, it takes a little more time for execution. You may want to try using the cache to store the retrieved data for performance optimization if it suits your need.

Great!! Appreciate it Amit.

I tried your solution but again it was giving the same error like mine.

I forgot to notice the

<VariablePrefix>developer</VariablePrefix>

in my ExtractVariable policy. Once I removed that, then your solution started working and mine too.

Thanks for your effort.

Also, converting XML to JSON is a good idea.