1

我在这里阅读 XSLT 3.0 的 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 中是否有不同的方法来创建映射?

4

2 回答 2

3

为了补充@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 树更有效,因为地图不需要支持诸如节点标识、文档顺序、命名空间、前/后/父导航等概念。

于 2014-09-14T15:31:33.537 回答
2

你提到的链接也有很好的例子。您可以参考这些值的使用和检索。

就早期版本的 XSLT 而言,没有任何结构与map. 如果您需要稍后检索值,您可以做的最好的是将它们存储在不同的变量中。这正是引入地图结构的原因:

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.

于 2014-09-14T13:52:57.757 回答