在xslt中不执行多个条件

时间:2019-02-23 20:32:57

标签: xslt

我希望成绩应符合要求的成绩标准。我尝试了xsl:choose来检查标记条件,但是它没有检查when条件,而是始终执行else条件。谁能分享解决方案?

XML:

{
<marklist>
    <student>
        <reg_no>100</reg_no>
        <name>aaa</name>
        <marks>
            <CD>55</CD>
            <AI>44</AI>
        </marks>
    </student>
    <student>
        <reg_no>101</reg_no>
        <name>bbb</name>
        <marks>
            <CD>65</CD>
            <AI>46</AI>
        </marks>
    </student>
</marklist>
}

所需的XML格式:

{
<marklist>
    <student>
        <reg_no>100</reg_no>
        <name>aaa</name>
        <grade>
            <CD>D</CD>
            <AI>E</AI>
        </grade>
    </student>
    <student>
        <reg_no>101</reg_no>
        <name>bbb</name>
        <grade>
            <CD>C</CD>
            <AI>E</AI>
        </grade>
    </student>
</marklist>
}

1 个答案:

答案 0 :(得分:0)

<xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="CD">
        <CD>
        <xsl:choose>
            <xsl:when test=". &lt; 60 and . &gt; 50">
                    <xsl:value-of select="'D'"/>
            </xsl:when>
            <xsl:when test=". &lt; 70 and . &gt; 60">
                <xsl:value-of select="'C'"/>
            </xsl:when>
        </xsl:choose>
        </CD>
    </xsl:template>

    <xsl:template match="AI">
        <AI>
        <xsl:choose>
            <xsl:when test=". &lt; 60 and . &gt; 50">
                <xsl:value-of select="'D'"/>
            </xsl:when>
            <xsl:when test=". &lt; 70 and . &gt; 60">
                <xsl:value-of select="'C'"/>
            </xsl:when>
            <xsl:when test=". &lt; 50 and . &gt; 30">
                <xsl:value-of select="'E'"/>
            </xsl:when>
        </xsl:choose>
        </AI>
    </xsl:template>

You may check value of CD and AI and apply Grade according to that