使用来自不同父节点的节点值的 XSL 计算
XSL Calculation With Node Values From Different Parent
我已经为此工作了几个小时,我相信你们,社区,将能够提出以下逻辑:
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 个节点,确定哪些代码是 "BAD".. 然后从 GPA 计算中排除这些课程。我的逻辑只有在返回单个 /Section/Class[@CODE='BAD']/@Id_num 时才有效,因此我需要找到一种方法让所有 "BAD" 课程成为!=(不包括)在计算中。
使用 xsl 版本 1.0。
如果使用<xsl:value-of>
来定义变量值,它们都是字符串,所以最好直接使用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
If one object to be compared is a node-set and the other is a string,
then the comparison will be true if and only if there is a node in the
node-set such that the result of performing the comparison on the
string-value of the node and the other string is true.
我已经为此工作了几个小时,我相信你们,社区,将能够提出以下逻辑:
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 个节点,确定哪些代码是 "BAD".. 然后从 GPA 计算中排除这些课程。我的逻辑只有在返回单个 /Section/Class[@CODE='BAD']/@Id_num 时才有效,因此我需要找到一种方法让所有 "BAD" 课程成为!=(不包括)在计算中。
使用 xsl 版本 1.0。
如果使用<xsl:value-of>
来定义变量值,它们都是字符串,所以最好直接使用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
If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true.