Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XSLT
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 4th, 2018, 02:58 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #2 (permalink)  
Old December 4th, 2018, 04:38 AM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 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
Reply With Quote
  #3 (permalink)  
Old December 4th, 2018, 05:05 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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.
Reply With Quote
  #4 (permalink)  
Old December 4th, 2018, 05:21 AM
Friend of Wrox
Points: 6,666, Level: 34
Points: 6,666, Level: 34 Points: 6,666, Level: 34 Points: 6,666, Level: 34
Activity: 50%
Activity: 50% Activity: 50% Activity: 50%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,240
Thanks: 0
Thanked 244 Times in 243 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
Reply With Quote
  #5 (permalink)  
Old December 4th, 2018, 05:34 AM
Registered User
Points: 12, Level: 1
Points: 12, Level: 1 Points: 12, Level: 1 Points: 12, Level: 1
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
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
Reply With Quote
  #6 (permalink)  
Old December 4th, 2018, 06:52 AM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 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
Reply With Quote
  #7 (permalink)  
Old December 6th, 2018, 05:08 AM
mhkay's Avatar
Wrox Author
Points: 18,420, Level: 59
Points: 18,420, Level: 59 Points: 18,420, Level: 59 Points: 18,420, Level: 59
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,952
Thanks: 0
Thanked 289 Times in 284 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
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Different color for a piechart segment ??? harifsheik C# 2005 3 July 30th, 2008 03:40 AM
How can I set ROLLBACK SEGMENT of Oracle in SSIS? Samuel Choi SQL Server DTS 1 August 14th, 2007 08: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 02:59 AM
Inserting just the time segment Trojan_uk SQL Server ASP 1 April 15th, 2004 11:03 AM



All times are GMT -4. The time now is 04:41 AM.


Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.