如何进行列表选项

时间:2008-12-16 15:57:08

标签: xslt

在伪代码中

If Domain inList(GB,US,ES,FR Then
   Print This Html
Else
  Print This HTML
EndIf

4 个答案:

答案 0 :(得分:4)

这是非常的一般形式,但是在设计时你不知道列表的地方,只要你能获得对表示列表的节点集的引用,你就可以做简单测试如:

<xsl:when test="$listset/item[@property=$variable]">

其中说$ variable = / foo / bar / @ property和$ listset = / foo / list for XML

<?xml version="1.0"?>
<foo>
  <bar property="gb" />
  <list>
    <item property="gb"/>
    <item property="us"/>
  </list>
</foo>

答案 1 :(得分:1)

尝试使用xsl:choose.(另请参阅the spec here)它提供了基本的if / else功能。编辑 - 我做了测试,它的工作原理:

<xsl:choose>
  <xsl:when test="domain = 'GB' or domain = 'US' or domain = 'ES' or domain = 'FR'">
    print this html
  </xsl:when>
  <xsl:otherwise>
    print other html
  </xsl:otherwise>
</xsl:choose>

答案 2 :(得分:0)

如果您在给定文件的情况下使用XSLT 2.0                            

你可以使用这样的东西:
<xsl:template match="list/item">
Property [<xsl:value-of select="@property"/>] html
</xsl:template>

<xsl:template match="list/item[some $x in ('us', 'gb') satisfies $x eq @property ]">
Property [<xsl:value-of select="@property"/>] HTML
</xsl:template>

答案 3 :(得分:0)

当前3个答案未提及的另一个解决方案是要有一个选项字符串,您要与之比较domain的值。然后,当@test的值是分隔值之一时,以下XPath表达式(在<xsl:if><xsl:when>的{​​{1}}属性中的确切评估为true()在字符串中(我们在这个具体例子中使用了一个空格作为分隔符):

domain

这里我们假设contains(' GB US ES ', concat(' ', domain, ' '))的值没有空格。如果无法保证,XPath表达式也可以验证此要求:

domain
not(contains(domain, ' '))
and