当我在 java 中使用 XSLT 从 XML 转换为 JSON 时,会发生以下错误:
fn:xml-to-json() 的第一个参数的必需项类型是 node();提供的值具有项目类型 xs:string
XML:
<?xml version="1.0" encoding="UTF-8"?>
<map xmlns="http://www.w3.org/2005/xpath-functions">
<string key="student">john</string>
<string key="class">Bachelors</string>
<string key="subjects">
<subject>
<subjects>maths</subjects>
</subject>
</string>
</map>
XSLT(Xml 到 Json):
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="xmlText"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template name="init">
<xsl:apply-templates select="xml-to-json($xmlText)"/>
</xsl:template>
</xsl:stylesheet>
错误 :
Type error at char 12 in xsl:copy-of/@select on line 30 column 50 of json2xml.xsl:
XPTY0004: Required item type of first argument of fn:xml-to-json() is node();
supplied value has item type xs:string Exception in thread "main" net.sf.saxon.s9api.SaxonApiException:
Required item type of first argument of fn:xml-to-json() is node();
supplied value has item type xs:string at net.sf.saxon.s9api.XsltTransformer.transform(XsltTransformer.java:599)
at com.xmltojson.sampleclass.SimpleJaxp.main(SimpleJaxp.java:44)Caused by: net.sf.saxon.trans.XPathException:
Required item type of first argument of fn:xml-to-json() is node();
supplied value has item type xs:string