I want to use new feature in XPath3.1 like array
and map
, This might sound like a googleable question, but I try many example code still receive error message, here's how I got so far:
<!-- XSLT.xslt -->
<!-- using SaxonHE9-8-0-7 -->
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<xsl:copy-of select="system-property('xsl:version ')"/> <!-- show 3.0 -->
<xsl:copy-of select="system-property('xsl:vendor ')"/> <!-- show Saxonica -->
<xsl:copy-of select="system-property('xsl:xpath-version')"/> <!-- show 3.1 -->
<xsl:copy-of select="system-property('xsl:xsd-version ')"/> <!-- show 1.1 -->
</xsl:template>
</xsl:stylesheet>
So it's there a simple and working code that can demonstrate the power of array
and map
? thanks!
Maps can help primarily with two issues:
As for simple examples, the various sections in the specs on the map and array syntax and the functions have simple examples, to use the functions with XSLT you only have to make sure you declare the namespaces the functions are defined in with e.g.
I am not sure simple examples can demonstrate the power of a new language feature and maps and arrays have various features, to give you one example that transforms some XML input to JSON by creating maps (in one template using the XSLT
xsl:map
and in another the XPath map constructor syntax) and arrays and serializing the result asjson
:So that transforms
to the JSON
Online at http://xsltfiddle.liberty-development.net/b4GWV3.
I have also put together another sample at http://xsltfiddle.liberty-development.net/6qM2e27 that shows some ways to construct maps:
Output of this is (unfortunately there is currently no way in Saxon to pretty print/indent items with output method 'adaptive'):
And a similar example showing some basic variants to construct arrays is
which outputs
http://xsltfiddle.liberty-development.net/eiQZDbd
At this stage of the publication, it might also be worth looking at the various test cases in the XSLT 3 and the combined XPath and XQuery test suites, for instance:
xsl:map
with streamingxml-to-json
json-to-xml
test cases.