RE: [xsl] Challenge: do better than my implementation of "update map" and "print map"

Subject: RE: [xsl] Challenge: do better than my implementation of "update map" and "print map"
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Thu, 29 Aug 2013 09:16:56 +0000
Wow!

Thank you Sean, Dimitre, and Wolfgang.

You are awesome.

Okay, we now have two very fine functions on maps:

1. Update an existing map with another key/value pair

2. Print the contents of a map

Below is a complete, working stylesheet which tests the two map functions.

/Roger
------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:map="http://www.w3.org/2005/xpath-functions/map";
                xmlns:xs="http://www.w3.org/2001/XMLSchema";
                xmlns:f="function"
                version="3.0">

    <xsl:template match="/">
        <xsl:variable name="m" select="map{'Linda' := 'Rosie'}" />

        <xsl:variable name="m1" select="f:add-entry-to-map('Sally', 'Betsy',
$m)" />
        <xsl:variable name="m2" select="f:add-entry-to-map('Barb', 'Sue',
$m1)" />
        <xsl:variable name="m3" select="f:add-entry-to-map('Nadia', 'Valerie',
$m2)" />
        <xsl:variable name="m4" select="f:add-entry-to-map('Faye', 'Carol',
$m3)" />

        <xsl:sequence select="f:print-map($m4)" />

        <!-- The output is:
               Linda - Rosie
               Sally - Betsy
               Barb - Sue
               Nadia - Valerie
               Faye - Carol
        -->

    </xsl:template>

    <xsl:function name="f:add-entry-to-map" as="map(xs:anyAtomicType,
item()*)">
        <xsl:param name="key" as="xs:anyAtomicType" />
        <xsl:param name="value" as="item()*" />
        <xsl:param name="m" as="map(xs:anyAtomicType, item()*)" />

        <xsl:sequence select="map:new(($m, map:entry($key, $value)),
map:collation($m) )" />

    </xsl:function>

    <xsl:function name="f:print-map" as="xs:string*">
        <xsl:param name="m" as="map(xs:anyAtomicType, item()*)" />

        <xsl:sequence select="map:keys($m) ! (., ' - ', map:get($m, .))"/>

    </xsl:function>

</xsl:stylesheet>

Current Thread