【发布时间】:2016-08-09 07:25:24
【问题描述】:
我需要将 JSON 字符串转换为 XML 字符串。标签确实包含属性。从this 主题中的答案开始,我开始使用 XSLT。
存在一个函数fn:json-to-xml。我知道它应该将 JSON 转换为没有属性的 XML(我使用 XSLT 格式化)。
如何使用此功能?
因为它是在 XSLT 中实现的,所以我猜想在 .xsl 文件中,但我找不到任何示例。
提前非常感谢!
【问题讨论】:
我需要将 JSON 字符串转换为 XML 字符串。标签确实包含属性。从this 主题中的答案开始,我开始使用 XSLT。
存在一个函数fn:json-to-xml。我知道它应该将 JSON 转换为没有属性的 XML(我使用 XSLT 格式化)。
如何使用此功能?
因为它是在 XSLT 中实现的,所以我猜想在 .xsl 文件中,但我找不到任何示例。
提前非常感谢!
【问题讨论】:
该函数在 XSLT 3.0、XPath 3.1 和 XQuery 3.1 中定义。
使用它的最简单方法可能是安装 Saxon-HE 9.7.0.7,然后在命令行上从 XQuery 运行它,如下所示:
java -cp /dddd/9.7.0.7/he/saxon9he.jar net.sf.saxon.Query -t -qs:"json-to-xml(unparsed-text('/eeee/test.json'))" -o:output.xml
【讨论】:
json-to-xml($param) 并将 $param 的值(JSON 格式的字符串)作为参数传递给查询。
这是一个取自 XSLT 3.0 规范 https://www.w3.org/TR/xslt-30/#func-json-to-xml 的简单示例,示例输入可以是 JSON 格式的任何字符串,下面我使用包含 data 元素和 JSON 的 XML 文档:
<root>
<data>{
"desc" : "Distances between several cities, in kilometers.",
"updated" : "2014-02-04T18:50:45",
"uptodate": true,
"author" : null,
"cities" : {
"Brussels": [
{"to": "London", "distance": 322},
{"to": "Paris", "distance": 265},
{"to": "Amsterdam", "distance": 173}
],
"London": [
{"to": "Brussels", "distance": 322},
{"to": "Paris", "distance": 344},
{"to": "Amsterdam", "distance": 358}
],
"Paris": [
{"to": "Brussels", "distance": 265},
{"to": "London", "distance": 344},
{"to": "Amsterdam", "distance": 431}
],
"Amsterdam": [
{"to": "Brussels", "distance": 173},
{"to": "London", "distance": 358},
{"to": "Paris", "distance": 431}
]
}
}</data>
</root>
data 元素的模板然后简单地调用json-to-xml(.) 和整个样式表来演示结果输出返回的 XML:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:copy-of select="json-to-xml(.)"/>
</xsl:template>
</xsl:stylesheet>
Saxon 9.7 HE 的输出是
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="desc">Distances between several cities, in kilometers.</string>
<string key="updated">2014-02-04T18:50:45</string>
<boolean key="uptodate">true</boolean>
<null key="author"/>
<map key="cities">
<array key="Brussels">
<map>
<string key="to">London</string>
<number key="distance">322</number>
</map>
<map>
<string key="to">Paris</string>
<number key="distance">265</number>
</map>
<map>
<string key="to">Amsterdam</string>
<number key="distance">173</number>
</map>
</array>
<array key="London">
<map>
<string key="to">Brussels</string>
<number key="distance">322</number>
</map>
<map>
<string key="to">Paris</string>
<number key="distance">344</number>
</map>
<map>
<string key="to">Amsterdam</string>
<number key="distance">358</number>
</map>
</array>
<array key="Paris">
<map>
<string key="to">Brussels</string>
<number key="distance">265</number>
</map>
<map>
<string key="to">London</string>
<number key="distance">344</number>
</map>
<map>
<string key="to">Amsterdam</string>
<number key="distance">431</number>
</map>
</array>
<array key="Amsterdam">
<map>
<string key="to">Brussels</string>
<number key="distance">173</number>
</map>
<map>
<string key="to">London</string>
<number key="distance">358</number>
</map>
<map>
<string key="to">Paris</string>
<number key="distance">431</number>
</map>
</array>
</map>
</map>
因此,与将输入作为as xs:string 的任何其他函数一样,您当然可以传入您在 XSLT 或 XPath 代码中拥有的任何字符串值,或者您可以传入一个节点,然后首先将其原子化为字符串。
【讨论】:
unparsed-text('file.json') 的 JSON 文件。或者通过将带有包含 JSON 的字符串的参数传递给 XSLT。该函数与编程语言中的大多数函数一样,不受特定上下文的限制。