更新时间:2023-02-12 18:43:50
你可以使用 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.