Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2018, 02:58 AM
Registered User
 
Join Date: Dec 2018
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to group repeated segment in same context

select every following segment until next sibling occurs and group them under target structure

Source Structure-->
Code:
<?xml version="1.0" encoding="utf-8"?>
<ns:MM_FlatFile_CBOX_ORDRSP_Out xmlns:ns="urn:test:corp:pi:a:ecc:cbox:ordrsp:100">
	<DataSet>
		<EDI_Header>
			<LineIdent>SIM</LineIdent>
			<EDIPartnerNo>047866091</EDIPartnerNo>
			<MessageType>ORDRSP01</MessageType>
			<VersionNo>3A7V0</VersionNo>
			<Mode>102</Mode>
		</EDI_Header>
		<Header>
			<LineIdent>HO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<CustomerOrderNo>0038283143</CustomerOrderNo>
			<MessageFunction/>
			<Reference1>6174625</Reference1>
			<Reference2/>
			<Reference3/>
			<SoldToID>126919</SoldToID>
			<Partner2>126919</Partner2>
			<Partner3/>
			<ShipToID>126919</ShipToID>
			<ShipToName>XXXXX</ShipToName>
			<ShipToAddress/>
			<ShipToAddress2>XXXXX</ShipToAddress2>
			<ShipToCity>XXXXX</ShipToCity>
			<ShipToZIP>XXXXX</ShipToZIP>
			<ShipToCountry>FI</ShipToCountry>
			<CustContact>XXXXX</CustContact>
			<CustContactTelNo>XXXXX</CustContactTelNo>
			<TransportType>EUR</TransportType>
		</Header>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
		</Item>
		<Delivery_Info>
			<LineIdent>TE01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1DI1</LineItemNo>
			<Quantity>3000</Quantity>
			<Unit>PCE</Unit>
			<DelDateReq>20181130</DelDateReq>
			<DelDate>20181212</DelDate>
			<DateCode>C</DateCode>
			<QuantityOrdered>3000</QuantityOrdered>
			<DateCurrent>20181121</DateCurrent>
			<SysVar/>
		</Delivery_Info>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
		</Item>
		<Delivery_Info>
			<LineIdent>TE01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1DI1</LineItemNo>
			<Quantity>3000</Quantity>
			<Unit>PCE</Unit>
			<DelDateReq>20181130</DelDateReq>
			<DelDate>20181212</DelDate>
			<DateCode>C</DateCode>
			<QuantityOrdered>3000</QuantityOrdered>
			<DateCurrent>20181121</DateCurrent>
			<SysVar/>
		</Delivery_Info>
	</DataSet>
	<DataSet>
		<EDI_Header>
			<LineIdent>SIM</LineIdent>
			<EDIPartnerNo>047866091</EDIPartnerNo>
			<MessageType>ORDRSP01</MessageType>
			<VersionNo>3A7V0</VersionNo>
			<Mode>102</Mode>
		</EDI_Header>
		<Header>
			<LineIdent>HO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<CustomerOrderNo>0038283143</CustomerOrderNo>
			<MessageFunction/>
			<Reference1>6174625</Reference1>
			<Reference2/>
			<Reference3/>
			<SoldToID>126919</SoldToID>
			<Partner2>126919</Partner2>
			<Partner3/>
			<ShipToID>126919</ShipToID>
			<ShipToName>XXXXX</ShipToName>
			<ShipToAddress/>
			<ShipToAddress2>XXXXX</ShipToAddress2>
			<ShipToCity>XXXXX</ShipToCity>
			<ShipToZIP>XXXXX</ShipToZIP>
			<ShipToCountry>FI</ShipToCountry>
			<CustContact>XXXXX</CustContact>
			<CustContactTelNo>XXXXX</CustContactTelNo>
			<TransportType>EUR</TransportType>
		</Header>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
		</Item>
		<Delivery_Info>
			<LineIdent>TE01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1DI1</LineItemNo>
			<Quantity>3000</Quantity>
			<Unit>PCE</Unit>
			<DelDateReq>20181130</DelDateReq>
			<DelDate>20181212</DelDate>
			<DateCode>C</DateCode>
			<QuantityOrdered>3000</QuantityOrdered>
			<DateCurrent>20181121</DateCurrent>
			<SysVar/>
		</Delivery_Info>
		<Delivery_Info>
			<LineIdent>TE01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1DI1</LineItemNo>
			<Quantity>3000</Quantity>
			<Unit>PCE</Unit>
			<DelDateReq>20181130</DelDateReq>
			<DelDate>20181212</DelDate>
			<DateCode>C</DateCode>
			<QuantityOrdered>3000</QuantityOrdered>
			<DateCurrent>20181121</DateCurrent>
			<SysVar/>
		</Delivery_Info>
		<Delivery_Info>
			<LineIdent>TE01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1DI1</LineItemNo>
			<Quantity>3000</Quantity>
			<Unit>PCE</Unit>
			<DelDateReq>20181130</DelDateReq>
			<DelDate>20181212</DelDate>
			<DateCode>C</DateCode>
			<QuantityOrdered>3000</QuantityOrdered>
			<DateCurrent>20181121</DateCurrent>
			<SysVar/>
		</Delivery_Info>
	</DataSet>
