XSLT Sort nested xml - xslt

I need to sort the below XML from SAP based on E1EDP01 - POSEX and ACTION, rest of the xml should be copied as it is. Please note that there's E1EDP19 under E1EDP01, which should be copied as it is as well. My XSLT is not working bcoz am not sure what should be the value for the template match. Please help.
<ORDERS05>
<IDOC BEGIN="1">
<E1EDK14 SEGMENT="1">
<QUALF>014</QUALF>
<ORGID>PF01</ORGID>
</E1EDK14>
<E1EDP01 SEGMENT="1">
<POSEX>00020</POSEX>
<ACTION>001</ACTION>
<PSTYP>0</PSTYP>
<MENGE>540.000</MENGE>
<MENEE>RO</MENEE>
<E1EDP19>
<Text> Test 2 </Text>
</E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>00030</POSEX>
<ACTION>001</ACTION>
<PSTYP>1</PSTYP>
<MENGE>140.000</MENGE>
<MENEE>AD</MENEE>
<E1EDP19>
<Text> Test 1 </Text>
</E1EDP19>
</E1EDP01>
<E1EDS01 SEGMENT="1">
<SUMID>002</SUMID>
<SUMME>832.2</SUMME>
<SUNIT>USD</SUNIT>
</E1EDS01>
</IDOC>
</ORDERS05>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<!-- -->
<xsl:strip-space elements="*"/>
<!-- -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="E1EDP01">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:apply-templates select="POSEX">
<xsl:sort select="POSEX" order="descending" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Expected Result
<ORDERS05>
<IDOC BEGIN="1">
<E1EDK14 SEGMENT="1">
<QUALF>014</QUALF>
<ORGID>PF01</ORGID>
</E1EDK14>
<E1EDP01 SEGMENT="1">
<POSEX>00030</POSEX>
<ACTION>001</ACTION>
<PSTYP>1</PSTYP>
<MENGE>140.000</MENGE>
<MENEE>AD</MENEE>
<E1EDP19>
<Text> Test 1 </Text>
</E1EDP19>
</E1EDP01>
<E1EDP01 SEGMENT="1">
<POSEX>00020</POSEX>
<ACTION>001</ACTION>
<PSTYP>0</PSTYP>
<MENGE>540.000</MENGE>
<MENEE>RO</MENEE>
<E1EDP19>
<Text> Test 2 </Text>
</E1EDP19>
</E1EDP01>
<E1EDS01 SEGMENT="1">
<SUMID>002</SUMID>
<SUMME>832.2</SUMME>
<SUNIT>USD</SUNIT>
</E1EDS01>
</IDOC>
</ORDERS05>

If you want to sort the E1EDP01 elements then by using xsl:sort in an xsl:apply-templates of a template processing the children of the parent element:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[E1EDP01]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="node()" group-adjacent="boolean(self::E1EDP01)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()">
<xsl:sort select="POSEX" order="descending"/>
<xsl:sort select="ACTION" order="descending"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
http://xsltransform.net/ncntCT9/1 has an online example.

Related

XSL to suppress xml declaration conditonally (fails)

