使用 xslt 1.0 将 xml(带有 json)转换为 json 响应

问题描述 投票:0回答:1

我有一个请求 xml,其中有一个 json -> 我想使用 xslt 将其映射到另一个 json。我正在使用 xslt 1.0。有没有办法在 xslt 1.0 中实现这一点: 请求xml是:

<root> { id: 5656, country-name: 'country-text' } <root>

我需要映射到的响应:

{ mid: 5656, cid : 'country-text' }

请帮忙

我尝试了一些方法,能够将源 json 复制到最终响应中 - 但这没有帮助,因为我需要读取各个字段并将它们映射到不同的键

用我尝试过的方法 -> 我能够得到

{id:5656, country-name:'country-text' }

我想要的是:

{ mid: 5656, cid : 'country-text' }

xml xslt xslt-1.0
1个回答
0
投票

我想这是你看待它的一种方式:

XSLT 1.0

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

<xsl:template match="/root">
    <xsl:text>{ mid: </xsl:text>
    <xsl:value-of select="substring-before(substring-after(., 'id: '), ',')" />
    <xsl:text>, cid : </xsl:text>
    <xsl:value-of select="substring-before(substring-after(., 'country-name: '), ' }')" />
    <xsl:text> }</xsl:text>
</xsl:template>

</xsl:stylesheet>
© www.soinside.com 2019 - 2024. All rights reserved.