使用XSLT将XML转换为JSON,并将方括号[]添加到JSON

使用XSLT将XML转换为JSON,并将方括号[]添加到JSON,json,xml,xslt,xslt-3.0,Json,Xml,Xslt,Xslt 3.0,在使用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-2

在使用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>
但我想将其转换如下:

[{ "foedselsdato" : "2019-04-22",
    "individId" : "01387",
    "varslinger" : 
    {  } }]
下面的代码有效

<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="xml-to-json(., map { 'indent' : true() })"/>
      <xsl:text>]</xsl:text>
  </xsl:template>
</xsl:stylesheet>

[
]


[
]

如果希望使用XPath 3.1和XSLT 3中JSON的XDM表示,可以使用

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/">
      <xsl:sequence select="array { xml-to-json(.) => parse-json() }"/>
  </xsl:template>


要将前面的JSON封装到数组中:

如果您想使用XPath 3.1和XSLT 3中JSON的XDM表示,可以使用

  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/">
      <xsl:sequence select="array { xml-to-json(.) => parse-json() }"/>
  </xsl:template>


要将以前的JSON封装到一个数组中:

怎么样?我该怎么做,我得到了一个错误“fn:concat的第三个参数需要一个原子值(“当我使用表达式时,反过来做。将xml转换为JSON放入concat中。感谢@ceving,这也行得通。[]怎么样?我该怎么做,我得到了一个错误“fn:concat的第三个参数需要一个原子值(“当我使用expression时,反过来做。将xml转换为json放入concat中。谢谢@ceving,这也行。[]
  <xsl:output method="json" indent="yes"/>

  <xsl:template match="/">
      <xsl:sequence select="array { xml-to-json(.) => parse-json() }"/>
  </xsl:template>