xslt 3.0 json到xml和xml到json的转换
目前,我需要使用xslt3.0和Saxon-HE将json转换为xml,并将xml转换为json 下面是我的json abc.xml文件xslt 3.0 json到xml和xml到json的转换,json,xml,xslt,saxon,xslt-3.0,Json,Xml,Xslt,Saxon,Xslt 3.0,目前,我需要使用xslt3.0和Saxon-HE将json转换为xml,并将xml转换为json 下面是我的json abc.xml文件 <?xml version="1.0" encoding="UTF-8" ?> <root> <data>{ "cars" : [ {"doors" : "4","price" : "6L"}, {"doors" : "5","price" : "13L"}
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<data>{
"cars" : [
{"doors" : "4","price" : "6L"},
{"doors" : "5","price" : "13L"}
]
}
</data>
</root>
{
“汽车”:[
{“门”:“4”,“价格”:“6L”},
{“门”:“5”,“价格”:“13L”}
]
}
下面是xsl文件xyz.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:output indent="yes"/>
<xsl:template match="data">
<xsl:copy-of select="json-to-xml(.)"/>
</xsl:template>
下面是输出xml
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<array key="cars">
<map>
<string key="doors">4</string>
<string key="price">6L</string>
</map>
<map>
<string key="doors">5</string>
<string key="price">13L</string>
</map>
</array>
</map>
在这里试试这个例子
在xsl中,我选择了名为data的错误模板。因为数据模板不在output.xml中。我不确定我应该在这里写什么
<xsl:template match="data">
您需要在/
上进行匹配,如中所示
<?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>
与
请给我们展示一个最小但完整的XSLT示例,当我尝试使用XML时,它基本上是
,输出是{“cars”:[{“doors”:“4”,“price”:“6L”},{“doors”:“5”,“price”:“13L”}}
。如果您在不调用XML-to-json()的情况下输出XML,您的输出看起来就像预期的一样。您做错了什么,但是如果没有看到调用xml-to-json()的代码,我们就看不到什么。
<?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>
{"cars":[{"doors":"4","price":"6L"},{"doors":"5","price":"13L"}]}
<xsl:template match="/">
<xsl:value-of select="xml-to-json(., map { 'indent' : true() })"/>
</xsl:template>
{ "cars" :
[
{ "doors" : "4",
"price" : "6L" },
{ "doors" : "5",
"price" : "13L" } ] }