根据Element值更改结果显示

时间:2013-02-12 04:21:39

标签: xml xslt

我的XML包含大学课程的信息。课程按职衔,课程编号和学分总数显示。

我的XSLT按学习领域对数据进行分组,然后按课程编号列出课程,并提供课程说明和学分总数。一切都很好,但我想调整最后的信用时数。

每个班级至少有1-4个学分。有些班级有最多的学分。

如果存在,我的XSLT会同时提取最低点数<CRSMINCRED1>和最高点数<CRSMAXCRED1>

然后,XSLT构建最终显示,使其显示为以下之一:

当班级数据仅显示<CRSMINCRED1>时,显示如下所示: “1学分” “2学分” “3学分” “4学分”

当班级数据同时包含<CRSMINCRED1><CRSMAXCRED1>时,显示如下所示: “1至4学分。”

除了第一项外,一切都很好。当课程只有“1”最低学分而没有最高学分时,我希望显示为:

“1学分”

我试图制作一个“变量”和“如果”的陈述但没有成功。

以下是一些示例XML:

<?xml version="1.0" encoding="UTF-8"?>
<CrystalReport>
<Details Level="1">
<Section SectionNumber="0">
<CRSTITLE1>Clinical Applications of CT I</CRSTITLE1>
<DEPTSDESC1>Diagnostic Medical Imaging</DEPTSDESC1>
<CRSNO1>2511</CRSNO1>
<CRSMINCRED1>3</CRSMINCRED1>
<CRSMAXCRED1/>
</Section>
</Details>
<Details Level="1">
<Section SectionNumber="0">
<CRSTITLE1>Clinical Applications of CT II</CRSTITLE1>
<CRSDEPTS1>DMI</CRSDEPTS1>
<DEPTSDESC1>Diagnostic Medical Imaging 2</DEPTSDESC1>
<CRSNO1>2512</CRSNO1>
<CRSMINCRED1>1</CRSMINCRED1>
<CRSMAXCRED1>4</CRSMAXCRED1>
</Section>
</Details>
<Details Level="1">
<Section SectionNumber="0">
<CRSTITLE1>Clinical Applications of CT III</CRSTITLE1>
<CRSDEPTS1>DMI</CRSDEPTS1>
<DEPTSDESC1>Diagnostic Medical Imaging 3</DEPTSDESC1>
<CRSNO1>2513</CRSNO1>
<CRSMINCRED1>1</CRSMINCRED1>
<CRSMAXCRED1/>
</Section>
</Details>
</CrystalReport>

以下是我正在努力开展工作的XSLT的一部分:

<xsl:template match="CrystalReport">
...
 <xsl:apply-templates select="Section/CRSMINCRED1|Section/CRSMAXCRED1"/>

</xsl:template>

<xsl:template match="Section/CRSMINCRED1">
<xsl:if test=". = 1">
<mincredit><xsl:value-of select="."/></mincredit><xsl:text> credit hour</xsl:text></xsl:if>
<xsl:if test=". &gt; 1">
<mincredit><xsl:value-of select="."/></mincredit><xsl:text> credit hours</xsl:text></xsl:if>
</xsl:template>

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

</xsl:stylesheet>

这个样式表产生我想要的东西,直到课程显示最低和最高学分,然后我得到以下显示:“1学分到4学分”

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我建议做这样的事情:

  <xsl:template match="CrystalReport">
    ...
    <xsl:apply-templates select="Section" mode="credits"/>

  </xsl:template>

  <xsl:template match="Section" mode="credits">
    <xsl:apply-templates select="CRSMINCRED1" />
    <xsl:text> credit hour</xsl:text>
    <xsl:apply-templates select="CRSMINCRED1[. != 1]" mode="pluralize" />
  </xsl:template>

  <xsl:template match="Section[normalize-space(CRSMAXCRED1)]" mode="credits">
    <xsl:apply-templates select="CRSMINCRED1" />
    <xsl:text> to </xsl:text>
    <xsl:apply-templates select="CRSMAXCRED1" />
    <xsl:text> credit hour</xsl:text>
    <xsl:apply-templates select="CRSMAXCRED1[. != 1]" mode="pluralize"/>
  </xsl:template>

  <xsl:template match="CRSMINCRED1">
    <mincredit>
      <xsl:value-of select="." />
    </mincredit>
  </xsl:template>

  <xsl:template match="CRSMAXCRED1">
    <maxcredit>
      <xsl:value-of select="." />
    </maxcredit>
  </xsl:template>

  <xsl:template match="*" mode="pluralize">
    <xsl:text>s</xsl:text>
  </xsl:template>