I have below xml as input and from that, I am looping the segment for-each. From the below data I need the count the number of the same Element-234_8 when Element-235_8= "IK" in the whole message.
So the count would be 5. When I tried count(Loop-HL/Segment-SLN[Element-235_8='IK']/Element-234_8) it's giving 15. So what other functions i need to use it in XSLT 2.0
for example, If I search the value 400200167700010191932355211 from the entire message it's repeated 5 times. So I am trying to get that value count how many times it's reapeataed in whole Loop-HL
<Loop-HL>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8> 25th
<Element-234_8>400200167700010191932355211</Element-234_8>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355334</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355440</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
</Loop-HL>
<Loop-HL>
<Segment-SLN>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355211</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355334</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355440</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
</Loop-HL>
<Loop-HL>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355211</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355334</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355440</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
</Loop-HL>
<Loop-HL>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355211</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355334</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355440</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
</Loop-HL>
<Loop-HL>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355211</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355334</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
<Segment-SLN>
<Element-234_7>1</Element-234_7>
<Element-235_8>IK</Element-235_8>
<Element-234_8>400200167700010191932355440</Element-234_8>
<Element-235_9>NW</Element-235_9>
<Element-234_9>1.69</Element-234_9>
</Segment-SLN>
</Loop-HL>
CodePudding user response:
Perhaps you want
count(distinct-values(Loop-HL/Segment-SLN[Element-235_8='IK']/Element-234_8))
But it's hard to tell from your description.
You're more likely to get an answer to this kind of question if you simplify it by cutting out extraneous detail: simplify the element names and values, omit those that play no part, get the XML to fit on one page without scrolling.
In some cases, simplifying the problem to explain it to others also means that the answer leaps out at you...
CodePudding user response:
To count the number of occurrences of each distinct value of Element-234_8
you can do:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/*">
<output>
<xsl:for-each-group select="Loop-HL/Segment-SLN[Element-235_8='IK']" group-by="Element-234_8">
<grp key="{current-grouping-key()}">
<xsl:value-of select="count(current-group())"/>
</grp>
</xsl:for-each-group>
</output>
</xsl:template>
</xsl:stylesheet>
When applied to your example input (after adding the missing root element!) this will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<grp key="400200167700010191932355211">5</grp>
<grp key="400200167700010191932355334">5</grp>
<grp key="400200167700010191932355440">5</grp>
</output>