Questions tagged [xslt-grouping]

Grouping mechanisms that are specific to XSLT. Should always be complemented with either the xslt-1.0, xslt-2.0 or xslt-3.0 tag to indicate the version used.

Grouping in XSLT 3

Grouping in XSLT 3, the current and latest version of XSLT since 2017, can be achieved primarily using the xsl:for-each-group instruction where you select the items to be grouped, the grouping population, with the select attribute and then have three different ways to group the population:

  1. the group-by attribute selecting a sequence of grouping keys by which the grouping population is to be grouped
  2. the group-adjacent attribute selecting a sequence of grouping keys by which adjacent items in the population are to be grouped
  3. the group-starting-with or group-ending-with attributes defining patterns to identify and start groups based on pattern matching of the initial (group-starting-with) or final (group-ending-with) member of a group

Access to the items of a currently processed group is given by the current-group() function, access to the current grouping key by the current-grouping-key() function.

The XSLT 3 specification gives at least one example for each grouping approach directly in the specification section https://www.w3.org/TR/xslt-30/#grouping-examples; below you find links to XSLT fiddles based on these examples:

Of course the different approaches can, for more complex tasks, be combined by nesting xsl:for-each-group instructions.

XSLT 3 with XPath 3.1 support can also group JSON represented as XPath 3.1 maps and arrays, here is a list of showing the previous XML grouping samples now using JSON input and directly grouping the map/array data structure returned by the parse-json function:

Positional grouping

Positional grouping can be used to split a sequence of items into groups of a certain size; in XSLT 2 or 3 this is easily achieved using e.g. for-each-group select="foo" group-adjacent="(position() - 1) idiv $chunk-size": https://xsltfiddle.liberty-development.net/asoTK9

https://martin-honnen.github.io/xslt/generic-positional-grouping-functions.xsl is a compact XSLT 3 module using higher-order functions importable by other code that needs to split up a sequence of items using positional grouping, you could use it as follows (Saxon-JS 2, Saxon 10 or higher all editions, Saxon 9.8 or higher PE and EE, Altova XML 2017 R3 or later)

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:mf="http://example.com/mf"
    xmlns:ex="http://example.com/ex"
    exclude-result-prefixes="#all"
    expand-text="yes">

  <xsl:import href="https://martin-honnen.github.io/xslt/generic-positional-grouping-functions.xsl"/>

  <xsl:function name="ex:wrap-rows" as="element()">
    <xsl:param name="group" as="item()*"/>
    <xsl:param name="pos" as="xs:integer"/>
    <xsl:param name="wrapper-name" as="xs:QName"/>
    <xsl:element name="{$wrapper-name}" namespace="{namespace-uri-from-QName($wrapper-name)}">
      <xsl:attribute name="index" select="$pos"/>
      <xsl:sequence select="$group"/>
    </xsl:element>
  </xsl:function>

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="root">
    <root>
      <xsl:sequence select="mf:group-chunks(item, 3, ex:wrap-rows(?, ?, QName('', 'chunk')))"/>
    </root>
  </xsl:template>

  <xsl:template match="/">
    <xsl:next-match/>
    <xsl:comment>Run with {system-property('xsl:product-name')} {system-property('xsl:product-version')} {system-property('Q{http://saxon.sf.net/}platform')}</xsl:comment>
  </xsl:template>

</xsl:stylesheet>

to e.g. wrap any chunk into a container element.

Grouping in XSLT 2

Grouping in XSLT 2 also works with the xsl:for-each-group instruction as in XSLT 3, the main restrictions are that XSLT 2 does not support composite grouping keys (so to group on various items you need to concat or string-join them as single value (e.g. composite="yes" group-by="foo, bar" in XSLT 3 needs to be done as group-by="string-join((foo, bar), '|')")) and that pattern matching can only be done on nodes, not on primitive values.

The XSLT 2 specification gives at least one example for each grouping approach directly in the specification section https://www.w3.org/TR/xslt20/#grouping-examples; below you find links to XSLT fiddles based on these examples:

Grouping in XSLT 1.0

The preferred solution in XSLT 1.0 is to use the Muenchian grouping method:
http://www.jenitennison.com/xslt/grouping/muenchian.html

