Reputation: 9279
I am reading the W3C documentation for XSLT 3.0 here. Here is what I have got:
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<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>
</xsl:stylesheet>
After we have created a map, how do we use and retrieve its values? Were there different ways to create a map in earlier versions of XSLT?
Upvotes: 2
Views: 6510
Reputation: 163635
To add to the answer from @Cajetan_Rodrigues, the nearest equivalent in XSLT 2.0 was probably to create a temporary tree like this:
<xsl:variable name="week" as="map(xs:string, xs:string)">
<map>
<entry key="Mo" value="Monday"/>
<entry key="Tu" value="Tuesday"/>
<entry key="We" value="Wednesday"/>
<entry key="Th" value="Thursday"/>
<entry key="Fr" value="Friday"/>
<entry key="Sa" value="Saturday"/>
<entry key="Su" value="Sunday"/>
</map>
</xsl:variable>
The benefits of a map over a temporary XML tree are:
The entries can be any value, not only XML elements and attributes. For example, an entry might be a sequence of integers, or a reference to an external XML element
Modifying a map by adding or deleting entries can be a lot more efficient than modifying a temporary XML tree, because maps do not need to support concepts such as node identity, document order, namespaces, preceding/following/parent navigation, and so on.
Upvotes: 3
Reputation: 172
The link you mentioned also has good examples. You can refer those for usage and retrieval of values.
As far as earlier versions of XSLT are concerned, there wasn't any structure similar in functionality to the map
. If you needed values to be retrieved later, the best you could do is store them in different variables. That is precisely the reason why the map structure was introduced:
Maps have many uses, but their introduction to XSLT 3.0 was strongly motivated by streaming use cases. In essence, when a source document is processed in streaming mode, data that is encountered in the course of processing may need to be retained in variables for subsequent use, because the nodes cannot be revisited. This creates a need for a flexible data structure to accommodate such temporary data, and maps were designed to fulfil this need.
Upvotes: 2