[英]XSLT 1.0 Muenchian grouping implementation is not working
Working XSLT 2.0 implementation:工作 XSLT 2.0 实现:
<xsl:template name="SummaryRows">
<xsl:for-each-group select="//Item/ItemGroup/ItemEntry" group-by="Description">
<tr>
<td>
<xsl:value-of select="current-grouping-key()"/>
</td>
<td></td>
<td>
<xsl:call-template name="formatNumber2">
<xsl:with-param name="number" select="format-number(number(sum(current-group()/ItemSum/text())), '#.##')"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each-group>
</xsl:template>
Attempt to implement Muenchian grouping :尝试实现Muenchian 分组:
<xsl:key name="items-by-description" match="ItemEntry" use="Description"/>
<xsl:template name="SummaryRows">
<xsl:for-each select="//Item/ItemGroup/ItemEntry[count(. | key('items-by-description', Description)[1]) = 1]">
<xsl:variable name="current-grouping-key" select="Description"/>
<xsl:variable name="current-group" select="key('items-by-description', $current-grouping-key)"/>
<xsl:for-each select="$current-group">
<tr>
<td>
<xsl:value-of select="$current-grouping-key"/>
</td>
<td/>
<td align="right">
<xsl:call-template name="formatNumber2">
<xsl:with-param name="number" select="format-number(number(sum(./ItemSum/text())), '#.##')"/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
Example xml snippet:示例 xml 片段:
<Item>
<ItemGroup groupId="1">
<ItemEntry>
<Description>A</Description>
<ItemSum>6301.1</ItemSum>
</ItemEntry>
<ItemEntry>
<Description>B</Description>
<ItemSum>901.1</ItemSum>
</ItemEntry>
</ItemGroup>
<ItemGroup groupId="2">
<ItemEntry>
<Description>C</Description>
<ItemSum>631.1</ItemSum>
</ItemEntry>
<ItemEntry>
<Description>A</Description>
<ItemSum>9.1</ItemSum>
</ItemEntry>
</ItemGroup>
</Item>
It should return list of ItemEntry
's Description
(that suppose to be current-grouping-key() in xslt 2.0).它应该返回ItemEntry
的Description
列表(假设是 xslt 2.0 中的 current-grouping-key() )。 For each such group it has to have sum of all ItemSum
of that group.对于每个这样的组,它必须具有该组的所有ItemSum
的总和。
Example result:结果示例:
A 6310.2
B 901.1
C 631.1
The <xsl:for-each select="$current-group">
loop is not executed resulting in empty set. <xsl:for-each select="$current-group">
循环未执行导致空集。 What is the implementation missing?缺少什么实现?
Update:更新:
The environment is Java 8环境是Java 8
Transformation factory properties / initialization:转换工厂属性/初始化:
System.setProperty("javax.xml.transform.TransformerFactory", "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
System.setProperty(XPathFactory.DEFAULT_PROPERTY_NAME + ":" + XPathFactory.DEFAULT_OBJECT_MODEL_URI,
"org.apache.xpath.jaxp.XPathFactoryImpl");
XPathFactory xpf = XPathFactory.newInstance();
As I said in a comment, you don't need two nested for-each
elements to replace a single for-each-group
, you just use one, then a minimal sample would be正如我在评论中所说,您不需要两个嵌套的for-each
元素来替换单个for-each-group
,您只需使用一个,那么最小样本将是
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes" doctype-system="about:legacy-doctype"/>
<xsl:key name="items-by-description" match="ItemEntry" use="Description"/>
<xsl:template match="/*">
<xsl:for-each select="//Item/ItemGroup/ItemEntry[count(. | key('items-by-description', Description)[1]) = 1]">
<xsl:variable name="current-grouping-key" select="Description"/>
<xsl:variable name="current-group" select="key('items-by-description', $current-grouping-key)"/>
<tr>
<td>
<xsl:value-of select="$current-grouping-key"/>
</td>
<td/>
<td align="right">
<xsl:value-of select="format-number(number(sum($current-group/ItemSum/text())), '#.##')"/>
</td>
</tr>
</xsl:for-each>
</xsl:template>
https://xsltfiddle.liberty-development.net/3MvmXiv/3 and would output three tr
elements for your sample, just like the XSLT 2 approach https://xsltfiddle.liberty-development.net/3MvmXiv/0 https://xsltfiddle.liberty-development.net/3MvmXiv/3并将为您的示例输出三个tr
元素,就像 XSLT 2 方法https://xsltfiddle.liberty-development.net/3MvmXiv/0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.