Am 11.03.2019 um 07:04 schrieb Mukul Gandhi
gandhi(_dot_)mukul(_at_)gmail(_dot_)com:
I'm continuing on this topic.
On Fri, Mar 8, 2019 at 4:39 PM Michael Kay mike(_at_)saxonica(_dot_)com
<mailto:mike(_at_)saxonica(_dot_)com> <xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com
<mailto:xsl-list-service(_at_)lists(_dot_)mulberrytech(_dot_)com>> wrote:
we decided on a different approach: provide a function to convert
a very specific XML vocabulary to JSON, and then (because we
already have powerful XML-to-XML transformation capabilities), let
the user say how their particular XML should be transformed to
that vocabulary.
Lets say, we've a following input XML document,
<?xml version="1.0" encoding="UTF-8"?>
<root>
<val>1</val>
<val>2</val>
<val>3</val>
<val>4</val>
</root>
I would want to transform above XML document to following JSON,
{
"root": {
"val": [
1,
2,
3,
4
]
}
}
How can I do this with XSLT 3.0 and XPath 3.1's fn:xml-to-json()
function ?
You need to transform your XML to the format the function expects, if
the input format is known, i.e. you know you have various "val" elements
with numbers you want to be transformed to array items then it is rather
easy:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
xmlns="http://www.w3.org/2005/xpath-functions"
expand-text="yes"
version="3.0">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="root">
<map>
<map key="{local-name()}">
<array key="val">
<xsl:apply-templates/>
</array>
</map>
</map>
</xsl:template>
<xsl:template match="val">
<number>{.}</number>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="json-xml">
<xsl:apply-templates/>
</xsl:variable>
<xsl:value-of select="xml-to-json($json-xml, map { 'indent' :
true() })"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/ncdD7mc
--~----------------------------------------------------------------
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
EasyUnsubscribe: http://lists.mulberrytech.com/unsub/xsl-list/1167547
or by email: xsl-list-unsub(_at_)lists(_dot_)mulberrytech(_dot_)com
--~--