英文:
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"/>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论