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
相关问题
- XML - XSLT - document() function inside count() fu
- Using XSLT to select after EACH instance in a stri
- XSLT open other xml files
- How to use MSbuild property in TransformXml task?
- Using asp objects in .NET - Max compatibility
相关文章
- xslt localization
- convert xml to soap request using xslt transformat
- XALAN register Extension Function like in SAXON
- How to group using XSLT
- How to reformat XML with group-adjacent (XSLT)
- AddExtensionObject - Performance
- Transforming HTML nodes with XSLT in chrome/webkit
- visual studio 2015 xsl debugging transformation fa
The basic algorithm is that there are two nested loops. The outer loop selects one representative node from each group, and the inner loop selects all the nodes in that group (including the one chosen as representative). The easiest way of selecting one representative node from a group is to select the first, hence the predicate
[1]
.This is simple:
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.
We could equally well do the grouping taking the last node from each group (but this will be less efficient):
when applied on this XML document:
produces the wanted, correctly grouped result:
Let's say we have a key definition
<xsl:key name="contacts-by-surname" match="contact" use="surname"/>
, then the expressionkey('contacts-by-surname', 'Doe')
gives you a node set with allcontact
elements where thesurname
isDoe
. The expressionkey('contacts-by-surname', 'Doe')[1]
gives you the firstcontact
in that "group".Now when processing all
contact
elements withfor-each
orapply-templates
we usually want a way to identify the firstcontact
element in each group. This can be achieved with<xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[1]) = 1]">
or<xsl:for-each select="contact[generate-id() = generate-id(key('contacts-by-surname', surname)[1])]">
.If your requirement is different and you for instance wanted to identify the last item in each group then you could of course use a different predicate, as in
<xsl:for-each select="contact[count(. | key('contacts-by-surname', surname)[last()]) = 1]">
or<xsl:for-each select="contact[generate-id() = generate-id(key('contacts-by-surname', surname)[last()])]">
.