</ns:MM_FlatFile_CBOX_ORDRSP_Out>

Target Structure-->
Code:
<?xml version="1.0" encoding="utf-8"?>
<ns:MM_FlatFile_CBOX_ORDRSP_Out xmlns:ns="urn:test:corp:pi:a:ecc:cbox:ordrsp:100">
	<DataSet>
		<EDI_Header>
			<LineIdent>SIM</LineIdent>
			<EDIPartnerNo>047866091</EDIPartnerNo>
			<MessageType>ORDRSP01</MessageType>
			<VersionNo>3A7V0</VersionNo>
			<Mode>102</Mode>
		</EDI_Header>
		<Header>
			<LineIdent>HO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<CustomerOrderNo>0038283143</CustomerOrderNo>
			<MessageFunction/>
			<Reference1>6174625</Reference1>
			<Reference2/>
			<Reference3/>
			<SoldToID>126919</SoldToID>
			<Partner2>126919</Partner2>
			<Partner3/>
			<ShipToID>126919</ShipToID>
			<ShipToName>XXXXX</ShipToName>
			<ShipToAddress/>
			<ShipToAddress2>XXXXX</ShipToAddress2>
			<ShipToCity>XXXXX</ShipToCity>
			<ShipToZIP>XXXXX</ShipToZIP>
			<ShipToCountry>FI</ShipToCountry>
			<CustContact>XXXXX</CustContact>
			<CustContactTelNo>XXXXX</CustContactTelNo>
			<TransportType>EUR</TransportType>
		</Header>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
			<Deliveryinfoset>
				<Delivery_Info>
					<LineIdent>TE01</LineIdent>
					<SupplierOrderNo>6174625</SupplierOrderNo>
					<LineItemNo>D1L1DI1</LineItemNo>
					<Quantity>3000</Quantity>
					<Unit>PCE</Unit>
					<DelDateReq>20181130</DelDateReq>
					<DelDate>20181212</DelDate>
					<DateCode>C</DateCode>
					<QuantityOrdered>3000</QuantityOrdered>
					<DateCurrent>20181121</DateCurrent>
					<SysVar/>
				</Delivery_Info>
			</Deliveryinfoset>
		</Item>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
			<Deliveryinfoset>
				<Delivery_Info>
					<LineIdent>TE01</LineIdent>
					<SupplierOrderNo>6174625</SupplierOrderNo>
					<LineItemNo>D1L1DI1</LineItemNo>
					<Quantity>3000</Quantity>
					<Unit>PCE</Unit>
					<DelDateReq>20181130</DelDateReq>
					<DelDate>20181212</DelDate>
					<DateCode>C</DateCode>
					<QuantityOrdered>3000</QuantityOrdered>
					<DateCurrent>20181121</DateCurrent>
					<SysVar/>
				</Delivery_Info>
			</Deliveryinfoset>
		</Item>
	</DataSet>
	<DataSet>
		<EDI_Header>
			<LineIdent>SIM</LineIdent>
			<EDIPartnerNo>047866091</EDIPartnerNo>
			<MessageType>ORDRSP01</MessageType>
			<VersionNo>3A7V0</VersionNo>
			<Mode>102</Mode>
		</EDI_Header>
		<Header>
			<LineIdent>HO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<CustomerOrderNo>0038283143</CustomerOrderNo>
			<MessageFunction/>
			<Reference1>6174625</Reference1>
			<Reference2/>
			<Reference3/>
			<SoldToID>126919</SoldToID>
			<Partner2>126919</Partner2>
			<Partner3/>
			<ShipToID>126919</ShipToID>
			<ShipToName>XXXXX</ShipToName>
			<ShipToAddress/>
			<ShipToAddress2>XXXXX</ShipToAddress2>
			<ShipToCity>XXXXX</ShipToCity>
			<ShipToZIP>XXXXX</ShipToZIP>
			<ShipToCountry>FI</ShipToCountry>
			<CustContact>XXXXX</CustContact>
			<CustContactTelNo>XXXXX</CustContactTelNo>
			<TransportType>EUR</TransportType>
		</Header>
		<Item>
			<LineIdent>PO01</LineIdent>
			<SupplierOrderNo>6174625</SupplierOrderNo>
			<LineItemNo>D1L1</LineItemNo>
			<ActionCode>6</ActionCode>
			<ItemNoCust>P380200</ItemNoCust>
			<ItemNoSup>B39172B5364U410W 3</ItemNoSup>
			<Quantity>D1L1</Quantity>
			<Unit>PCE</Unit>
			<Price>0.4000</Price>
			<PriceType/>
			<QuantityPriceUnit>1</QuantityPriceUnit>
			<LineItemNoCust/>
			<Deliveryinfoset>
				<Delivery_Info>
					<LineIdent>TE01</LineIdent>
					<SupplierOrderNo>6174625</SupplierOrderNo>
					<LineItemNo>D1L1DI1</LineItemNo>
					<Quantity>3000</Quantity>
					<Unit>PCE</Unit>
					<DelDateReq>20181130</DelDateReq>
					<DelDate>20181212</DelDate>
					<DateCode>C</DateCode>
					<QuantityOrdered>3000</QuantityOrdered>
					<DateCurrent>20181121</DateCurrent>
					<SysVar/>
				</Delivery_Info>
				<Delivery_Info>
					<LineIdent>TE01</LineIdent>
					<SupplierOrderNo>6174625</SupplierOrderNo>
					<LineItemNo>D1L1DI1</LineItemNo>
					<Quantity>3000</Quantity>
					<Unit>PCE</Unit>
					<DelDateReq>20181130</DelDateReq>
					<DelDate>20181212</DelDate>
					<DateCode>C</DateCode>
					<QuantityOrdered>3000</QuantityOrdered>
					<DateCurrent>20181121</DateCurrent>
					<SysVar/>
				</Delivery_Info>
				<Delivery_Info>
					<LineIdent>TE01</LineIdent>
					<SupplierOrderNo>6174625</SupplierOrderNo>
					<LineItemNo>D1L1DI1</LineItemNo>
					<Quantity>3000</Quantity>
					<Unit>PCE</Unit>
					<DelDateReq>20181130</DelDateReq>
					<DelDate>20181212</DelDate>
					<DateCode>C</DateCode>
					<QuantityOrdered>3000</QuantityOrdered>
					<DateCurrent>20181121</DateCurrent>
					<SysVar/>
				</Delivery_Info>
			</Deliveryinfoset>
		</Item>
	</DataSet>