I fail to suppress the xml declaration with my following xsl. I want a completely empty xml when OWNER=0000080070:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:choose>
<xsl:when test="//IDOC/ZSCWM_TU[OWNER='0000080070']">
</xsl:when>
<xsl:otherwise>
<xsl:text disable-output-escaping="yes"><?xml version="1.0" encoding="UTF-8"?>
</xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:apply-templates/>
</xsl:template>
<!-- Special template -->
<xsl:template match="/*[//IDOC/ZSCWM_TU[OWNER='0000080070']]">
</xsl:template>
<!-- Default Template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Input xml:
<?xml version="1.0" encoding="UTF-8"?>
<ZSCWM_-TU_LOADLIST>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
</EDI_DC40>
<ZSCWM_TU SEGMENT="1">
<OWNER>0000080070</OWNER>
</ZSCWM_TU>
</IDOC>
</ZSCWM_-TU_LOADLIST>
Current output:
<?xml version="1.0" encoding="UTF-8"?>
Required output:
BLANK XML
I must be making a stupid mistake :(
Kind regards,
Mike
If you're using an XSLT 2.0 processor such as Saxon then I would recommend
<xsl:template match="/">
<xsl:result-document omit-xml-declaration=
"{if (//IDOC/ZSCWM_TU[OWNER='0000080070']) then 'yes' else 'no'}">
<xsl:apply-templates/>
</xsl:result-document>
</xsl:template>

Keep main element after sorting with xsl

I have a source xml:
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<SHPMNT05>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
</EDI_DC40>
<E1EDT20 SEGMENT="1">
<TKNUM>0000046741</TKNUM>
<E1EDL20 SEGMENT="1">
<VBELN>1</VBELN>
<VKORG>0200</VKORG>
</E1EDL20>
<E1EDL20 SEGMENT="1">
<VBELN>1</VBELN>
<VKORG>0100</VKORG>
</E1EDL20>
</E1EDT20>
</IDOC>
</SHPMNT05>
</ns0:Message1>
</ns0:Messages>
I'm sorting all E1EDL20 elements with xslt:
<xsl:template match="E1EDT20">
<xsl:for-each select="*[not(self::E1EDL20)]">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:for-each select="E1EDL20">
<xsl:sort select="VKORG"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:template>
<!-- Default Template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
But in my result I'm missing the parent E1EDT20 element:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<SHPMNT05>
<IDOC BEGIN="1">
<EDI_DC40 SEGMENT="1">
<TABNAM>EDI_DC40</TABNAM>
</EDI_DC40>
<TKNUM>0000046741</TKNUM>
<E1EDL20 SEGMENT="1">
<VBELN>1</VBELN>
<VKORG>0100</VKORG>
</E1EDL20>
<E1EDL20 SEGMENT="1">
<VBELN>1</VBELN>
<VKORG>0200</VKORG>
</E1EDL20>
</IDOC>
</SHPMNT05>
</ns0:Message1>
</ns0:Messages>
What do I need to put after
<xsl:template match="E1EDT20">
to keep the element / tag in the output?
Very sorry to bother you with this beginners question but every so often I have to use xslt and I can't find the answer after search for quite some time...
Mike
In your template which matches E1EDT20 you don't copy the element; you only copy its children.
You should copy the element with xsl:copy, and within that, copy its attributes, and then process the child elements:
<xsl:template match="E1EDT20">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:for-each select="*[not(self::E1EDL20)]">
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:for-each select="E1EDL20">
<xsl:sort select="VKORG"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
Wrap the content of your template in an xsl:copy instruction. You can also shorten it to:
<xsl:template match="E1EDT20">
<xsl:copy>
<xsl:copy-of select="#* | *[not(self::E1EDL20)]"/>
<xsl:for-each select="E1EDL20">
<xsl:sort select="VKORG"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>

XSLT 2.0 - passing a node set as a parameter doesn't seem to work

I have a document that I need to transform such that most elements are copied as-is, with some exceptions: for certain specified nodes, child elements need to be appended, and some of these child elements need to reference back to specific elements in the source document. A separate "model/crosswalk" xml file contains the elements to add. The elements in the crosswalk that need to refer back to the source document have "source" attributes that point them to specific elements. Of course, my actual source docs (& the actual crosswalk) are more complex and varied than these examples.
Here is a source document example:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<album>
<artist>Frank Sinatra</artist>
<title>Greatest Hits</title>
</album>
<album>
<artist>Miles Davis</artist>
<title>Kind Of Blue</title>
</album>
<movie>
<title>ET</title>
<director>Steven Spielberg</director>
</movie>
<movie>
<title>Blues Brothers</title>
<director>John Landis</director>
</movie>
</root>
Here is the "crosswalk" (crswlk.xml):
<?xml version="1.0" encoding="UTF-8"?>
<root>
<album>
<artist-info>
<artist2 source="artist"/>
</artist-info>
</album>
<movie>
<director-info>
<director2 source="director"/>
</director-info>
</movie>
</root>
And here is the desired output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<album>
<artist>Frank Sinatra</artist>
<title>Greatest Hits</title>
<artist-info>
<artist2>Frank Sinatra</artist2>
</artist-info>
</album>
<album>
<artist>Miles Davis</artist>
<title>Kind Of Blue</title>
<artist-info>
<artist2>Miles Davis</artist2>
</artist-info>
</album>
<movie>
<title>ET</title>
<director>Steven Spielberg</director>
<director-info>
<director2>Steven Spielberg</director2>
</director-info>
</movie>
<movie>
<title>Blues Brothers</title>
<director>John Landis</director>
<director-info>
<director2>John Landis</director2>
</director-info>
</movie>
</root>
Here's my xslt:
<?xml version="1.0" encoding="UTF-8"?>
<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:strip-space elements="*"/>
<xsl:variable name="crosswalk" select="document('crswlk.xml')"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/album|movie">
<xsl:variable name="theNode" select="."/>
<xsl:variable name="nodeName" select="name()"/>
<xsl:element name="{$nodeName}">
<xsl:apply-templates select="#* | node()"/>
<xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
<xsl:with-param name="curNode" select="$theNode"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="*[#source]">
<xsl:param name="curNode" />
<xsl:variable name="sourceNodeName" select="#source"/>
<xsl:element name="{name()}">
<xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
This stops processing once that last template tries to access $curNode for the first time. When I run the xslt in Eclipse (Xalan 2.7.1) it throws this error: " java.lang.ClassCastException: org.apache.xpath.objects.XString cannot be cast to org.apache.xpath.objects.XNodeSet".
If I pass a similar nodeset as a parameter to a template that matches nodes from the source document, it works as expected - the nodeset is accessible. However, passing the nodeset to the last template above doesn't work. Is it because the template matches nodes from the external document? I sure don't know. Any help much appreciated, it took me a while just to get to this point. Thanks!
Looks like you need to change 2 things:
add tunnel="yes" to xsl:with-param and xsl:param
remove the apostrophes from '$sourceNodeName' in the predicate of the xsl:value-of
Updated XSLT:
<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:strip-space elements="*"/>
<xsl:variable name="crosswalk" select="document('crswlk.xml')"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*/album|movie">
<xsl:variable name="theNode" select="."/>
<xsl:variable name="nodeName" select="name()"/>
<xsl:element name="{$nodeName}">
<xsl:apply-templates select="#* | node()"/>
<xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
<xsl:with-param name="curNode" select="$theNode" tunnel="yes"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="*[#source]">
<xsl:param name="curNode" tunnel="yes"/>
<xsl:variable name="sourceNodeName" select="#source"/>
<xsl:element name="{name()}">
<xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Also, you can remove a few of those extra xsl:variables...
<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:strip-space elements="*"/>
<xsl:variable name="crosswalk" select="document('crswlk.xml')"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="album|movie">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
<xsl:apply-templates select="$crosswalk/*/*[name()=current()/name()]/*">
<xsl:with-param name="curNode" select="." tunnel="yes"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*[#source]">
<xsl:param name="curNode" tunnel="yes"/>
<xsl:copy>
<xsl:value-of select="$curNode//*[name()=current()/#source]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
If you are using XSLT 2.0, then Daniel Haley's answer with tunnelling is surely the way to go. If, however, you are actually using Xalan, and therefore only XSLT 1.0, you need to take a different approach.
The problems start on this line:
<xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
This will select either artist-info or director-info in your cross walk document, but you have no specific template matching these, so the generic identity template you are using will match them
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
But this does not take any parameter, not does it pass any parameters on. Therefore, when your last template <xsl:template match="*[#source]"> is matched, the curNode will be empty (an empty string), which is not a node-set, and that saddens Xalan.
So, to solve this in XSLT1.0, just add the parameter to the identity template, and pass it on:
<xsl:template match="node()|#*">
<xsl:param name="curNode" />
<xsl:copy>
<xsl:apply-templates select="#* | node()">
<xsl:with-param name="curNode" select="$curNode" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
If the template is ever matched when there is no parameter being passed, it will just pass on an empty parameter without any issue.
Here is the full XSLT (also with the correction of apostrophes being removed from the xsl:value-of, as mentioned in Daniel's answer).
<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:strip-space elements="*"/>
<xsl:variable name="crosswalk" select="document('crswlk.xml')"/>
<xsl:template match="node()|#*">
<xsl:param name="curNode" />
<xsl:copy>
<xsl:apply-templates select="#* | node()">
<xsl:with-param name="curNode" select="$curNode" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="*/album|movie">
<xsl:variable name="theNode" select="."/>
<xsl:variable name="nodeName" select="name()"/>
<xsl:element name="{$nodeName}">
<xsl:apply-templates select="#* | node()"/>
<xsl:apply-templates select="$crosswalk//*[name()=$nodeName]/*">
<xsl:with-param name="curNode" select="$theNode"/>
</xsl:apply-templates>
</xsl:element>
</xsl:template>
<xsl:template match="*[#source]">
<xsl:param name="curNode" />
<xsl:variable name="sourceNodeName" select="#source"/>
<xsl:element name="{name()}">
<xsl:value-of select="$curNode//*[name()=$sourceNodeName]"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When applied to your XML documents, the following is output
<root>
<album>
<artist>Frank Sinatra</artist>
<title>Greatest Hits</title>
<artist-info>
<artist2>Frank Sinatra</artist2>
</artist-info>
</album>
<album>
<artist>Miles Davis</artist>
<title>Kind Of Blue</title>
<artist-info>
<artist2>Miles Davis</artist2>
</artist-info>
</album>
<movie>
<title>ET</title>
<director>Steven Spielberg</director>
<director-info>
<director2>Steven Spielberg</director2>
</director-info>
</movie>
<movie>
<title>Blues Brothers</title>
<director>John Landis</director>
<director-info>
<director2>John Landis</director2>
</director-info>
</movie>
</root>

Transform Exception. Adding Attribute after Sub-element

I have this XML:
<root>
<tab name="Detail">
<section name="mysection">
<items level="1">
<Idx_name>9</Idx_name>
<Type>mytype</Type>
<item name="myname">
<Grams um="(g)">9,0</Grams>
<Pre-infusion>Max</Pre-infusion>
</item>
<Std._Mode>On</Std._Mode>
<Price>100</Price>
</items>
</section>
</tab>
</root>
and this XSLT:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items/*">
<xsl:choose>
<xsl:when test="not(name()='item')">
<xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:copy-of select="#*"/>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Now, what I want is:
<root>
<tab name="Detail">
<section name="mysection">
<items level="1" Idx_name="9" Type="mytype" Std._Mode="On" Price="100">
<item name="myname">9,0Max</item>
</items>
</section>
</tab>
</root>
I obtain the error: "An attribute cannot be added after a child"
Unluckily, I can not change the order of elements in the node items of my original XML
How can I do it ?
Thanks
Ivan
Make sure you process the elements first you want to transform into attributes e.g. with XSLT 2.0 where you can process sequences which have a order you can simply do
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items">
<xsl:copy>
<xsl:apply-templates select="#*, * except item, item"/>
</xsl:copy>
</xsl:template>
<xsl:template match="items/*[not(self::item)]">
<xsl:attribute name="{name()}" select="."/>
</xsl:template>
<xsl:template match="items/item">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:value-of select="."/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
With XSLT 1.0 you would need to spell out several apply-templates in the order you want.

Joining data of the same type with XSLT

I would like to join all data of the same type with XSLT. I have the following XML:
<ZE1MARAM>
<ZE1KONDM SEGMENT="1">
<VKORG>NL01</VKORG>
<KONDART>VKP0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NL01</KONDWERT>
<MENGE> 70.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
<ZE1KONDM SEGMENT="1">
<VKORG>NLWS</VKORG>
<KONDART>VKP0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NLWS</KONDWERT>
<MENGE> 70.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
<ZE1KONDM SEGMENT="1">
<VKORG>NLWS</VKORG>
<KONDART>VKA0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NLWS</KONDWERT>
<MENGE> 33.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
</ZE1MARAM>
so each ZE1KONDM with the same VKORG value in the result xml has to be appended to the same element. So the result would be something like that:
<result>
<prices value="NL01">
<price type="VKP0">
70.00
</price>
</prices>
<prices value="NLWS">
<price type="VKP0">
70.00
</price>
<price type="VKA0">
55.00
</price>
</prices>
I tried to work with keys, and do something like that:
<xsl:key name="myKey" match="ZE1KONDM" use="normalize-space(VKORG)" />
<xsl:for-each select="ZE1KONDM">
<xsl:choose>
<xsl:when test="KONDART='VKP0'">
<xsl:element name="prices">
<xsl:element name="price">
<xsl:value-of select="key('myKey', normalize-space(VKORG))/MENGE"/>
</xsl:element>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:for-each>
but it does not work because it takes just one key..
There is some way to solve this problem with xslt?
There's probably a better way, but try this:
http://www.xmlplayground.com/2A3C7H
(see output source)
I. XSLT 1.0 Solution:
Here is a classical application of the Muenchian grouping method:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kZByM" match="ZE1KONDM" use="VKORG"/>
<xsl:template match="/*">
<result>
<xsl:apply-templates select=
"*[generate-id() = generate-id(key('kZByM', VKORG)[1])]"/>
</result>
</xsl:template>
<xsl:template match="ZE1KONDM">
<prices value="{VKORG}">
<xsl:apply-templates select="key('kZByM', VKORG)" mode="inGroup"/>
</prices>
</xsl:template>
<xsl:template match="ZE1KONDM" mode="inGroup">
<price type="{KONDART}">
<xsl:value-of select="MENGE"/>
</price>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<ZE1MARAM>
<ZE1KONDM SEGMENT="1">
<VKORG>NL01</VKORG>
<KONDART>VKP0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NL01</KONDWERT>
<MENGE>70.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
<ZE1KONDM SEGMENT="1">
<VKORG>NLWS</VKORG>
<KONDART>VKP0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NLWS</KONDWERT>
<MENGE>70.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
<ZE1KONDM SEGMENT="1">
<VKORG>NLWS</VKORG>
<KONDART>VKA0</KONDART>
<BEGINDATUM>99991231</BEGINDATUM>
<ENDDATUM>20120605</ENDDATUM>
<KONDWERT>NLWS</KONDWERT>
<MENGE>33.00</MENGE>
<CURRENCY>EUR</CURRENCY>
</ZE1KONDM>
</ZE1MARAM>
the wanted, correct result is produced:
<result>
<prices value="NL01">
<price type="VKP0">70.00</price>
</prices>
<prices value="NLWS">
<price type="VKP0">70.00</price>
<price type="VKA0">33.00</price>
</prices>
</result>
Do note: The Muenchian grouping method is probably the fastest XSLT 1.0 grouping method, because it uses keys. Other methods (such as comparing siblings values) are way too slower (O(N^2)) which is prohibitive fro using them on large data sizes.
II. XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<result>
<xsl:for-each-group select="*" group-by="VKORG">
<prices value="{VKORG}">
<xsl:apply-templates select="current-group()"/>
</prices>
</xsl:for-each-group>
</result>
</xsl:template>
<xsl:template match="ZE1KONDM">
<price type="{KONDART}">
<xsl:value-of select="MENGE"/>
</price>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on the same XML document (above), the same correct result is produced:
<result>
<prices value="NL01">
<price type="VKP0">70.00</price>
</prices>
<prices value="NLWS">
<price type="VKP0">70.00</price>
<price type="VKA0">33.00</price>
</prices>
</result>
Explanation:
Proper use of xsl:for-each-group with the group-by attribute, and the current-group() function.