json - 如何在 XSLT 中将 XML 转换为 Json?
问题描述
如何在xslt中将xml转换为json
输入xml:
<root>
<citiID>RR1</citiID>
<bib>ertyokf (5208). Free <Emphasis Type="Italic">of detachment</Emphasis>, aedrtg. dcdcdr<b>49</b> any text</bib>
</root>
预期的Json:
"root": [
{
"citeid": "RR1",
"bib": "ertyokf (5208). Free <Emphasis Type=\"Italic\">of detachment</Emphasis>, aedrtg. dcdcdr<b>49</b> any text."
},
]
解决方案
您可以使用两种方法,一种是将所需的 JSON 直接表示为 XDM 3.1 映射和数组:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="json" indent="yes"/>
<xsl:template match="/*">
<xsl:sequence
select="map {
local-name() : array {
map:merge(* ! map {
lower-case(local-name()) : serialize(node())
})
}
}"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/naZYrpR/1
第二个是将输入的 XML 转换为xml-to-json
函数使用的 JSON 的 XML 表示。
推荐阅读
- r - 匹配和替换部分字符串的优雅方式
- javascript - d3.js - 检测具有相同中心的圆圈
- vba - Excel VBA:使用 .Find 识别单元格内容并将行复制到新选项卡(多个搜索词)
- mysql - 计算百分比两个值和求和算术运算mysql
- swift - 无法在 RxSwift 中使用通用结果枚举错误
- reactjs - React Native - TypeError:this.addItem.bind
- php - 如何制作具有多个选项卡的过滤器并在laravel中动态获取数据
- android - 在 AsyncTask 中使用 onPostExecute 方法列表项单击事件调用 Android 中的另一个活动
- c++ - 如何在 C++ 中按值对地图进行排序,但如果键值相等?
- javascript - jQuery - onClick 在新页面中打开随机链接