Convert Number to String in XSLT in WSO2

huangapple go评论69阅读模式
英文:

Convert Number to String in XSLT in WSO2

问题

我有一个看起来像这样的XML有效负载:

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

我需要使用XSLT进行转换并将其发送到另一个系统。我正在使用xsl:value-of来提取值,但在转换后,第一个ProductId以数字形式显示,而第二个ProductId以字符串形式显示。

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

我想将每个ProductId转换为字符串。我看到了一个修复方法,需要在synapse-properties文件中添加配置。但那样会将每个数字都转换为字符串。有没有办法只针对ProductId进行检查,然后将其转换为字符串?我尝试使用concat函数如下,以在数字周围添加双引号,但这并不起作用。

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

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>

答案1

得分: 1

您可以使用自定义的JSON模式与[JSON Transform Mediator来实现这一功能。

假设您想要进行以下转换。

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

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

首先创建一个包含以下JSON模式的本地入口。

<?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>

然后,在XSLT中介器之后添加以下指向上述模式的内容。

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

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"/>

huangapple
  • 本文由 发表于 2023年3月7日 17:05:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定