首页 > 解决方案 > XSLT 3:将 xml 转换为 json

问题描述

当我尝试使用 XSLT3 将 XML 转换为 JSON 时 <xsl:copy-of select="xml-to-json($finalOutPut, map { 'indent' : false() })"/>

我收到以下错误:net.sf.saxon.s9api.SaxonApiException: xml-to-json: element found in wrong namespace: Q{}wrapper

基本上我正在将一个 xml 转换为另一个 xml,重命名某些字段。将此 xml 作为输入传递给 xml-to-json() 。

有什么建议么?

标签: xslt-3.0

解决方案


使用的 XML 格式xml-to-json在 XSLT 3.0 规范 ( https://www.w3.org/TR/xslt-30/#json-to-xml-mapping ) 以及 XPath 和 XQuery 3.1 函数规范中都指定了:https ://www.w3.org/TR/xpath-functions/#json 。

基本上所有元素都需要在命名空间中,http://www.w3.org/2005/xpath-functions并且是maparraystringbooleannumber,以反映 JSON 数据类型。

错误消息表明您的输入包含一个wrapper在 no namespace 中命名的元素,因此这肯定不是该函数的正确格式。您将需要使用额外的转换步骤将您的 XML 转换为函数期望的格式。


推荐阅读