</ns:MM_FlatFile_CBOX_ORDRSP_Out>
I am suppose to use xslt 1.0 version

Last edited by mrsnupan; December 4th, 2018 at 05:31 AM.. Reason: corrected input and output structure with real test data
 
Old December 4th, 2018, 04:38 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

In XSLT 2.0, use

Code:
<xsl:for-each-group select="*" group-starting-with="Item">
  <Item>
     ... item details ...
     <xsl:for-each select="current-group()[self::DeliveryInfo]">
        ... delivery details
     </xsl:for-each>
  </Item>
</xsl:for-each>
In XSLT 1.0, it's much more difficult. The simplest approach is along the lines

Code:
<xsl:for-each select="Item">
  <Item>
   ... item details ...
   <xsl:variable name="here" select="$generate-id()"/>
   <xsl:for-each select="following-sibling::DeliveryInfo[generate-id(preceding-sibling::Item[1]) = $here]">
      ... delivery details ...
   </xsl:for-each>
<xsl:for-each>
When asking XSLT questions, please always say which version you are using since the answers are often very different. Also use [ c o d e ] and [ / c o d e ] to delimit your code to make it readable.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 4th, 2018, 05:05 AM
Registered User
 
Join Date: Dec 2018
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael:
Thank you so much for quick help however my problem is DataSet will be multiple in a single file and we need to handle context as well as correct data should be populated for example i have corrected the entire input and output structure. May I request you to please help me here.
 
Old December 4th, 2018, 05:21 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is hard to understand your completely unindented input and output samples but I think you can use for-each-group, as already suggested (of course assuming you use an XSLT 2 or 3 processor like Saxon 9 or Altova 2017/18/19):

Code:
<?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="#all"
	version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output indent="yes"/>
  
  <xsl:template match="DataSet">
      <xsl:copy>
          <xsl:for-each-group select="*" group-starting-with="Item">
              <xsl:choose>
                  <xsl:when test="self::Item">
                      <xsl:copy>
                          <xsl:apply-templates select="node(), tail(current-group())"/>
                      </xsl:copy>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/jyH9rNt/2
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old December 4th, 2018, 05:34 AM
Registered User
 
Join Date: Dec 2018
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael:
Thanks for suggestion, However I am using XSLT 1.0 prod system has limitation to only use xslt 1.0, And also I have formatted/indent test data set. May I request you to please have a look.

Thanks
Jitendra
 
Old December 4th, 2018, 06:52 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I'm afraid it's over 15 years since I used XSLT 1.0 in anger and I've forgotten most of the workarounds.

And of course, it's no fun working with old technology when you know the problem is trivial to solve with something more up to date.

So I'm going to decline this one.

Sorry.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old December 6th, 2018, 05:08 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Go away, you scum. Find a way to promote your business that doesn't make your readers hate you.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Different color for a piechart segment ??? harifsheik C# 2005 3 July 30th, 2008 02:40 AM
How can I set ROLLBACK SEGMENT of Oracle in SSIS? Samuel Choi SQL Server DTS 1 August 14th, 2007 07:39 AM
How can I set ROLLBACK SEGMENT of Oracle in SSIS? Samuel Choi SQL Server 2005 0 March 8th, 2006 04:55 AM
Stack segment fuehrer C++ Programming 1 April 29th, 2005 01:59 AM
Inserting just the time segment Trojan_uk SQL Server ASP 1 April 15th, 2004 10:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.