XSLT 中的变量范围 [英] Variable scope in XSLT

查看:42
本文介绍了XSLT 中的变量范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试找出 xslt 上的 var 范围时遇到问题.我实际上想要做的是忽略具有重复旅游代码"的旅行"标签.

I am having an issue trying to figure out var scoping on xslt. What I actually want to do it to ignore 'trip' tags that have a repeated 'tourcode'.

示例 XML:

<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>X1</tourcode>
 <result>Budapest</result>
</trip>
<trip>
 <tourcode>Y1</tourcode>
 <result>london</result>
</trip>
<trip>
 <tourcode>Y1</tourcode>
 <result>london</result>
</trip>
<trip>
 <tourcode>Z1</tourcode>
 <result>Rome</result>
</trip>

XSLT 处理器:

<xsl:for-each select="trip">    
    <xsl:if test="not(tourcode = $temp)">
      <xsl:variable name="temp" select="tour"/>
      // Do Something (Print result!)
    </xsl:if>
</xsl:for-each>

预期输出:布达佩斯伦敦罗马

推荐答案

期望输出:布达佩斯伦敦罗马

您所追求的是按城市名称​​分组输出.在 XSLT 中有两种常用方法可以做到这一点.

What you are after is grouping output by city name. There are two common ways to do this in XSLT.

其中之一是这样的:

<xsl:template match="/allTrips">
  <xsl:apply-templates select="trip" />
</xsl:template>

<xsl:template match="trip">
  <!-- test if there is any preceding <trip> with the same <result> -->
  <xsl:if test="not(preceding-sibling::trip[result = current()/result])">
    <!-- if there is not, output the current <result> -->
    <xsl:copy-of select="result" />
  </xsl:if>
</xsl:template>

另一个叫做 Muenchian grouping,@Rubens Farias 刚刚发布了一个答案,展示了如何去做.

And the other one is called Muenchian grouping and @Rubens Farias just posted an answer that shows how to do it.

这篇关于XSLT 中的变量范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