Using fn:xml-to-json withing a stream giving error
Added by Mike Banks almost 9 years ago
Using saxonEE9-7-0-2, within a stream I get the error
FOJS0006: xml-to-json: element found in wrong namespace
when trying to use the function xml-to-json.
Here some some sample code. <xsl:stream href="test.xml"> <xsl:for-each select="/UnitList/Unit"> <xsl:variable name="vU" select="fn:copy-of(.)"/> <xsl:variable name="vXJ" select="fn:xml-to-json($vU)"/> <xsl:value-of select="$vXJ"/> </xsl:for-each> </xsl:stream> What I am doing wrong?
Replies (7)
Please register to reply
RE: Using fn:xml-to-json withing a stream giving error - Added by Michael Kay almost 9 years ago
I suspect that you have misunderstood the spec of fn:xml-to-json. It's not designed to convert arbitrary XML to JSON, only the specific XML vocabulary defined for the purpose. First transform your XML into this vocabulary, then do the JSON conversion.
RE: Using fn:xml-to-json withing a stream giving error - Added by Mike Banks almost 9 years ago
I see what I was assuming was incorrect now. I have it working now but one more question, how do I actually set the indent to true?
RE: Using fn:xml-to-json withing a stream giving error - Added by Michael Kay almost 9 years ago
It should be xml-to-json($xml, map{ "indent":true() })
RE: Using fn:xml-to-json withing a stream giving error - Added by Mike Banks almost 9 years ago
Thanks, great support. If only all companies were as good. Would be nice if more examples on usage were online, or are you saving them for your next book? :-)
RE: Using fn:xml-to-json withing a stream giving error - Added by Michael Kay almost 9 years ago
As it happens I'm working on examples of JSON processing in XSLT right now, for my conference talk at XML Prague on Friday. They will appear in the published paper which we'll put on the web site.
You're at the bleeding edge in terms of using new functionality and it takes a while for tutorial material to build up on the web.
RE: Using fn:xml-to-json withing a stream giving error - Added by Mike Banks almost 9 years ago
I just have a comment on the indent. The result looks like this below. The closing brackets are all in a line. Is this intentional? { "products" : [ { "id" : "Product1", "numRates" : "45", "locales" : [ { "loc" : "en_AT", "AveRate" : "3.6" },
{ "loc" : "de_AT",
"AveRate" : "3.3333" },
{ "loc" : "en_US",
"AveRate" : "3.5333" } ] } ] }
RE: Using fn:xml-to-json withing a stream giving error - Added by Michael Kay almost 9 years ago
I tried a few things quickly and this was the one that worked best for me, but I make no claim that it's perfect. I'm sure that the algorithm will improve over time, just as the XML and HTML indentation did.
Please register to reply