【问题标题】:How to use a map in XSLT?如何在 XSLT 中使用地图?
【发布时间】:2014-11-08 00:55:18
【问题描述】:

我正在阅读 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 中是否有不同的方法来创建地图?

【问题讨论】:

    标签: xml xslt xslt-3.0


    【解决方案1】:

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

    【讨论】:

    • 那么我们是否像这样检索它的值呢? $week('Mo')
    【解决方案2】:

    你提到的链接也有很好的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.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-10
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-03
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多