首页 > 解决方案 > 如何使用 XSLT 编写多个 when 条件?

问题描述

我试图在满足 3 个条件时获得响应,否则它应该拒绝并生成失败响应。以下是我的示例输入:

 <Message>
    <Header>
    <Role actionCode="04">
                <RoleCode>BM01</RoleCode>
             </Role>
             <Role actionCode="04">
                <RoleCode>CM011</RoleCode>
             </Role>
    <Common>
   <tag>Data</tag>
   <tag2>Data2</tag2>
    <Pstatus>111</Pstatus>
    </Common>
    </Header>
    </Message>

我把条件写成

<xsl:when test="$RoleCode = 'BM01 and CM011' and $Pstatus='111 OR 112' ">
<xsl:value-of select="$tag"/>
<xsl:when test="$RoleCode = 'BM02 and CM011' and $Pstatus='111 OR 112' ">
<xsl:value-of select="$tag2"/>
<xsl:when test="$Pstatus='101' ">Reject
<xsl:otherwise>Reject</xsl:otherwise>

当角色代码与BM01/BM02 和CM011 以及Pstatus 为111 或112 匹配时,它应该复制数据,如果pstatus 是101 接收到,它应该拒绝。我只收到拒绝消息作为输出,但条件没有执行。有人可以帮忙吗?

标签: xmlxslt

解决方案


我猜(!)你想做类似的事情:

<xsl:template match="Message">
    <xsl:variable name="RoleCode" select="Header/Role/RoleCode" />
    <xsl:variable name="Pstatus" select="Header/Common/Pstatus" />
    <xsl:variable name="tag" select="Header/Common/tag" />
    <xsl:variable name="tag2" select="Header/Common/tag2" />
    <xsl:choose>
        <xsl:when test="$RoleCode='BM01' and $RoleCode='CM011' and ($Pstatus='111' or  $Pstatus='112')">
            <xsl:value-of select="$tag"/>
        </xsl:when>
        <xsl:when test="$RoleCode='BM02' and $RoleCode='CM011' and ($Pstatus='111' or  $Pstatus='112')">
            <xsl:value-of select="$tag2"/>
        </xsl:when>
        <xsl:otherwise>Reject</xsl:otherwise>
    </xsl:choose>
</xsl:template>

推荐阅读