Loop over an array using xslt

Hi,

I want to loop over an array using xslt and modify certain node values. I want to replace the value-1, value-2 , value-3 and value-4 values using an array that I am using to store the replacement values.

user_perm_array contains the elements that will replace the ones in the xml. But I am not able to replace the values.

Can anyone please help.

xml

<env:Envelope
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header>
        <wsse:Security
            xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
            xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" env:mustUnderstand="1">
            <wsse:UsernameToken wsu:Id="UsernameToken-43">
                <wsse:Username>{simWSUser}</wsse:Username>
            </wsse:UsernameToken>
        </wsse:Security>
    </env:Header>
    <env:Body>
        <ns5:UserRequest>
            <ns5:LogonUserName>{LogonUserName}</ns5:LogonUserName>
            <ns5:Context>{Context}</ns5:Context>
            <ns5:UserKey>{Key}</ns5:UserKey>
            <ns5:PermissionCode>value-1</ns5:PermissionCode>
            <ns5:PermissionCode>value-2</ns5:PermissionCode>
            <ns5:PermissionCode>value-3</ns5:PermissionCode>
            <ns5:PermissionCode>value-4</ns5:PermissionCode>
        </ns5:UserRequest>
    </env:Body>
</env:Envelope>

xslt

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns5="http://www.service.com/service/schema/permission/message/"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    <xsl:param name="user_perm_array" />
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:variable name="el"/>
    <xsl:template match="/env:Envelope/env:Body/ns5:UserRequest/ns5:PermissionCode">
        <xsl:value-of select="$user_perm_array" />
        
    </xsl:template>
     
      
     


Thanks

0 1 16.8K
1 REPLY 1

This is an XSLT question, not really an Apigee question, but I can help. The answer will apply to any XSLT 2.0 environment, including Apigee Edge.

The key is to populate an array with the values you get in the user_perms_array parameter. You can do this with the tokenize() function that is provided in xslt2.0. Then you can replace the text of each PermissionCode element with the item from that array. To get the index for the array lookup, use the count() function on the preceding-sibling axis, but limited only to PermissionCode elements.

With your XML as input, if I use this XSL ....

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:ns5="http://www.service.com/service/schema/permission/message/"
    xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">

  <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:strip-space elements="*"/>

    <xsl:param name="user_perm_array" select="'green blue grey black'"/>

    <!-- keep this array for later -->
    <xsl:variable name="vPerms" select="tokenize($user_perm_array, ' ')"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/env:Envelope/env:Body/ns5:UserRequest/ns5:PermissionCode">
      <!-- the number of THIS PermCode element, among all PermCode elements -->
      <xsl:variable name='vRelativeCount' 
                 select="count(preceding-sibling::ns5:PermissionCode) + 1"/>

      <!-- index into the vPerms array, according to the position -->
      <ns5:PermissionCode><xsl:value-of select="$vPerms[$vRelativeCount]"/></ns5:PermissionCode>

    </xsl:template>

</xsl:stylesheet>


...then I get this XML output:

<env:Envelope xmlns:ns5="http://www.service.com/service/schema/permission/message/"
              xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header>
      <wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                     xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
                     env:mustUnderstand="1">
         <wsse:UsernameToken wsu:Id="UsernameToken-43">
            <wsse:Username>{simWSUser}</wsse:Username>
         </wsse:UsernameToken>
      </wsse:Security>
   </env:Header>
   <env:Body>
      <ns5:UserRequest>
         <ns5:LogonUserName>{LogonUserName}</ns5:LogonUserName>
         <ns5:Context>{Context}</ns5:Context>
         <ns5:UserKey>{Key}</ns5:UserKey>
         <ns5:PermissionCode>green</ns5:PermissionCode>
         <ns5:PermissionCode>blue</ns5:PermissionCode>
         <ns5:PermissionCode>grey</ns5:PermissionCode>
         <ns5:PermissionCode>black</ns5:PermissionCode>
      </ns5:UserRequest>
   </env:Body>
</env:Envelope>