XSLT:包含多个字符串的()

chu*_*h97 7 xslt

如果有问题的产品具有名称A或B或A和B两者的属性variable_name,我在XSLT中有一个我想要设置的变量1.

<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>
Run Code Online (Sandbox Code Playgroud)

有没有办法使用if语句匹配多个字符串,因为如果存在A或存在B,我的匹配就是匹配.如果A和B都存在,它不会将变量设置为1.由于我是XSLT中的新手,所以对此有任何帮助.

ant*_*res 14

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

例:

<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> 
Run Code Online (Sandbox Code Playgroud)

这将使用name variable_name的值设置名为variable_name的新变量.

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

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

<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> 
Run Code Online (Sandbox Code Playgroud)

如果你提供你正在编写xslt的xml,将会很有帮助.