My requirement is to move the sibling E_record fields into the L_Records. Could you please check where i am missing the logic. The XSLT code is pushing all the E_record fields into every L_Record. Please check the second root 2nd L_Record.
I am using the following input XML.
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</E_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
</root>
The output i am receiving is
<root>
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</L_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
</root>
The expected output is
<root>
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
<Record>
<H_Record>
<Rec_Type>H</Rec_Type>
</H_Record>
<C_Record>
<Rec_Type>C</Rec_Type>
</C_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
<Rec_Type>E</Rec_Type>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</L_Record>
<R_Record>
<Rec_Type>R</Rec_Type>
</R_Record>
</Record>
</root>
Here is the XSLT code executed.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="L_Record">
<xsl:copy>
<xsl:apply-templates/>
<xsl:if test="following-sibling::*[1][self::E_Record]">
<xsl:call-template name="Next_E_Record">
<xsl:with-param name="next" select="following-sibling::*[self::E_Record]/*"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:copy>
</xsl:template>
<xsl:template name="Next_E_Record">
<xsl:param name="next"/>
<xsl:copy-of select="$next"/>
<xsl:if test="$next/following-sibling::*[1][self::E_Record]">
<xsl:call-template name="Next_E_Record">
<xsl:with-param name="next" select="$next/following-sibling::*[self::E_Record]/*"></xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="E_Record"/>
</xsl:stylesheet>
enter code here
You could use this solution :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Replace first E_Record in a Record -->
<xsl:template match="E_Record[position()=1]">
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<!-- Copy all contents of E_Record siblings -->
<xsl:copy-of select="../E_Record/*"/>
</L_Record>
</xsl:template>
<!-- Eliminate all other E_Record -->
<xsl:template match="E_Record"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here: https://xsltfiddle.liberty-development.net/6pS26n8
Updated version:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<!-- Replace E_Record immediately following an L_Record -->
<xsl:template match="E_Record[name(preceding-sibling::*[1])='L_Record']">
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<!-- Copy the contents of the current E_Record -->
<xsl:copy-of select="*"/>
<!-- Call template to copy next E_Record -->
<xsl:call-template name="copyNextE">
<xsl:with-param name="next" select="following-sibling::*[1]"/>
</xsl:call-template>
</L_Record>
</xsl:template>
<!-- Recursive template, will call itself until the next sibling is not an E_Record -->
<xsl:template name="copyNextE">
<xsl:param name="next"/>
<!-- If we are still dealing with an E_Record -->
<xsl:if test="name($next)='E_Record'">
<xsl:copy-of select="$next/*"/>
<xsl:call-template name="copyNextE">
<!-- Call the same template with the next sibling -->
<xsl:with-param name="next" select="$next/following-sibling::*[1]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<!-- Eliminate all other E_Record -->
<xsl:template match="E_Record"/>
<!-- Identity template -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here: https://xsltfiddle.liberty-development.net/6pS26n8/1
Related
I have an XML input which is merged format of two xmls:
<DATA>
<RECORDS1>
<RECORD>
<id>11</id>
<value>123</value>
</RECORD>
<RECORD>
<id>33</id>
<value>321</value>
</RECORD>
<RECORD>
<id>55</id>
<value>121113</value>
</RECORD>
...
</RECORDS1>
<RECORDS2>
<RECORD>
<id>11</id>
<value>123</value>
</RECORD>
<RECORD>
<id>33</id>
<value>323</value>
</RECORD>
<RECORD>
<id>44</id>
<value>12333</value>
</RECORD>
...
</RECORDS2>
I need to copy in the output the records in RECORDS1 provided:
The records in RECORDS1 doesnot exist in RECORDS2
The records in RECORDS1 exists in RECORDS2 but the value is different
Plus if the output could be extended such with an extra field with value as NEW (when does not not exist) as CHANGE (when exists but value is different)
Output
<DATA>
<RECORDS>
<RECORD>
<id>33</id>
<value>321</value>
<kind>Change</kind>
</RECORD>
<RECORD>
<id>55</id>
<value>121113</value>
<kind>New</kind>
</RECORD>
...
</RECORDS>
I have applied FOR Loop but as the variable in xslt cant be reset hence it doesnot work.
Any ideas?
Perhaps
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output indent="yes"/>
<xsl:key name="rec2-complete" match="RECORDS2/RECORD" use="concat(id, '|', value)"/>
<xsl:key name="rec2-id" match="RECORDS2/RECORD" use="id"/>
<xsl:template match="DATA">
<xsl:apply-templates select="RECORDS1/RECORD[not(key('rec2-complete', concat(id, '|', value)))]"/>
</xsl:template>
<xsl:template match="RECORDS1/RECORD">
<xsl:copy>
<xsl:copy-of select="node()"/>
<merged>
<xsl:value-of select="if (key('rec2-id', id)/value != value) then 'changed' else 'new'"/>
</merged>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
implements the requirements.
Or, to construct the complete result you have shown now, use
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output indent="yes"/>
<xsl:key name="rec2-complete" match="RECORDS2/RECORD" use="concat(id, '|', value)"/>
<xsl:key name="rec2-id" match="RECORDS2/RECORD" use="id"/>
<xsl:template match="DATA">
<xsl:copy>
<RECORDS>
<xsl:apply-templates select="RECORDS1/RECORD[not(key('rec2-complete', concat(id, '|', value)))]"/>
</RECORDS>
</xsl:copy>
</xsl:template>
<xsl:template match="RECORDS1/RECORD">
<xsl:copy>
<xsl:copy-of select="node()"/>
<kind>
<xsl:value-of select="if (key('rec2-id', id)/value != value) then 'change' else 'new'"/>
</kind>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have following input XML. The successive E_Records are optional and it should be populated into L_Record. I have written the below XSLT coding. Is there any changes should i have to do?
Input XML
<?xml version="1.0" encoding="UTF-8"?>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
</Record>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
</E_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</E_Record>
</Record>
The Output XML i am expecting is
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>3</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>4</E_Qty>
</E_Record>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
</Record>
<Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>1</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>2</E_Qty>
</E_Record>
</L_Record>
<L_Record>
<Rec_Type>L</Rec_Type>
<L_Level>2</L_Level>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>5</E_Qty>
</E_Record>
<E_Record>
<Rec_Type>E</Rec_Type>
<E_Qty>6</E_Qty>
</E_Record>
</L_Record>
</Record>
I have written the XSLT mapping for this as below but i am not getting the required output. Could you please help me on this?
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Record/L_Record">
<L_Record>
<xsl:variable name="header" select="."/>
<xsl:apply-templates/>
<xsl:if test = "not(following-sibling::L_Record)">
<xsl:for-each select="following-sibling::E_Record[preceding-sibling::L_Record = $header]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:if>
</L_Record>
</xsl:template>
</xsl:stylesheet>
Please help me on this?
When i am executing the above code, Record 1 is working fine, but the record 2 is not working properly. The E-Record segment is not appearing in the L-Record segment.
Here's an example that uses group-starting-with mentioned by Martin...
<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="Record">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="*" group-starting-with="L_Record">
<xsl:copy>
<xsl:apply-templates select="#*,node(),current-group()[self::E_Record]"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Fiddle: http://xsltfiddle.liberty-development.net/bFWRApc
I am trying to create an XSLT to transform an XML document and having trouble with identifying the record boundaries. Below is my xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<mheader>
<mid>1</mid>
<mname>mn</mname>
</mheader>
<cheader>
<cid>1</cid>
<cname>cn</cname>
</cheader>
<lheader>
<lid>1</lid>
<lname>ln</lname>
</lheader>
<aheader>
<aid>1</aid>
<aname>an</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay1</pname>
</pos>
<mheader>
<mid>2</mid>
<mname>mh1</mname>
</mheader>
<cheader>
<cid>2</cid>
<cname>ch1</cname>
</cheader>
<lheader>
<lid>2</lid>
<lname>lh1</lname>
</lheader>
<aheader>
<aid>2</aid>
<aname>ah1</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay3</pname>
</pos>
<pos>
<pid>3</pid>
<pname>pay4</pname>
</pos>
</catalog>
I have to transform my xml like the one below
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<record>
<mheader>
<mid>1</mid>
<mname>mn</mname>
</mheader>
<cheader>
<cid>1</cid>
<cname>cn</cname>
</cheader>
<lheader>
<lid>1</lid>
<lname>ln</lname>
</lheader>
<aheader>
<aid>1</aid>
<aname>an</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay1</pname>
</pos>
</record>
<record>
<mheader>
<mid>2</mid>
<mname>mh1</mname>
</mheader>
<cheader>
<cid>2</cid>
<cname>ch1</cname>
</cheader>
<lheader>
<lid>2</lid>
<lname>lh1</lname>
</lheader>
<aheader>
<aid>2</aid>
<aname>ah1</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay3</pname>
</pos>
<pos>
<pid>3</pid>
<pname>pay4</pname>
</pos>
</record>
</catalog>
A record ideally should start from the tag mheader and ends at the last POS tag.
This is what i have tried till now
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<record>
<xsl:apply-templates select="catalog/mheader"/>
<xsl:apply-templates select="catalog/cheader"/>
<xsl:apply-templates select="catalog/lheader"/>
<xsl:apply-templates select="catalog/aheader"/>
<xsl:apply-templates select="catalog/pos"/>
</record>
</xsl:template>
</xsl:stylesheet>
Any ideas on how to form a proper record here in this case?
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalog">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="mheader">
<Record>
<xsl:copy-of select="current-group()"/>
</Record>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Check it.
I have a strange transformation that I'm trying to do.
XML looks like this:
<?xml version="1.0" standalone="yes"?>
<Parent>
<RecordCount>4</RecordCount>
<Record name="1">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
</Parent>
And this is what it needs to look like:
<?xml version="1.0" standalone="yes"?>
<Parent>
<RecordCount>4</RecordCount>
<Record name="1">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="2">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="3">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="4">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
</Parent>
Is something like this even possible with XSLT or should I rather just handle this in code?
Here's another way using XSLT 2.0...
XML Input
<Parent>
<RecordCount>4</RecordCount>
<Record name="1">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
</Parent>
XSLT 2.0
<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="RecordCount">
<xsl:variable name="record" select="../Record"/>
<xsl:copy-of select="."/>
<xsl:for-each select="1 to .">
<xsl:apply-templates select="$record" mode="replicate">
<xsl:with-param name="cnt" select="."/>
</xsl:apply-templates>
</xsl:for-each>
</xsl:template>
<xsl:template match="Record" mode="replicate">
<xsl:param name="cnt"/>
<Record name="{$cnt}">
<xsl:apply-templates select="#* except #name|node()"/>
</Record>
</xsl:template>
<xsl:template match="Record"/>
</xsl:stylesheet>
Output
<Parent>
<RecordCount>4</RecordCount>
<Record name="1">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="2">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="3">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
<Record name="4">
<Child1>Value 1</Child1>
<Child2>Value 2</Child2>
</Record>
</Parent>
Try following xlst
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.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="/Parent">
<Parent>
<xsl:variable name="count" select="RecordCount" />
<xsl:call-template name="multiply">
<xsl:with-param name="maxCount" select="$count" />
<xsl:with-param name="nodeToCopy" select="Record" />
</xsl:call-template>
</Parent>
</xsl:template>
<xsl:template name="multiply">
<xsl:param name="maxCount" />
<xsl:param name="i" select="1" />
<xsl:param name="nodeToCopy" />
<xsl:choose>
<xsl:when test="$i <= $maxCount">
<xsl:element name="{name($nodeToCopy)}">
<xsl:attribute name="name">
<xsl:value-of select="$i" />
</xsl:attribute>
<xsl:copy-of select="$nodeToCopy/child::*" />
</xsl:element>
<xsl:call-template name="multiply">
<xsl:with-param name="maxCount" select="$maxCount" />
<xsl:with-param name="nodeToCopy" select="$nodeToCopy" />
<xsl:with-param name="i" select="$i+1" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
It is based on recursive calling of named template with increasing of "iterating" value. If would be something unclear just write a comment.
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.