在使用XSLT进行转换后,我正在尝试将方括号包含在Json中。我希望Json以列表/数组的形式存在。
下面是我的XML文件。
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="foedselsdato">2019-04-22</string>
<string key="individId">01387</string>
<map key="varslinger"/>
</map>下面是我的XSL文件。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:value-of select="xml-to-json(., map { 'indent' : true() })"/>
</xsl:template>
</xsl:stylesheet>使用https://xsltfiddle.liberty-development.net/b4GWVd进行转换,我得到:
{ "foedselsdato" : "2019-04-22",
"individId" : "01387",
"varslinger" :
{ } }但我想将其转换为以下内容:
[{ "foedselsdato" : "2019-04-22",
"individId" : "01387",
"varslinger" :
{ } }]发布于 2020-09-18 18:43:29
如果想在XSLT3.1和XSLT3中使用XPath的XDM表示,可以使用
<xsl:output method="json" indent="yes"/>
<xsl:template match="/">
<xsl:sequence select="array { xml-to-json(.) => parse-json() }"/>
</xsl:template>要将前面的JSON包装到一个数组中:https://xsltfiddle.liberty-development.net/b4GWVd/81
发布于 2020-09-18 17:07:54
下面的代码起作用了。
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:value-of select="xml-to-json(., map { 'indent' : true() })"/>
<xsl:text>]</xsl:text>
</xsl:template>
</xsl:stylesheet>或
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:value-of select="concat('[',xml-to-json(., map { 'indent' : true() }))"/>
<xsl:text>]</xsl:text>
</xsl:template>
</xsl:stylesheet>https://stackoverflow.com/questions/63952199
复制相似问题