D
D
Denis2015-07-29 09:33:43
XSL & XSLT
Denis, 2015-07-29 09:33:43

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>

I've read articles that describe this method, but still don't fully understand.
Maybe someone can tell more?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Sirotenko, 2017-04-21
@darkmayers

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.

https://xsltdev.ru/xpath/generate-id/
https://www.w3schools.com/xml/func_generateid.asp
is possible to limit the number of elements that match a pattern by entering a filter - an expression, enclosed in square brackets and immediately following the path operator.
This line
we say the following:
Apply all patterns to those item elements that match the criteria (filter) given in square brackets
In this part
we say the following:
Give me a set of "item" elements from the key "group" whose value of the attribute "group" equals the value of the "group" attribute of the "item" in the XML document
Let's move on. With this line
We say the following
Returning a set of item with the same value of the "group" attribute, give me the id of the first element from the set With
this line
We say
the following Apply all wildcards to Items whose generate-id equals the unique generate-id of the element.
As a result, it turns out that the templates will only be applied to "item" elements with a unique "group" attribute.
More details with examples:
geekspace.info/notes/1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question