I am facing Issue while sorting 3 different items separately, i tried to explain with example.
Node/ITEM Sorting: Case 1: (If ITEM has one line number only): we need check if this case exist, then need to check if Node/ITEM/LINE/FIER/VALUE where Key=1 is repeating in another Node/ITEM/LINE/FIER/Value where Type=Key1,if its repeating then based on ITEM/LINE/FIER/VALUE (Type=Key2) and ITEM/LINE/EXTEN/BNUMBER we need to perform sorting. in my example first 3 ITEMs will has this.
Node/ITEM Sorting Case 2: (If ITEM has two or more Line numbers): condition to sort is same as above, we need to perform sorting based on similar fields, in my example 4th ITEM will has this
Node/LINE Sorting: Same as above, if Node/LINE exists then based on Node/LINE/FIER/VALUE and /EXTEN/BNUMBER, we can do sorting.
my XSLT is giving exact results, but because of 2nd condition Node/ITEM Sorting (If ITEM has two or more Line numbers), its giving me error as A sequence of more than one item is not allowed as the first argument of fn:concat() (<BNUMBER>, <BNUMBER>)
how can i resolve this 2nd case sorting issue.
Input:
<SBD>
<DOCUMENT>
<Field1> value1</Field1>
</DOCUMENT>
<Node documentStatus="True" date="2024-09-27">
<ship>123</ship>
<field2>345r</field2>
<ITEM>
<IDENT>
<CODE>
<Value>67896789</Value>
</CODE>
</IDENT>
<LINE number="1">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>555</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>XYZ</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>3243</Value>
</CODE>
</IDENT>
<LINE number="2">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>BBBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<Segment1>
<subsegment>
<value>123</value>
</subsegment>
</Segment1>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>BBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="4">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<LINE number="1">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>6382</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>REA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="2">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>5678</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>CBA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>5678</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>BCA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<rdtime>123</rdtime>
</Node>
</SBD>
Desired output:
<?xml version="1.0" encoding="UTF-8"?>
<SBD>
<DOCUMENT>
<Field1> value1</Field1>
</DOCUMENT>
<Node documentStatus="True" date="2024-09-27">
<ship>123</ship>
<field2>345r</field2>
<ITEM>
<IDENT>
<CODE>
<Value>67896789</Value>
</CODE>
</IDENT>
<LINE number="1">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>555</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>XYZ</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>3243</Value>
</CODE>
</IDENT>
<LINE number="2">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>BBBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<Segment1>
<subsegment>
<value>123</value>
</subsegment>
</Segment1>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>BBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<LINE number="1">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>6382</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>REA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>5678</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>BCA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="2">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>5678</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>CBA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<rdtime>123</rdtime>
</Node>
</SBD>
XSLT I used is below:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="3.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all">
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="Node">
<xsl:copy>
<xsl:apply-templates select="@*, * except (ITEM, LINE, rdtime)"/>
<xsl:for-each-group select="ITEM" group-by="LINE/CIDENT/FIER[TYPE='Key1']/VALUE">
<xsl:sequence select="sort(current-group(), (), function($l) {
concat(
$l/LINE/EXTEN/BNUMBER,
'_',
$l/LINE/CIDENT/FIER[TYPE='Key2']/VALUE
)
})" />
</xsl:for-each-group>
<xsl:for-each-group select="LINE" group-by="CIDENT/FIER[TYPE='Key1']/VALUE">
<xsl:sequence select="sort(current-group(), (), function($l) {
concat(
$l/EXTEN/BNUMBER,
'_',
$l/CIDENT/FIER[TYPE='Key2']/VALUE
)
})" />
</xsl:for-each-group>
<xsl:apply-templates select="rdtime"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Please review it once
Technically, what you can do is use a sequence of sort keys generated by the third argument of the sort
function e.g.
sort(current-group(), (), function($l) {
for-each-pair(
$l/LINE/EXTEN/BNUMBER,
$l/LINE/CIDENT/FIER[TYPE='Key2']/VALUE,
function($b, $v) { $b, $v }
)
})
e.g. in context
<xsl:for-each-group select="ITEM" group-by="LINE/CIDENT/FIER[TYPE='Key1']/VALUE">
<xsl:sequence
select="sort(current-group(), (), function($l) {
for-each-pair(
$l/LINE/EXTEN/BNUMBER,
$l/LINE/CIDENT/FIER[TYPE='Key2']/VALUE,
function($b, $v) { $b, $v }
)
})
"/>
</xsl:for-each-group>
which outputs the ITEM
s as e.g.
<ITEM>
<IDENT>
<CODE>
<Value>67896789</Value>
</CODE>
</IDENT>
<LINE number="1">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>555</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>XYZ</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>0870</Value>
</CODE>
</IDENT>
<LINE number="3">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>BBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<LINE number="4">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>No</VALUE>
<TYPE>Key3</TYPE>
</FIER>
<FIER>
<VALUE>AAA</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
</ITEM>
<ITEM>
<IDENT>
<CODE>
<Value>3243</Value>
</CODE>
</IDENT>
<LINE number="2">
<CIDENT>
<name>12</name>
<FIER>
<VALUE>444</VALUE>
<TYPE>Key1</TYPE>
</FIER>
<FIER>
<VALUE>BBBB</VALUE>
<TYPE>Key2</TYPE>
</FIER>
</CIDENT>
<EXTEN>
<BNUMBER>S123</BNUMBER>
<Date>2025-08-15</Date>
</EXTEN>
</LINE>
<Segment1>
<subsegment>
<value>123</value>
</subsegment>
</Segment1>
</ITEM>
That is mainly meant as an example to avoid the error on the one hand and on the other to find some way to compare pair wise. I am not sure that is what you want and I have not managed to identify from your explanation or your desired output what the sort keys might be or whether you perhaps want to do some nesting grouping to eliminate duplicates.