0

我有一个这样的transcript.xml,想计算gpa

    <?xml version="1.0" encoding="UTF-8"?>
<transcript>
    <courses>
        <course>
            <code>IK2210</code>        
            <credits>3</credits>
            <grade>A</grade>
        </course>
        <course>
            <code>IB2210</code>
            <credits>3</credits>
            <grade>A</grade>
        </course>
        <course>
            <code>IB2210</code>
            <credits>1</credits>
            <grade>C</grade>
        </course>
        <course>
            <code>ID1234</code>
            <credits>2</credits>
            <grade>B</grade>
        </course>
    </courses>

</transcript>

我像这样导入了成绩单

<xsl:variable name="transcript" select="document('transcript2.xml')/transcript"/> 

我试着这样做

<xsl:value-of select=" (4*(sum($transcript/courses/course/credits[$transcript/courses/course/grade='A'])) + 3*(sum($transcript/courses/course/credits[$transcript/courses/course/grade='B'])) + 2*(sum($transcript/courses/course/credits[$transcript/courses/course/grade='C'])) + 1*(sum($transcript/courses/course/credits[$transcript/courses/course/grade='D']))) div (sum($transcript/courses/course/credits))"/>

但零件

(sum($transcript/courses/course/credits[$transcript/courses/course/grade='A']))

总结所有学分

怎么查条件?谢谢

4

1 回答 1

2

我相信如果不使用以下公式,您的公式会起作用:

(sum($transcript/courses/course/credits[$transcript/courses/course/grade='A']))

你用过:

(sum($transcript/courses/course[grade='A']/credits))

但也许这更简洁一点:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:key name="kGrade" match="credits" use="../grade"/>

  <xsl:template match="/">
    <root>
      <xsl:variable name="transcript" select="document('SOTranscript.xml')/transcript" />
      <xsl:apply-templates select="$transcript" mode="getTotal" />
    </root>
  </xsl:template>

  <xsl:template match="transcript" mode="getTotal">
    <xsl:value-of
            select="(4 * sum(key('kGrade', 'A')) + 
                  3 * sum(key('kGrade', 'B')) + 
                  2 * sum(key('kGrade', 'C')) + 
                  sum(key('kGrade', 'D'))
                 ) div sum(courses/course/credits)"/>
  </xsl:template>
</xsl:stylesheet>

在您的示例输入上使用时,结果是:

<root>3.5555555555555553</root>

我相信这是预期值。

于 2013-01-27T12:49:28.257 回答