使用XSLT对分组的XML工作流中的数据求和

时间:2013-02-14 06:05:16

标签: xml xslt

我正在尝试在我的XSLT中创建一个类学分的总和,但我无法弄清楚在何处放入sum语句。

这是我创建的总结声明:

   <credit-sum><xsl:value-of select='format-number(sum(CRSMINCRED1),"##")'/></credit-sum>

数据分组三次:

1. by Area of Study
2. By Degree
3. By Requirement or Elective status 

我想仅仅总结“必需”类的学分。但是当我把sum语句放在其中时,只是单独显示每个类的总数而不是类组。

这是XSLT:

<?xml version="1.0"?><!-- DWXMLSource="STX049-2-8-13-parsed.xml" -->
<!DOCTYPE xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:variable name="allSections" select="/CrystalReport/Group/Group/Group/Details/Section" />

<xsl:key name="kArea" match="Section" use="ACPGAREAOFSTUDY1"/>
<xsl:key name="kDegree" match="Section" use="concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1)" />
<xsl:key name="kDepartment" match="Section" use="concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', ICCB1)" />

<xsl:variable name="degreeFirsts" select="$allSections[generate-id() = generate-id(key('kDegree', concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1))[1])]" />
<xsl:variable name="deptFirsts" select="$allSections[generate-id() = generate-id(key('kDepartment', concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', ICCB1))[1])]" />


<xsl:template match="/">
<CrystalReport>
<Degrees>
<xsl:apply-templates select="$allSections[generate-id() = generate-id(key('kArea', ACPGAREAOFSTUDY1)[1])]" mode="group"/>
</Degrees>
</CrystalReport>
</xsl:template>

<xsl:template match="Section" mode="group">
<xsl:variable name="area" select="ACPGAREAOFSTUDY1" />
<xsl:text>&#xA;</xsl:text>
<areaofstudy><xsl:value-of select="$area"/></areaofstudy>
<xsl:apply-templates select="$degreeFirsts[ACPGAREAOFSTUDY1 = $area]" mode="degree"/>
</xsl:template>

<xsl:template match="Section" mode="degree">
<xsl:variable name="area" select="ACPGAREAOFSTUDY1" />
<xsl:variable name="degree" select="ACPGDEGREE1" />
<Degree>
<xsl:apply-templates select="$deptFirsts[ACPGAREAOFSTUDY1 = $area and ACPGDEGREE1 = $degree]" mode="department">
<xsl:sort select="ACADPROGRAMSID1" />
</xsl:apply-templates>
</Degree>
</xsl:template>

<xsl:template match="Section" mode="department">
<department><xsl:text>&#xA;</xsl:text><Degreetitle><xsl:apply-templates select="ACPGDEGREE1" /></Degreetitle>
<Certtitle><xsl:apply-templates select="CCD11" /></Certtitle><xsl:text>&#xA;</xsl:text>
<DegreeDesc><xsl:apply-templates select="ACPGCOMMENTS1" /></DegreeDesc>
<xsl:text>&#xA;ICCB Code </xsl:text><ICCBcode><xsl:apply-templates select="ICCB1" /></ICCBcode>
<xsl:text> | Field of Study Code: </xsl:text><ProgramID><xsl:apply-templates select="ACADPROGRAMSID1" /></ProgramID>

<xsl:variable name="courses" select="key('kDepartment', concat(ACPGAREAOFSTUDY1, '+', ACPGDEGREE1, '+', ICCB1))" />

<xsl:call-template name="CourseGroup">
<xsl:with-param name="courses" select="$courses[FlagElectives1 = 'N']" />
<xsl:with-param name="title" select="'Program Requirements'" />
</xsl:call-template>

<xsl:call-template name="CourseGroup">
<xsl:with-param name="courses" select="$courses[FlagElectives1 = 'Y']" />
<xsl:with-param name="title" select="'Program Electives'" />
</xsl:call-template>
<xsl:apply-templates select="ACPGHOMELANGNOTREQDRSN1" />
</department>
</xsl:template>

<xsl:template name="CourseGroup">
<xsl:param name="courses" />
<xsl:param name="title" />

<xsl:if test="$courses"><xsl:text>&#xA;</xsl:text>
<req-electitle><xsl:value-of select="$title" /></req-electitle>
<xsl:apply-templates select="$courses">
<xsl:sort select="FlagElectives1" order="ascending" />
<xsl:sort select="CRSSUBJECT1" />
<xsl:sort select="CRSNO1" />
</xsl:apply-templates>
</xsl:if>
</xsl:template>

<xsl:template match="Section">
<xsl:text>&#xA;</xsl:text>
<Details>
<class><deptname><xsl:apply-templates select="CRSSUBJECT1" /></deptname><xsl:text>  </xsl:text>
<courseno><xsl:apply-templates select="CRSNO1" /></courseno><xsl:text>  </xsl:text>
<classname><xsl:apply-templates select="CRSTITLE1" /></classname><xsl:text> </xsl:text>
<classcredit><xsl:apply-templates select="CRSMINCRED1" /></classcredit>
<xsl:apply-templates select="CRSMAXCRED1" />
</class>
</Details>
</xsl:template>

<xsl:template match="ACPGHOMELANGNOTREQDRSN1[string-length() != 0]">
<xsl:text>&#xA;</xsl:text><totalcredits><xsl:value-of select="normalize-space(.)" /></totalcredits>
</xsl:template>

<xsl:template match="CRSMAXCRED1[string-length() != 0]">
<xsl:text> to </xsl:text><maxcredits><xsl:value-of select="normalize-space(.)" /></maxcredits>
</xsl:template>

<xsl:template match="ACPGDEGREE1/text()">
<xsl:value-of select="concat(., ' DEGREE')"/>
</xsl:template>

<xsl:template match="CCD11/text()">
<xsl:value-of select="('CERTIFICATE')" />
</xsl:template>

</xsl:stylesheet>

最终输出将导入Adobe InDesign,应如下所示:      ...

 ICCB Code 4903 | Field of Study Code: AIRC.CER.ENERG
 Program Requirements
 AIRC   2232    Energy Audits/Economics                     2
 AIRC   2240    Load Calculations and Duct Design           5
 AIRC   2260    Heating and Air Conditioning Contracting    3
 Total Credits                                             10

1 个答案:

答案 0 :(得分:0)

似乎CourseCroup模板是进行此更改的地方:

  <xsl:template name="CourseGroup">
    <xsl:param name="courses" />
    <xsl:param name="title" />
    <xsl:param name="requiredCourses" select="false()" />

    <xsl:if test="$courses">
      <xsl:text>&#xA;</xsl:text>
      <req-electitle>
        <xsl:value-of select="$title" />
      </req-electitle>
      <xsl:apply-templates select="$courses">
        <xsl:sort select="FlagElectives1" order="ascending" />
        <xsl:sort select="CRSSUBJECT1" />
        <xsl:sort select="CRSNO1" />
      </xsl:apply-templates>
      <xsl:if test="$requiredCourses">
        <credit-sum>
          <xsl:value-of select='format-number(sum($courses/CRSMINCRED1),"##")'/>
        </credit-sum>
      </xsl:if>
    </xsl:if>
  </xsl:template>

然后你只需要修改第一个call-template来添加一个额外的参数:

  <xsl:call-template name="CourseGroup">
    <xsl:with-param name="courses" select="$courses[FlagElectives1 = 'N']" />
    <xsl:with-param name="title" select="'Program Requirements'" />
    <xsl:with-param name="requiredCourses" select="true()" />
  </xsl:call-template>