How to block same value section on for each loop in XSLT 1

Mahesh Madhusanka 221 Reputation points
2024-06-12T11:27:54.77+00:00

Hi Team,

Currently we have a created xslt map with foreach to block looping using single value(PartNumbers) - , in the below example same partnumber(Consideration value) repeating on both Source-:

<rootnodecall>	
	<Shops>
		<ShopsSequence>1</ShopsSequence>		
		<DateTime>
			<DateAndTime>2024-06-01T14:00:00</DateAndTime>
			<TimeZone>Universal Time Coordinated</TimeZone>
		</DateTime>
		<OrderDetails>
			<Packagelineitem>
				<Length>12</Length>
				<Weight>13.7</Weight>				
				<PartNumbers>1525549-00-A</PartNumbers>
			</Packagelineitem>
		</OrderDetails>
	</Shops>
	<Shops>
		<ShopsSequence>2</ShopsSequence>
		
		<DateTime>
			<DateAndTime>2024-06-26T14:00:00</DateAndTime>
			<TimeZone>Universal Time Coordinated</TimeZone>
		</DateTime>
		<OrderDetails>
			<Packagelineitem>
				<Length>13</Length>
				<Weight>13.7</Weight>				
				<PartNumbers>1525549-00-A</PartNumbers>
			</Packagelineitem>
		</OrderDetails>
	</Shops>
</rootnodecall>


current XSLT- :

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
	<xsl:template match="/rootnodecall">
		<Shipment>
				<PackingLineCollection Content="Complete">
					<xsl:for-each select="Shops/OrderDetails/Packagelineitem[1]">
						<PackingLine>
							<Link>
								<xsl:value-of select="position()"/>
							</Link>							
							
							<Length>
								<xsl:value-of select="Length"/>
							</Length>
							<Width>
								<xsl:value-of select="Width"/>
							</Width>
						</PackingLine>
					</xsl:for-each>
				</PackingLineCollection>
				
			</Shipment>
		
	</xsl:template>
</xsl:stylesheet>

Current Output

<Shipment>
   <PackingLineCollection Content="Complete">
      <PackingLine>
         <Link>1</Link>
         <Length>12</Length>
         <Width/>
      </PackingLine>
      <PackingLine>
         <Link>2</Link>
         <Length>13</Length>
         <Width/>
      </PackingLine>
   </PackingLineCollection>
</Shipment>


Expected output-:

<Shipment>
   <PackingLineCollection Content="Complete">
      <PackingLine>
         <Link>1</Link>
         <Length>12</Length>
         <Width/>
      </PackingLine>
      </PackingLineCollection>
</Shipment>

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,542 questions
Developer technologies .NET Other
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 26,586 Reputation points
    2024-06-17T14:38:50.55+00:00

    Hi @Mahesh Madhusanka,

    Please try the following XSLT 1.0.

    It is using grouping to produce a single entry for each Shops tag.

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="xml" indent="yes"/>
    	<xsl:strip-space elements="*"/>
    	<xsl:key name="ShopsKey" match="rootnodecall/Shops"
    	         use="OrderDetails/Packagelineitem/PartNumbers"/>
    	<xsl:template match="/rootnodecall">
    		<Shipment>
    			<PackingLineCollection Content="Complete">
    				<xsl:for-each select="Shops[generate-id(.) = generate-id(key('ShopsKey', OrderDetails/Packagelineitem/PartNumbers)[1])]">
    					<xsl:sort select="ShopsSequence"
    					          order="ascending"/>
    					<PackingLine>
    							<Link>
    								<xsl:value-of select="position()"/>
    							</Link>							
    							<Length>
    								<xsl:value-of select="OrderDetails/Packagelineitem/Length"/>
    							</Length>
    							<Width>
    								<xsl:value-of select="OrderDetails/Packagelineitem/Width"/>
    							</Width>
    						</PackingLine>
    				</xsl:for-each>
    			</PackingLineCollection>
    		</Shipment>
    	</xsl:template>
    </xsl:stylesheet>
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-06-13T03:16:19.03+00:00

    Hi,@Mahesh Madhusanka. Welcome to Microsoft Q&A. 

    Adjusting xslt

    
    <xsl:for-each select="Shops[1]/OrderDetails/Packagelineitem[1]">
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Mahesh Madhusanka 221 Reputation points
    2024-06-20T09:52:58.71+00:00

    Hi @Yitzhak Khabinsky , Thank you very much its working as expected, much appreciate your support.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.