是否可以将其他条件添加到生成密钥的XSL选择中?

时间:2009-11-09 22:03:09

标签: xslt

我有一个商店位置的XML文档,其中有一个Type节点,指示存储的类型,以及一组现有模板,用于根据每个位置的状态向每个位置添加一个键,然后按照状态顺序将这些键添加到HTML中

我需要做的是从显示中排除没有Type 1的所有状态。在for-each循环中很容易跳过那些Locations的显示但是我最终显示的状态没有其中的价值观。

以下是<select/> HTML标记内的代码:

<xsl:apply-templates select="$locations/descendant::Locations" mode="locations.list"/>

该模板如下所示:

<xsl:template match="Locations" mode="locations.list">
    <xsl:apply-templates select="Location[generate-id(.) = generate-id(key('state', State)[1])]"  mode="locations.list.location"/>
</xsl:template>

它调用的模板如下:

<xsl:template match="Location" mode="locations.list.location">
    <option value="state"><xsl:value-of select="normalize-space(State)"/></option>
    <xsl:for-each select="key('state', State)">
        <option value="{normalize-space(SiteKey)}"><xsl:value-of select="normalize-space(Location)/></option>
    </xsl:for-each>
    <option value=""/>
</xsl:template>

通常在<xsl:apply-templates select="Location[generate-id(.) = generate-id(key('state', State)[1])]" mode="locations.list.location"/>我只是在选择中指出我只对类型为'1'的位置感兴趣,但我似乎无法找到一种方法来插入该条件密钥生成代码。

1 个答案:

答案 0 :(得分:1)

如果没有看到整个输入XML,这有点猜测:

<xsl:apply-templates 
  mode   = "locations.list.location"
  select = "Location[
    generate-id(.) 
    = 
    generate-id(key('state', State)[Type = '1'][1])
  ]" 
 />

选择Location个节点,其中唯一ID等于第一个Location与当前State