DataPower

 View Only
  • 1.  Update XML Field value using XSLT

    Posted Thu December 16, 2021 02:18 PM
    Edited by Alison Donato Fri December 17, 2021 08:45 AM
    Hi All,

    I am trying to update XML field value using gateway script was not successful somehow is not able to update the field value

    Sample Message :
    <Student>
    <Name>Adarsh</Name>
    <Age>32</Age>
    <Student>

    This what I wrote in Gateway script but somehow was not able to update the value  :
    inputXMLRequest.getElementsByTagName('Name').textContent = 'John';
    Assuming inputXMLRequest is correctly parsed.
    Can anyone please help me how to implement this using XSLT in API conect as I do not have experience in XSLT.

    ------------------------------
    Adarsh Thakur
    ------------------------------


  • 2.  RE: Update XML Field value using XSLT

    Posted Fri December 17, 2021 09:36 AM

    Hi Adarsh,

    Only the get functions have been implemented for the DOM API objects in GatewayScript.  Your two choices are

    1) Convert your XML to a string using XML.stringify(inputXMLRequest) and use string manipulation

    2) Use the transform.xslt async function to execute an xsl stylesheet that would update the XML and return the result.

    I'd suggest you open a request for enhancement for the XML DOM create, update, and delete functions within GatewayScript.

    Best Regards,
    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------



  • 3.  RE: Update XML Field value using XSLT

    IBM Champion
    Posted Mon December 20, 2021 03:07 AM
    Hi Adarsh,

    in case you decide to switch to XSLT, here is a simple example how to change Name - element value:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    	<xsl:output method="xml" encoding="UTF-8"/>
    
    	<xsl:template match="/">	
    		<xsl:apply-templates/>
    	</xsl:template>
    	
    	<xsl:template match="//*[local-name()='Name']/text()">	
    		<xsl:value-of select="'John'"/>
    	</xsl:template>
    	
    	<xsl:template match="@* | node()">
                    <xsl:copy>
                         <xsl:apply-templates select="@* | node()"/>
                    </xsl:copy>
            </xsl:template>
      
    </xsl:stylesheet>​


    ------------------------------
    Hermanni Pernaa
    ------------------------------



  • 4.  RE: Update XML Field value using XSLT

    Posted Mon December 20, 2021 02:23 PM

    Steve Edwards steve@steve-edwards.org

    15:30 (5 minutes ago)
    to IBMCOMMUNITY_datapowergeneral_5e3e698d-fec9-4d0b-bb9a-cb8eb1c77deb
    Hi,
    I've done a lot of XSLT over 20 years, on DataPower and otherwise.

    As an example, the following will change the value of <Age></Age>

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:variable name="new-value" select="5"/>

    <xsl:template match="/">
    <xsl:apply-templates select="*"/>
    </xsl:template>

    <xsl:template match="Age">
    <Age><xsl:value-of select="$new-value"/></Age>
    </xsl:template>

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

    </xsl:stylesheet>

    where the value of "new-value" can be determined at run-time through various means,
    e.g. from somewhere else in the DataPower stream / message / some calculation.

    Enjoy,
    Steve Edwards




    ------------------------------
    Steve Edwards
    Director
    Escala Ltd
    Southampton
    02380783802
    ------------------------------