[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: "Costello, Roger L." <costello@xxxxxxxxx>
Date: Thu, 29 Aug 2013 09:16:56 +0000


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.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

    <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:function name="f:add-entry-to-map" as="map(xs:anyAtomicType,
        <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 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, .))"/>



Current Thread