XSLT 2.0 分组、求和、计数和条件 [英] XSLT 2.0 Grouping, Summing, Counting and Conditions
问题描述
我有 XSLT,它对输入进行分组,对当前组求和,如果总和满足某个条件,则仅输出一条记录.for-each-group 工作正常,但我无法获得预告片记录的准确记录数.
I have XSLT that is grouping the input, summing the current-group, and only outputting a record if the sum meets a certain condition. The for-each-group works fine, but I can't get an accurate record count for a trailer record.
这是一个示例输入:
<Book_Data>
<Book_Entry>
<Book>
<Book_ID>1</Book_ID>
<Book_Name>Test1</Book_Name>
</Book>
<Sales>
<Quantity>2</Quantity>
</Sales>
<Book_Entry>
<Book_Entry>
<Book>
<Book_ID>1</Book_ID>
<Book_Name>Test1</Book_Name>
</Book>
<Sales>
<Quantity>3</Quantity>
</Sales>
<Book_Entry>
<Book_Entry>
<Book>
<Book_ID>2</Book_ID>
<Book_Name>Test2</Book_Name>
</Book>
<Sales>
<Quantity>3</Quantity>
</Sales>
<Book_Entry>
<Book_Entry>
<Book>
<Book_ID>2</Book_ID>
<Book_Name>Test2</Book_Name>
</Book>
<Sales>
<Quantity>-3</Quantity>
</Sales>
<Book_Entry>
</Book_Data>
这是一个示例 XSLT (2.0):
And here is a sample XSLT (2.0):
<xsl:for-each-group select="Book_Data/Book_Entry" group-by="Book/Book_ID">
<xsl:if test="sum(current-group()/Sales/Quantity) != 0">
<xsl:value-of select="Book/Book_ID"/>
<xsl:value-of select="Book/Book_Name"/>
</xsl:if>
</xsl:for-each-group>
<xsl:value-of select="count(distinct-values(Book_Data/Book_Entry/Book/Book_ID[sum(../../Sales/Quantity) != 0]))"/>
问题是顶部正确地只输出了第一本书.但预告片计算了两本书,我不知道如何调整总和以在外部上下文中引用 Book_ID.
The problem is the top section correctly outputs only book 1. But the trailer counts both books, and I can't figure out how to condition the sum to reference a Book_ID in the outer context.
推荐答案
您可以将 for-each-group
的结果输出存储在一个变量中,然后计算组数,如果我明白什么你想要:
You could store the resulting output of the for-each-group
in a variable and then count the groups, if I understand what you want:
<xsl:variable name="groups" as="xs:string*">
<xsl:for-each-group select="Book_Data/Book_Entry" group-by="Book/Book_ID">
<xsl:if test="sum(current-group()/Sales/Quantity) != 0">
<xsl:sequence select="current-grouping-key()"/>
</xsl:if>
</xsl:for-each-group>
</xsl:variable>
<xsl:value-of select="$groups"/>
<xsl:value-of select="count($groups)"/>
这篇关于XSLT 2.0 分组、求和、计数和条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!