552 questions
0
votes
1 answer

XSLT 1.0: sort unique values with key but with exceptions

I have the following simplified XML structure:
Peter
  • 1,786
  • 4
  • 21
  • 40
0
votes
1 answer

How keys work in muenchian grouping

I haven't found an answer yet on how keys are generated while doing muenchian-grouping in xslt. The examples I found all have very simple xml files. Given the following xml-file:
erik
  • 509
  • 6
  • 16
0
votes
1 answer

XSLT Child Nodes Copy from parent node

I want to copy nodes from parent to the child. I am not really sure how this can be acheived. My source xml Robin1
KRP
  • 131
  • 1
  • 3
  • 15
0
votes
1 answer

Copy part of parent nodes to child node XSLT

I want to copy nodes from parent to the child. I am not really sure how this can be acheived. My source xml Robin1
KRP
  • 131
  • 1
  • 3
  • 15
0
votes
1 answer

xslt multilevel grouping - convert from one xml format to other

I am facing problem with my xslt transformation. Where the source xml has multilevel nodes. I want to transform xml from one form to other Here is my xslt
KRP
  • 131
  • 1
  • 3
  • 15
-1
votes
1 answer

XSLT grouping creation for sequence of elements with specfied condition

I'm new for xslt i need to transform the xml to xml with grouping My input is here below in that i need to group introduction related information that all are have to group with box element, and i have list blocks as para with specified a attribute…
-1
votes
1 answer

XSLT XML to CSV looping through tags and set a variable to true of a condition is met

I am looking to write a xslt where it ierates through dates and sets ispast to true in csv if any one value is true xmls looks like below 2022
anudeep
  • 9
  • 3
-1
votes
1 answer

Can someone assist me in converting this from XSLT-2.0 to XSLT-1.0? *Update*, attempted to create Muenchian Grouping XSLT-1.0

As a preface, I have very little knowledge of XSLT-1.0. I feel as though what I am trying to accomplish is beyond that of a beginner. Through other members of the community here, I have been able to come up with the following…
Justin W
  • 5
  • 4
-1
votes
2 answers

XSLT mapping to remove double quotes which has PIPE delimited symbol inside

Experts, i need to write XSLT 1.0 code to eliminate the Pipe delimited symbol inside double quotes and also need to remove those double quotes.. Input:
Sree
  • 17
  • 7
-1
votes
1 answer

duplicate consideration from source through xslt

input data: below is the input data getting from the source. how we can achieve through XSLT mapping SCPI, Thanks in advance, and looking forward to your response. I have the following problem (which is quite often discussed in this domain but still…
-1
votes
1 answer

XSLT1.0: Need to eliminate records which is having empty fields

Experts, i need to write XSLT 1.0 code to eliminate the Record in the XML which is having empty fields. Input:
Sree
  • 17
  • 7
-1
votes
3 answers

XSLT 1.0 : Need to group the elements which is having the Parent ID

i need to relate Parent/sscc with its Logistic/sscc.we need to check same Parent/sscc in the input file & need to relate that with its logistic/sscc.As a result output must be logistic/sscc with its parent/sscc. i provided input and output details.…
Sree
  • 17
  • 7
-1
votes
1 answer

How to group nearby same elements using XSLT version 1

I need to transform an XML structure to another XML structure by grouping nearby elements using XSLT version 1 and store into a variable for later process. My current solution: https://xsltfiddle.liberty-development.net/naZXVF1/3 XML
user1219310
  • 732
  • 1
  • 9
  • 23
-1
votes
1 answer

First row for the Report_Entry(worker) needs to be defaulted to "YES". All other rows for worker to return "NO"

I'm working on a data extraction program to pull phone numbers with types including Work mobile, Work Telephone, Home Mobile and Home Telephone. If there are duplicate usage types, I'm picking the first instance. I'm needing help setting a flag for…
-1
votes
1 answer

Merge two XMLs with common fields and adding fields from both xmls using XSLT

I have two xmls a.xml b.xml Below is the a.xml file Username ID Ocean Urban
2437850
  • 287
  • 2
  • 10