xsl-list
[Top] [All Lists]

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

2013-08-29 04:17:17
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>

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--