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

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: sean@xxxxxxxxxxxxxxxxx
Date: Wed, 28 Aug 2013 21:03:59 -0600

Hi Roger,

You may also want to sort the output of the print-map() function if it is intended for human reading.
XPath 2 could always do sorting, but it was inefficient (O3). XPath 3 can now implement QuickSort in 3 lines of code.


On 2013-08-28 15:52, Costello, Roger L. wrote:
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?

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<!-- 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: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 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, .)" />


Current Thread