我在 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/