xslt 3.0 json-to-xml and xml-to-json conversion

自古美人都是妖i 提交于 2019-11-30 16:25:49

You need to match on /, as in

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:output method="text"/>

  <xsl:template match="/">
      <xsl:value-of select="xml-to-json(.)"/>
  </xsl:template>

</xsl:stylesheet>

then the result is

{"cars":[{"doors":"4","price":"6L"},{"doors":"5","price":"13L"}]}

With

  <xsl:template match="/">
      <xsl:value-of select="xml-to-json(., map { 'indent' : true() })"/>
  </xsl:template>

you get indentation although Saxon is not doing a nice job there:

  { "cars" : 
    [ 
      { "doors" : "4",
        "price" : "6L" },

      { "doors" : "5",
        "price" : "13L" } ] }

https://xsltfiddle.liberty-development.net/b4GWVd/1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!