XSLT:包含()多个字符串

标签 xslt

我在 XSLT 中有一个名为 variable_name 的变量,如果相关产品具有名称为 A 或 B 或两者均为 A & 的属性,我将尝试将其设置为 1 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 的新手。

最佳答案

您可以使用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> 

这将设置名为“variable_name”的新变量以及属性“product/attributes”的值。

了解更多信息... 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> 

如果您提供编写 xslt 所依据的 xml,将会很有帮助。

关于XSLT:包含()多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2310300/

相关文章:

xml - 使用 xsl 谓词根据另一个节点的值选择一个节点

xml - Chrome 显示 : Resource interpreted as Stylesheet but transferred with MIME type application/xml

performance - XSLT迭代或递归性能

function - 如何在xslt中调用void外部函数​​?

xslt - 如何 XPath sum() XSL for-each 循环中的所有先前节点?

xml - XSLT:属性内的 'xsl:value-of'

c# - 在 XSLT 1.0 和分组方面需要帮助

xml - XSLT 将外部图形装入 block 中

xml - XSLT 1.0 分组依据

xslt - 如何避免 XSLT 中所有子节点的命名空间声明