xsl:map

Used to construct a new map.

Category: instruction
Content: sequence-constructor
Permitted parent elements: any XSLT element whose content model is sequence-constructor; any literal result element

Attributes

on-duplicates?

expression

Experimental XSLT 4.0 addition, available only if syntax extensions are enabled. An expression which must evaluate to an arity-2 function. If the attribute is present, then when a duplicate key value is encountered, the function is called supplying the old and new values for the key, and the old value for the key is replaced with the result of the function call. For example, if the function is ->($old, $new){$old, $new} then the new value will be the sequence-concatenation of the duplicate entries.

Details

The sequence constructor must evaluate to a sequence of maps. These can be constructed using xsl:map-entry elements.

Examples

Example 1

<xsl:variable name="week" as="map(xs:string, xs:string)"> <xsl:map> <xsl:map-entry key="'Mo'" select="'Monday'"/> <xsl:map-entry key="'Tu'" select="'Tuesday'"/> <xsl:map-entry key="'We'" select="'Wednesday'"/> <xsl:map-entry key="'Th'" select="'Thursday'"/> <xsl:map-entry key="'Fr'" select="'Friday'"/> <xsl:map-entry key="'Sa'" select="'Saturday'"/> <xsl:map-entry key="'Su'" select="'Sunday'"/> </xsl:map> </xsl:variable>

Example 2

This XSLT 4.0 example creates a map in which the value for a given product code is the total of the sales for that product across multiple outlets.

<xsl:variable name="total-sales-by-sku" as="map(xs:string, xs:decimal)"> <xsl:map on-duplicates="->($old, $new){$old + $new}"> <xsl:for-each select="//outlet/product"> <xsl:map-entry key="@sku" select="xs:decimal(@sales)"/> </xsl:for-each> </xsl:map> </xsl:variable>

Links to W3C specifications

XSLT 3.0 Specification

See also

xsl:map-entry