如何在XSLT,XPath中的for-each循环中添加多个值?

时间:2014-05-30 00:51:24

标签: xml xslt xpath foreach sum

我想知道如何在for-each循环中添加多个值?以下是有问题的代码:

<xsl:for-each select="all/courses/allcourses/course">
          <course code="{c_code}">

<xsl:variable name="courseCreditPoints" select="coursecp" />

    <xsl:variable name="numberOfEnrolled" select="count(student[sum(results/u_points)!= $courseCreditPoints])" />
    <xsl:variable name="thisWontWork" select="$numberOfEnrolled + $numberOfEnrolled" />
    <xsl:if test="position() &lt; 5">
        <xsl:value-of select="$thisWontWork" />
    </xsl:if>           
</course>
</xsl:for-each> <br />

XML:

<?xml version="1.0" encoding="UTF-8"?>
<all>
    <courses>
        <allcourses>
            <course>
                <c_code>U65</c_code>
                <c_title>Computer Science</c_title>
                <coursecp>360</coursecp>
                <student>
                    <studentID>10265654</studentID>
                    <fname>Sarah</fname>
                    <lname>Clarke</lname>
                    <results>
                        <u_title>Communicating in an IT Environment</u_title>
                        <u_code>CSG1132</u_code>
                        <u_points>15</u_points>
                        <result>65</result>
                    </results>
                    <results>
                        <u_title>Programming Principles</u_title>
                        <u_code>CSP1150</u_code>
                        <u_points>15</u_points>
                        <result>45</result>
                    </results>
                    <results>
                        <u_title>Computer Fundamentals</u_title>
                        <u_code>ENS1161</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                </student>
                <student>
                    <studentID>10276768</studentID>
                    <fname>John</fname>
                    <lname>Smith</lname>
                    <results>
                        <u_title>Computer Fundamentals</u_title>
                        <u_code>ENS1161</u_code>
                        <u_points>15</u_points>
                        <result>85</result>
                    </results>
                    <results>
                        <u_title>Operating Systems</u_title>
                        <u_code>CSG1102</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Computer Forensics</u_title>
                        <u_code>CSG2305</u_code>
                        <u_points>15</u_points>
                        <result>75</result>

                    </results>
                </student>
            </course>
            <course>
                <c_code>Y89</c_code>
                <c_title>Bachelor of Science (Cyber Security)</c_title>
                <coursecp>360</coursecp>
                <student>
                    <studentID>10254548</studentID>
                    <fname>Jacob</fname>
                    <lname>Green</lname>
                    <results>
                        <u_title>Communicating in an IT Environment</u_title>
                        <u_code>CSG1132</u_code>
                        <u_points>15</u_points>
                        <result>77</result>
                    </results>
                    <results>
                        <u_title>Programming Principles</u_title>
                        <u_code>CSP1150</u_code>
                        <u_points>15</u_points>
                        <result>45</result>
                    </results>
                    <results>
                        <u_title>Computer Fundamentals</u_title>
                        <u_code>ENS1161</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Systems Analysis</u_title>
                        <u_code>CSI1241</u_code>
                        <u_points>15</u_points>
                        <result> 87 </result>
                    </results>
                </student>
                <student>
                    <studentID>10254548</studentID>
                    <fname>Mason</fname>
                    <lname>Johnson</lname>
                    <results>
                        <u_title>Communicating in an IT Environment</u_title>
                        <u_code>CSG1132</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Programming Principles</u_title>
                        <u_code>CSP1150</u_code>
                        <u_points>15</u_points>
                        <result>85</result>
                    </results>
                    <results>
                        <u_title>Computer Fundamentals</u_title>
                        <u_code>ENS1161</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Systems Analysis</u_title>
                        <u_code>CSI1241</u_code>
                        <u_points>15</u_points>
                        <result> 65 </result>
                    </results>
                </student>
                <student>
                    <studentID>102534548</studentID>
                    <fname>Jack</fname>
                    <lname>Johnson</lname>
                    <results>
                        <u_title>Communicating in an IT Environment</u_title>
                        <u_code>CSG1132</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Programming Principles</u_title>
                        <u_code>CSP1150</u_code>
                        <u_points>15</u_points>
                        <result>85</result>
                    </results>
                    <results>
                        <u_title>Computer Fundamentals</u_title>
                        <u_code>ENS1161</u_code>
                        <u_points>15</u_points>
                        <result>55</result>
                    </results>
                    <results>
                        <u_title>Systems Analysis</u_title>
                        <u_code>CSI1241</u_code>
                        <u_points>15</u_points>
                        <result> 65 </result>
                    </results>
                </student>
            </course>
        </allcourses>
    </courses>
</all>

我想要实现的目标:所以基本上如果我要输出$ numberOfEnrolled,我会得到3个值,2,3和0.如何将它们一起添加?正如您在上面的代码中所看到的,我尝试过这样做,但它最终失败了,而是输出4,6和0.将这些单独的值组合在一起的最佳方法是什么?

1 个答案:

答案 0 :(得分:2)

我只能在输入XML中看到2 course个节点(在我编辑之前)。

将下面的样式表应用于输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:strip-space elements="*"/>

    <xsl:output indent="yes"  omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:for-each select="all/courses/allcourses/course">
            <course code="{c_code}">
                <xsl:variable name="courseCreditPoints" select="coursecp" />
                <xsl:variable name="numberOfEnrolled" select="count(student[sum(results/u_points)!= $courseCreditPoints])" />
                <numberOfEnrolled><xsl:value-of select="$numberOfEnrolled" /></numberOfEnrolled>
            </course>
        </xsl:for-each>
        <totalEnrollees>
            <xsl:value-of select="count(//student[sum(results/u_points)!= preceding-sibling::coursecp])" />
        </totalEnrollees>
    </xsl:template>

</xsl:stylesheet>

将产生:

<course code="U65">
    <numberOfEnrolled>2</numberOfEnrolled>
</course>
<course code="Y89">
    <numberOfEnrolled>3</numberOfEnrolled>
</course>
<totalEnrollees>5</totalEnrollees>