我正在阅读XSLT 3.0 here的W3C文档。这是我得到的:
<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>
创建地图后,我们如何使用和检索其值?是否有不同的方法在早期版本的XSLT中创建地图?
答案 0 :(得分:2)
要添加@Cajetan_Rodrigues的答案,XSLT 2.0中最接近的等价物可能是这样创建一个临时树:
<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>
地图优于临时XML树的好处是:
条目可以是任何值,不仅仅是XML元素和属性。例如,条目可能是整数序列,或引用到外部XML元素
通过添加或删除条目来修改地图可能比修改临时XML树更有效,因为地图不需要支持节点标识,文档顺序,命名空间,前/后/父等概念导航,等等。
答案 1 :(得分:1)
您提到的链接也很好examples。 您可以参考这些值来使用和检索值。
就早期版本的XSLT而言,没有任何类似于map
的功能结构。如果您需要稍后检索的值,您可以做的最好的事情是将它们存储在不同的变量中。这正是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.