xsl-list
[Top] [All Lists]

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

2013-08-28 22:40:24
Here is another thought about f:add-entry-to-map(), Roger.

What about preserving collation? map:new() with an argument of a sequence of maps returns a map with the default collation from the static context, but what you probably want is the collation from $m.

How about this instead ...

<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( ($m, map:entry($key, $value)), map:collation($m) )" />
</xsl:function>


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

Faithfully,
Sean B. Durkin

On 2013-08-28 20:45, sean(_at_)seanbdurkin(_dot_)id(_dot_)au wrote:
Hi Roger,

I think this would work better ...

<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( ($m), map:entry($key, $value)))" />
</xsl:function>

.. as the map:new() function works on a sequence of maps. Refer to
the example in the spec, entitled "Example: Using a Map as an Index".


Faithfully,
Sean B. Durkin

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?
/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>
--~------------------------------------------------------------------
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>
--~--

--~------------------------------------------------------------------
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>
--~--

--~------------------------------------------------------------------
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>
--~--