[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

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


Subject: [xsl] Challenge: do better than my implementation of "update map" and "print map"
From: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Wed, 28 Aug 2013 21:52:27 +0000

Hi Folks,

Below is a stylesheet that implements two functions on maps:

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

2. Print the contents of a map

I'm mighty proud of these two functions.

But I'm willing to believe that there exists an even better implementation.

Challenge: can you create a better implementation of these two functions?

/Roger
-----------------------------------------------------------------------------
----
<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">

    <!-- Test the two map functions -->

    <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)" />

    </xsl:template>

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

        <xsl:sequence select="map:new((for $i in map:keys($m) return
map:entry($i, map:get($m, $i)), map:entry($key, $value)))" />

    </xsl:function>

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

        <xsl:for-each select="map:keys($m)">
            <xsl:value-of select="." />
            <xsl:text> - </xsl:text>
            <xsl:value-of select="map:get($m, .)" />
        </xsl:for-each>
    </xsl:function>

</xsl:stylesheet>


Current Thread