当我使用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(XSLT) :
<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发布于 2017-07-27 09:52:16
我否决了这个问题,因为您显然没有对它给予足够的注意:错误消息指的是您展示给我们的代码中没有的xsl:copy-of指令;您也没有向我们展示样式表是如何被调用的,以及如何提供$xmlText的值。
但是(合并评论中已经建议的内容),您需要:
(a)确保xml()的参数是一个节点。如果您是从URI开始的,请调用doc()或document()函数来获取节点。如果您从包含词法XML的字符串开始,请调用parse()函数。
(b)确保所传递的节点对于规范中定义的模式(对于JSON的XML表示)是有效的。例如,此架构不允许subject作为string的子模式。(我不确定您想在这里实现什么输出:如果您希望JSON输出以形式显示
"subjects": "<subject><subjects>maths</subjects></subject>"然后,您应该将输入更改为
<string key="subjects"><![CDATA[<subject><subjects>maths</subjects></subject>]]></string>(如果有新行出现,则会出现更复杂的情况,但这将是另一个问题)。
https://stackoverflow.com/questions/45330151
复制相似问题