Answer the question
In order to leave comments, you need to log in
How does the Munch group work?
Good afternoon!
Dear experts! He mastered the grouping by the Munch method. I understand that:
1) Keys (xsl:key) are used to select a set of nodes by their properties.
2) Using the generate-id function, you can find out if the node is the first node in the set in the order in which the xml document is viewed.
But I have a question. So what?
Here is a code snippet that works:
<xsl:key name="group" match="item" use="@group"/>
<xsl:template match="list">
<group-list>
<xsl:apply-templates
select="item[generate-id(.) = generate-id(key('group',@group))]" <!--Магия у нас тут-->
/>
</group-list>
</xsl:template>
<xsl:template match="item">
<group name="{@group}">
<xsl:for-each select="key('group',@group)">
<item name="{@id}"/>
</xsl:for-each>
</group>
</xsl:template>
Answer the question
In order to leave comments, you need to log in
XSL - key element
key can contain an element or a set of elements. You can get an element or a set of elements from a key using the key() function by the value passed as an argument.
https://www.w3schools.com/xml/ref_xsl_el_key.asp
The example above creates a key called group that contains a set of item (match="item") and this "item" or set of it can be retrieved by the value of the group attribute (use="@group").
In the example above, the "group" key contains a set of "items", which can be accessed by the value of the "group" attribute through the "key()" function.
Returns a string that uniquely identifies the first node in the document. If a set is passed to the function, then the unique id of the first element from this set will be returned
The generate-id function returns a unique string identifier for the first node in document lookup order that is passed to it as an argument. If the argument is omitted, the function returns the context node's unique ID. If the argument is an empty set, the function must return an empty string.
The generate-id function returns the same ID for two nodes if and only if the two nodes are the same. This means that during a single transformation, the generate-id function will return the same ID for the same node, and generate-id will necessarily return different IDs for different nodes.
The generate-id() function returns a string value that uniquely identifies a specified node.
If the node-set specified is empty, an empty string is returned. If you omit the node-set parameter, it defaults to the current node.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question