我的输入 xml 就像
<Reports>
<Report>
<ReportHeader>
<Name>ABC</Name>
<ReportNo>123</ReportNo>
</ReportHeader>
<ReportLine>
<ReportNo>123</ReportNo>
<LineGroup>XYZ</LineGroup>
<LineAmount>10</LineAmount>
<ReportLine>
<ReportLine>
<ReportNo>123</ReportNo>
<LineGroup>PQR</LineGroup>
<LineAmount>20</LineAmount>
<ReportLine>
<ReportLine>
<ReportNo>123</ReportNo>
<LineGroup>XYZ</LineGroup>
<LineAmount>30</LineAmount>
<ReportLine>
</Report>
<Report>
<ReportHeader>
<Name>DEF</Name>
<ReportNo>456</ReportNo>
</ReportHeader>
<ReportLine>
<ReportNo>456</ReportNo>
<LineGroup>IJK</LineGroup>
<LineAmount>40</LineAmount>
<ReportLine>
<ReportLine>
<ReportNo>456</ReportNo>
<LineGroup>XYZ</LineGroup>
<LineAmount>50</LineAmount>
<ReportLine>
<ReportLine>
<ReportNo>456</ReportNo>
<LineGroup>IJK</LineGroup>
<LineAmount>60</LineAmount>
<ReportLine>
</Report>
</Reports>
我的输出 xml 就像
<NewReport>
<Header>
<Name>ABC</Name>
<HeaderNo>456</HeaderNo>
</Header>
<Line>
<LineGroup>XYZ</LineGroup>
<Amount>40</Amount>
</Line>
<Line>
<LineGroup>PQR</LineGroup>
<Amount>20</Amount>
</Line>
</NewReport>
<NewReport>
<Header>
<Name>DEF</Name>
<HeaderNo>456</HeaderNo>
</Header>
<Line>
<LineGroup>IJK</LineGroup>
<Amount>100</Amount>
</Line>
<Line>
<LineGroup>XYZ</LineGroup>
<Amount>50</Amount>
</Line>
</NewReport>
我使用的 XSL 是
<xsl:key name="KLinesByGroup" match="/Reports/Report/ReportLine" use="LineGroup"/>
<xsl:key name="KLinesByReportNo" match="/Reports/Report/ReportLine" use="ReportNo"/>
<xsl:template match="/">
<xsl:for-each select="/ns2:Reports/ns2:Report">
<xsl:variable name="HeaderReportNo"><xsl:value-of select="ReportHeader/ReportNo"/></xsl:variable>
<Header>
<Name><xsl:value-of select="ReportHeader/Name"/></Name>
<HeaderNo><xsl:value-of select="ReportHeader/ReportNo"/></HeaderNo>
<xsl:apply-templates select="key('KLinesByReportNo', $HeaderReportNo)[1]" mode="reportno-mode"/>
</Header>
</xsl:for-each>
</xsl:template>
<xsl:template match="ReportLine" mode="reportno-mode">
<xsl:apply-templates select="key('KLinesByReportNo', ReportNo)[generate-id() = generate-id(key('KLinesByGroup', LineGroup)[1])]" mode="group-mode"/>
</xsl:template>
<xsl:template match="ReportLine" mode="group-mode">
<Line>
<xsl:value-of select="sum(key('KLinesByGroup', LineGroup)/LineAmount)"/>
</Line>
</xsl:template>
但输出不是我所期望的。我得到的输出是在组级别或行级别添加所有金额,但不是在行和组级别。任何人都可以帮忙。
谢谢