Muenchian分组 [英] muenchian grouping
问题描述
我想知道在谓词分组中,如何将该谓词([1])始终硬编码为1.经过大量搜索之后,这个概念对我而言并不明确.将其解释为当前节点,将其与密钥返回的第一组进行比较.
为什么总是与第一个匹配的键比较?
同样为什么我们还要再次给contact[count(. | key('contacts-by-surname', surname)[1]) = 1], the =1 part?
1进行硬编码.我引用了以下链接
I was wondering how this predicate([1]), is hardcoded as 1 always in the muenchian grouping. The concept was not clear for me, after a lot of search. It is explained as the current node, is compared with the 1st group returned by the key.
Why does it always compare with the first one that a key is matched?
Also why are we giving contact[count(. | key('contacts-by-surname', surname)[1]) = 1], the =1 part?
again 1 is hardcoded. I referred the below link
http://www.jenitennison.com/xslt/grouping/muenchian.html
推荐答案
我想知道如何将此谓词([1])始终硬编码为1 muenchian分组.
I was wondering how this predicate([1]), is hardcoded as 1 always in the muenchian grouping.
这很简单:
key()
函数会生成给定组的所有节点,而我们只希望从任何组中获取一个节点.
The key()
function produces all nodes for a given group, and we want to take just one node from any group.
不能保证所有组中都有两个或更多节点-有些可能只有一个节点.
It isn't guaranteed that all groups will have two or more nodes in them -- some might have just one node.
这就是为什么安全,方便地从每个组中提取第一个(可能是唯一的)节点.
This is why it is safe and convenient to take the first (and possibly the only) node from each group.
我们同样可以对每个组的 last 节点进行分组(但这会降低效率):
We could equally well do the grouping taking the last node from each group (but this will be less efficient):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kNumByMod3" match="num"
use=". mod 3"/>
<xsl:template match=
"num[generate-id()
=
generate-id(key('kNumByMod3', . mod 3)[last()])
]
">
3k + <xsl:value-of select=". mod 3"/>:
<xsl:text/>
<xsl:copy-of select="key('kNumByMod3', . mod 3)"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
应用于此XML文档:
<nums>
<num>01</num>
<num>02</num>
<num>03</num>
<num>04</num>
<num>05</num>
<num>06</num>
<num>07</num>
<num>08</num>
<num>09</num>
<num>10</num>
</nums>
产生想要的,正确分组的结果:
3k + 2:
<num>02</num>
<num>05</num>
<num>08</num>
3k + 0:
<num>03</num>
<num>06</num>
<num>09</num>
3k + 1:
<num>01</num>
<num>04</num>
<num>07</num>
<num>10</num>
这篇关于Muenchian分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!