Search code examples
xmlxsltwso2wso2-integration-studio

Convert Number to String in XSLT in WSO2


I have an xml payload that looks like this:

<Product>
<ProductId>790982</ReceiptId>
<Qty>78</Qty>
<Product>
<Product>
<ProductId>WS-5678</ReceiptId>
<Qty>34</Qty>
<Product>

I need to transform using xslt and send it to another system. I am using xsl:value-of to extract the values, but after conversion the first ProductId comes out as a number and the second ProductId comes out as a String.

 <itemRef> <xsl:value-of select="/ProductId"/>   </itemRef>

I want to convert every ProductId to a string. I've seen a fix where we need to add conf in synapse-properties file. But that would convert every Number to String. Is there any way I can do it only for ProductId by checking if its a number or a string? I've tried concat function as below to add double quotes across the number but that doesn't work.

 <xsl:variable name="quot">"</xsl:variable>
 <xsl:variable name="sku" select="/ProductId"/>
<itemRef>   <xsl:value-of select="concat($quot, $sku,$quot)"/>  </itemRef>
                        

Solution

  • You can use a custom JSON Schema along with JSON Transform Mediator for this.

    Assuming you want to do the following conversion.

    <products>
        <Product>
            <ProductId>790982</ProductId>
            <Qty>78</Qty>
        </Product>
        <Product>
            <ProductId>WS-5678</ProductId>
            <Qty>34</Qty>
        </Product>
    </products>
    

    To

    {
        "products": {
            "Product": [
                {
                    "ProductId": "790982",
                    "Qty": 78
                },
                {
                    "ProductId": "WS-5678",
                    "Qty": 34
                }
            ]
        }
    }
    

    First Create a Local Entry with the JSON Schema like below.

    <?xml version="1.0" encoding="UTF-8"?>
    <localEntry key="JsonSchema" xmlns="http://ws.apache.org/ns/synapse"><![CDATA[
    {
        "$schema": "http://json-schema.org/draft-07/schema#", 
        "type": "object",
        "required": [
            "products"
        ],
        "properties": {
            "products": {
                "type": "object",
                "required": [
                    "Product"
                ],
                "properties": {
                    "Product": {
                        "type": "array",
                        "items":{
                            "type": "object",
                            "properties": {
                                "ProductId": {
                                    "type": "string"
                                },
                                "Qty": {
                                    "type": "integer"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    ]]></localEntry>
    

    Then after the XSLT mediator add the following pointing to the above schema.

    <property name="messageType" scope="axis2" type="STRING" value="application/json"/>
    <jsontransform schema="JsonSchema"/>