【问题标题】:XSL Calculation With Node Values From Different Parent使用来自不同父节点的节点值进行 XSL 计算
【发布时间】:2015-08-24 21:47:31
【问题描述】:

我已经为此工作了几个小时,我相信您,社区,将能够提出以下逻辑:

XML 节点

<Main>
 <Info>
  <Class Discipline="PHIL" Number="100" Gpa_grade_pts="3" Id_num="0030" Gpa_credits="3" >
  <Class Discipline="HIST" Number="103" Gpa_grade_pts="6" Id_num="0005" Gpa_credits="3" >
  <Class Discipline="HIST" Number="262" Gpa_grade_pts="9" Id_num="0026" Gpa_credits="3" >
 </Info>
 <Section>
  <Class Discipline="HIST" Number="103" Credits="3" Id_num="0030" Code="BAD"/>
  <Class Discipline="HIST" Number="111" Credits="3" Id_num="0005" Code="GOOD"/>
  <Class Discipline="HIST" Number="262" Credits="3" Id_num="0026" Code="BAD"
 </Section>
</Main>

我基本上需要遍历并找到 /Section/Class/Code="BAD" 的课程,并从以下 XSL (GPA) 计算中排除这些课程...

XSL

 <xsl:variable name="IdNum">
  <xsl:value-of select="Section/Class[@Code = 'BAD']/@Id_num" />  
 </xsl:variable>  

 <xsl:variable name="GpaGradePts">
  <xsl:value-of select="sum(Info/Class[@Id_num != $IdNum]/@Gpa_grade_pts)" />
 </xsl:variable>

 <xsl:variable name="GpaCredits">
  <xsl:value-of select="sum(Info/Class[@Id_num != $IdNum]/@Gpa_credits)" />
 </xsl:variable>

 <xsl:variable name="Gpa">
  <xsl:value-of select='format-number($GpaGradePts div $GpaCredits, "#.00")' /> <!-- New GPA -->
 </xsl:variable>

 <!-- Display the new value -->

 <xsl:value-of select="$Gpa" />

总结:

我需要比较 XML 各个部分的 Id_num 节点,确定哪些代码是“坏的”......然后从 GPA 计算中排除这些课程。我的逻辑只有在返回单个 /Section/Class[@CODE='BAD']/@Id_num 时才有效,因此我需要找到一种方法让所有“BAD”课程成为!=(不包括)在计算中.

使用 xsl 1.0 版。

【问题讨论】:

    标签: xml xslt xhtml xslt-1.0


    【解决方案1】:

    如果你使用&lt;xsl:value-of&gt;定义变量值,它们都是字符串,所以最好直接使用xsl:variable中的select属性。

    您可以像以前一样开始收集 BAD 课程的 ID(如果 MAIN 是上下文节点):

    <xsl:variable name="BadIdNums" select="Section/Class[@Code = 'BAD']/@Id_num"/>  
    

    以下是如何使用此节点集变量作为“信息”部分中课程元素的过滤器:

    <xsl:variable name="GpaGradePts" 
        select="sum(Info/Class[not(string(@Id_num) = $BadIdNums)]/@Gpa_grade_pts)"/>
    

    了解过滤器remember那个

    如果要比较的一个对象是节点集,另一个是字符串, 那么当且仅当在 节点集,使得对 节点的字符串值,其他字符串为真。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多