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

select?

expression

New in XSLT 4.0. Provides an alternative way of supplying the input sequence for constructing the map; if present, the contained sequence constructor must be empty.

duplicates?

expression

New in XSLT 4.0. An expression which must evaluate to either one of the strings "use-first", "use-last", "use-any", "combine", or "reject", or 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 fn($old, $new){$old, $new} then the new value will be the sequence-concatenation of the duplicate entries.

Saxon availability

Available in XSLT 3.0 and later versions. From Saxon 9.8, available in all editions. Implemented in Saxon-PE and Saxon-EE since Saxon 9.6. Available for all platforms.

Notes on the Saxon implementation

The new attributes duplicates and select are available from Saxon 13 in XSLT 4.0. The 4.0 duplicates attribute replaces on-duplicates which was defined in earlier drafts, and which was based on the Saxon extension attribute saxon:on-duplicates, which were previously available in Saxon 10 to 12, and are now dropped.

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 duplicates="fn($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

XSLT 4.0 Specification

See also

xsl:map-entry

xsl:record