我正在一个将JSON消息转换为XML的项目中,并且我有以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:exslt="http://exslt.org/common" extension-element-prefixes="exsl"
xmlns:exsl="http://exslt.org/common" xmlns:fcn="http://www.re.com/2018/local-functions"
exclude-result-prefixes="xs fn fcn exsl" version="3.0">
<xsl:param as="xs:string" name="json">[{
"results": [
{
"id": "750584f1-ce77-4e55-92b4-aae0c0ac37ae",
"profileId": "4aebb5de-5461-425c-b75e-959cef17048b",
"_embedded": {
"notes": {
"results": [
{
"content": "<div>General notes<div>"
},
{
"content": "General notes"
}
],
"paging": {
"pageNumber": 1,
"pageSize": 20,
"totalCount": 2
}
}
}
},
{
"id": "750584f1-ce77-4e55-92b4-aae0c0ac37ae",
"profileId": "4aebb5de-5461-425c-b75e-959cef17048b",
"_embedded": {
"notes": {
"results": [
{
"content": "General notes"
},
{
"content": "General notes"
}
],
"paging": {
"pageNumber": 1,
"pageSize": 20,
"totalCount": 2
}
}
}
]
}]
</xsl:param>
<xsl:variable name="input-as-xml" select="json-to-xml($json)"/>
<xsl:output indent="yes" method="text"/>
<xsl:template match="/" name="xsl:initial-template">
<xsl:variable name="transformed-xml">
<array xmlns="http://www.w3.org/2005/xpath-functions">
<xsl:for-each select="$input-as-xml/fn:array/fn:map/fn:array[@key = 'results']/*">
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="id">
<xsl:value-of select="*[@key='id']" />
</string>
</map>
</xsl:for-each>
</array>
</xsl:variable>
<xsl:value-of select="xml-to-json($transformed-xml, map {'indent': true()})"/>
</xsl:template>
</xsl:stylesheet>
当我尝试运行样式表时,我收到此错误消息:
Error executing XSLT at line 7 : A sequence of more than one item is not allowed as the value of variable $json ("[{ "results": [ {...", "General notes")
我猜我在匹配模板时犯了一些错误,但是我无法弄清楚我到底在做什么错。从我的测试看来,<div>
标签似乎也加剧了这个问题。一些帮助,将不胜感激。
答案 0 :(得分:1)
在这种情况下(嵌入式标记)是必要的,并且通常更安全的做法是将您拥有的JSON数据(如果您将其内联)放入CDATA部分:
LoadModule php7