XSLT:包含()多个字符串 [英] XSLT: contains() for multiple strings

查看:45
本文介绍了XSLT:包含()多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 XSLT 中有一个名为 variable_name 的变量,我试图将其设置为 1,如果有问题的产品具有名称为 A 或 B 或 A & 的属性;B.

I have a variable in XSLT called variable_name which I am trying to set to 1, if the Product in question has attributes with name A or B or both A & B.

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
    <xsl:if test="@attributename='A' or @attributename='B'">
      <xsl:value-of select="1"/>
    </xsl:if>
  </xsl:for-each>
</xsl:variable>

有没有办法使用 if 语句匹配多个字符串,因为我只匹配 A 存在或 B 存在.如果 A &B 存在,它没有将变量设置为 1.我是 XSLT 新手,因此不胜感激.

Is there any way to match multiple strings using the if statement, as mine just matches if A is present or B is present. If both A & B are present, it does not set the variable to 1. Any help on this would be appreciated as I am a newbie in XSLT.

推荐答案

你可以使用 xsl:choose 语句,类似于常用编程语言中的 switch :

You can use xsl:choose statement, it's something like switch in common programming languages:

示例:

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
  <xsl:choose>
    <xsl:when test="@attributename='A'">
      1
    </xsl:when>
    <xsl:when test=" @attributename='B'">
      1
    </xsl:when>
    <!--... add other options here-->
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
  </xsl:for-each>
</xsl:variable> 

这将设置名为 variable_name 的新变量,其值为 product/attributes.

This will set new variable with name variable_name with the value of attribute product/attributes.

欲了解更多信息...http://www.w3schools.comwww.w3schools.com/xsl/el_choose.asp

OP 要求的另一种方式(有点脏):

And another way (a little dirty) by OP's request:

<xsl:variable name="variable_name">
  <xsl:for-each select="product/attributes">
    <xsl:if test="contains(text(), 'A') or contains(text(), 'B')">
       1
    </xsl:if>
  </xsl:for-each>
</xsl:variable> 

如果您提供用于编写​​ xslt 的 xml,将会很有帮助.

It will be helpful if you provide the xml you're writing your xslt against.

这篇关于XSLT:包含()多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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