1

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>
                        
Ruby
  • 368
  • 1
  • 9
  • Are you converting this to a JSON? Can you add the converted payload. – ycr Mar 07 '23 at 12:05
  • The JSON payload should look like this: "itemRef": "790982","qty":78. with the existing xslt it goes like this: "itemRef": 790982,"qty":78. – Ruby Mar 07 '23 at 12:08
  • Please read https://stackoverflow.com/help/minimal-reproducible-example – Conal Tuohy Mar 07 '23 at 12:34

1 Answers1

1

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"/>
ycr
  • 12,828
  • 2
  • 25
  • 45
  • Shouldn't the schema be added as a registry resource rather than a localentry? – Ruby Mar 07 '23 at 16:45
  • 1
    @Ruby If you want, you can add it as a registry resource.... If you don't have a registry project you can simply add it as a localentry. It doesn't really matter. – ycr Mar 07 '23 at 17:23