XSLT 3.0 - 无法在 XSLT 3.0 xml-to-json() 中获取对象数组
XSLT 3.0 - Not able to get Array of objects in XSLT 3.0 xml-to-json()
我正在尝试使用 XSLT 3.0 将给定的 json 数据从一种形式转换为另一种形式。我正在使用 XSLT 3.0 提供的 json-to-xml 和 xml-to-json 函数来转换数据 from.to json to/from xml .
我有以下 json 数据。
{
"id": "123456",
"result": "Success"
}
我正在尝试使用 XSLT 3.0 将其转换为以下形式
[
{
"key":"id",
"value":"123456"
},
{
"key":"result",
"value":"Success"
}
]
我有以下 XSLT。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0"
xmlns="http://www.w3.org/2005/xpath-functions"
xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
expand-text="yes">
<xsl:param name="json"/>
<xsl:output method="text"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="/" name="init">
<xsl:variable name="json-xml" select="json-to-xml($json)"/>
<xsl:variable name="transformed-json-xml">
<map>
<xsl:apply-templates select="$json-xml//map"/>
</map>
</xsl:variable>
<xsl:value-of select="xml-to-json($transformed-json-xml, map { 'indent' : true() })"/>
</xsl:template>
<xsl:template match="map[string[@key = 'id'] and string[@key = 'result']]">
<string key="key">id</string>
<string key="value">{string[@key = 'id']}</string>
</xsl:template>
</xsl:stylesheet>
但它只生成一个对象
{ "key" : "id",
"value" : "123456" }
任何人都可以指出我需要进行更改的地方吗?
好吧,如果你想要一个数组作为外部 JSON 结构你需要改变
<xsl:variable name="transformed-json-xml">
<map>
<xsl:apply-templates select="$json-xml//map"/>
</map>
到
<xsl:variable name="transformed-json-xml">
<array>
<xsl:apply-templates select="$json-xml//map"/>
</array>
你想要的地图
<xsl:template match="map[string[@key = 'id'] and string[@key = 'result']]">
<xsl:apply-templates/>
</xsl:template>
加上
<xsl:template match="string">
<map>
<string key="key">{@key}</string>
<string key="value">{.}</string>
</map>
</xsl:template>
我正在尝试使用 XSLT 3.0 将给定的 json 数据从一种形式转换为另一种形式。我正在使用 XSLT 3.0 提供的 json-to-xml 和 xml-to-json 函数来转换数据 from.to json to/from xml .
我有以下 json 数据。
{
"id": "123456",
"result": "Success"
}
我正在尝试使用 XSLT 3.0 将其转换为以下形式
[
{
"key":"id",
"value":"123456"
},
{
"key":"result",
"value":"Success"
}
]
我有以下 XSLT。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="3.0"
xmlns="http://www.w3.org/2005/xpath-functions"
xpath-default-namespace="http://www.w3.org/2005/xpath-functions"
expand-text="yes">
<xsl:param name="json"/>
<xsl:output method="text"/>
<xsl:mode on-no-match="shallow-skip"/>
<xsl:template match="/" name="init">
<xsl:variable name="json-xml" select="json-to-xml($json)"/>
<xsl:variable name="transformed-json-xml">
<map>
<xsl:apply-templates select="$json-xml//map"/>
</map>
</xsl:variable>
<xsl:value-of select="xml-to-json($transformed-json-xml, map { 'indent' : true() })"/>
</xsl:template>
<xsl:template match="map[string[@key = 'id'] and string[@key = 'result']]">
<string key="key">id</string>
<string key="value">{string[@key = 'id']}</string>
</xsl:template>
</xsl:stylesheet>
但它只生成一个对象
{ "key" : "id",
"value" : "123456" }
任何人都可以指出我需要进行更改的地方吗?
好吧,如果你想要一个数组作为外部 JSON 结构你需要改变
<xsl:variable name="transformed-json-xml">
<map>
<xsl:apply-templates select="$json-xml//map"/>
</map>
到
<xsl:variable name="transformed-json-xml">
<array>
<xsl:apply-templates select="$json-xml//map"/>
</array>
你想要的地图
<xsl:template match="map[string[@key = 'id'] and string[@key = 'result']]">
<xsl:apply-templates/>
</xsl:template>
加上
<xsl:template match="string">
<map>
<string key="key">{@key}</string>
<string key="value">{.}</string>
</map>
</xsl:template>