我正在使用 XSLT 1.0I 转换以下 xml 文件:
我正在使用 XSLT 1.0I 转换以下 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test_1.xsl"?>
<Features>
<Feature item="a1" area="1"></Feature>
<Feature item="a1" area="1"></Feature>
<Feature item="a2" area="1"></Feature>
<Feature item="a2" area="1"></Feature>
<Feature item="b1" area="1"></Feature>
<Feature item="b1" area="1"></Feature>
<Feature item="b2" area="1"></Feature>
</Features>
通过此 xsl 文件:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="KeyStyle" match="Feature" use="@style"/>
<xsl:key name="KeyItem" match="Feature" use="@item"/>
<xsl:template match="Features">
<html>
<xsl:for-each select="Feature[generate-id() = generate-id(key('KeyStyle',@style)[1])]">
<xsl:sort select="@style" order="ascending"/>
Table for style (<xsl:value-of select="@style"/>)
<xsl:for-each select="key('KeyStyle',@style)">
<xsl:sort select="@item" order="ascending"/>
<xsl:if test="not(preceding::*[@item = current()/@item])">
<p>Style=<xsl:value-of select="@style"/></p>
<p>Item=<xsl:value-of select="@item"/></p>
<p>TotalAreaOf_Items (<xsl:value-of select="@item"/>)=<xsl:value-of select="sum(key('KeyItem',@item)/@area)"/></p>
</xsl:if>
</xsl:for-each> ----------
<br/><br/>
</xsl:for-each>
</html>
</xsl:template>
</xsl:stylesheet>
获得此输出:
<html>
Table for style (a)
<p>Item=a1</p>
<p>TotalAreaOf_Items (a1)=4</p>
<p>Item=a2</p>
<p>TotalAreaOf_Items (a2)=4</p>
-----------------
Table for style (b)
<p>Item=b1</p>
<p>TotalAreaOf_Items (b1)=3</p>
<p>Item=b2</p>
<p>TotalAreaOf_Items (b2)=3</p>
</html>
由于 substring(@item, 1, 1) 语句扫描所有 (a) 项目而忽略 (a) 之后的内容,因此我显然得到了以 (a) 开头的所有项目的总和:TotalAreaOf_Items (a1)=4 和 TotalAreaOf_Items (a2)=4
而不是所需的:TotalAreaOf_Items(a1)= 2 和 TotalAreaOf_Items(a2)= 2
我尝试处理