Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Created December 22, 2015 12:21
Show Gist options
  • Save ScalaWilliam/3d1d4279d9fe4803d9a5 to your computer and use it in GitHub Desktop.
Save ScalaWilliam/3d1d4279d9fe4803d9a5 to your computer and use it in GitHub Desktop.
json transform with xquery 3.1 (saxon 9.7) and xslt. Only doing it in xquery because want it self contained
{"stuff":{"more":["more stuff!",{"thing":"more stuff!"}]}}
xquery version "3.1";
declare namespace xsl = "http://www.w3.org/1999/XSL/Transform";
let $transformer := <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">
<xsl:template match="text()[. = 'stuff']">more stuff!</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
let $json := '{"stuff": {"more": ["stuff", {"thing": "stuff"}]}}'
(: http://www.w3.org/TR/xpath-functions-31/#func-transform
http://www.saxonica.com/html/documentation/functions/fn/transform.html
:)
return xml-to-json(transform(
map {
"stylesheet-node": $transformer,
"source-node": fn:json-to-xml($json)
}
)?output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment