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

Mahesh Madhusanka 216 Reputation points
2024-06-23T11:53:58.34+00:00

Hi Team,

Currently we have a created xslt map with for each to restrict looping same value section on for each loop - , in the below example repeating <poNumber> tag need to block on related section(Base on the order line, its mean same child loop), could you please assist on this?

Source-:

<?xml version="1.0" encoding="utf-8"?>
<UniversalInterchange xmlns="http://www.cws.com/Schemas/UN/2011/11" version="1.1">
	<Body>
		<UniversalShipment xmlns="http://www.cws.com/Schemas/UN/2011/11" version="1.1">
			<Shipment>
				<SubShipmentCollection>
					<SubShipment>
						
						<ActualChargeable>100.000</ActualChargeable>
						<RelatedShipmentCollection>
							<RelatedShipment>
								<AdditionalTerms/>
								<BookingConfirmationReference>P018091</BookingConfirmationReference>
								<Order>
									<OrderNumber>P018091</OrderNumber>
									<ClientReference>P018091</ClientReference>
									<OrderNumberSplit>0</OrderNumberSplit>
									<Status>
										<Code>INC</Code>
										<Description>Incomplete</Description>
									</Status>
									<OrderLineCollection Content="Complete">
										<OrderLine>
											<ContainerPackingOrder>0</ContainerPackingOrder>
											<InnerPacksQty>2.000</InnerPacksQty>
											<LineNumber>1</LineNumber>
											<PackageQty>100.000</PackageQty>
										</OrderLine>
										<OrderLine>
											<ContainerPackingOrder>0</ContainerPackingOrder>
											<InnerPacksQty>4.000</InnerPacksQty>
											<LineNumber>2</LineNumber>
											<PackageQty>300.000</PackageQty>
										</OrderLine>
										
									</OrderLineCollection>
								</Order>
							</RelatedShipment>
							
							<RelatedShipment>
								<AdditionalTerms/>
								<BookingConfirmationReference>P018092</BookingConfirmationReference>
								<Order>
									<OrderNumber>P018092</OrderNumber>
									<ClientReference>P018092</ClientReference>
									<OrderNumberSplit>0</OrderNumberSplit>
									<Status>
										<Code>INC</Code>
										<Description>Incomplete</Description>
									</Status>
									<OrderLineCollection Content="Complete">
										<OrderLine>
											<ContainerPackingOrder>0</ContainerPackingOrder>
											<InnerPacksQty>2.000</InnerPacksQty>
											<LineNumber>1</LineNumber>
											<PackageQty>140.000</PackageQty>
										</OrderLine>
										
									</OrderLineCollection>
								</Order>
							</RelatedShipment>
							
						</RelatedShipmentCollection>
					</SubShipment>
				</SubShipmentCollection>
			</Shipment>
		</UniversalShipment>
	</Body>
</UniversalInterchange>


Current xslt-:

<xsl:stylesheet xmlns:link="http://www.cws.com/Schemas/UN/2011/11" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://ua.com/3pl/FMS/XSDLibrary" xmlns:ms="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:gt="http://www.gtech.com/lsp/2009-09-23" xmlns:tr="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="ns0 ms dt gt tr">
	<xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
	<xsl:template match="/link:UniversalInterchange">
		<isfData>
			<manufacturer>
				<xsl:for-each select="link:Body/link:UniversalShipment/link:Shipment/link:SubShipmentCollection/link:SubShipment">
					<manufacturerName>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:CompanyName"/>
					</manufacturerName>
					<address1>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Address1"/>
					</address1>
					<address2>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Address2"/>
					</address2>
					<city>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:City"/>
					</city>
					<countryCode>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Country/link:Code"/>
					</countryCode>
					<postalCode>
						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Postcode"/>
					</postalCode>
				</xsl:for-each>
				
				<xsl:for-each select="link:Body/link:UniversalShipment/link:Shipment/link:SubShipmentCollection/link:SubShipment/link:RelatedShipmentCollection/link:RelatedShipment/link:Order/link:OrderLineCollection/link:OrderLine">
					<itemInfo>
						<xsl:for-each select="//link:RelatedShipment/link:Order/link:OrderNumber[1]">
							<poNumber>
								<xsl:value-of select="."/>
							</poNumber>
						</xsl:for-each>
						<itemDescription>BUCKET BAG</itemDescription>
						<countryOrigin>ID</countryOrigin>
						<quantity>
							<xsl:value-of select="link:PackageQty"/>
						</quantity>
					</itemInfo>
				</xsl:for-each>
				
			</manufacturer>
		</isfData>
	</xsl:template>
</xsl:stylesheet>

Current output-:

<isfData xmlns:link="http://www.cws.com/Schemas/UN/2011/11">
   <manufacturer>
      <manufacturerName/>
      <address1/>
      <address2/>
      <city/>
      <countryCode/>
      <postalCode/>
      <itemInfo>
         <poNumber>P018091</poNumber>
         <poNumber>P018092</poNumber>
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>100.000</quantity>
      </itemInfo>
      <itemInfo>
         <poNumber>P018091</poNumber>
         <poNumber>P018092</poNumber>
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>300.000</quantity>
      </itemInfo>
      <itemInfo>
         <poNumber>P018091</poNumber>
         <poNumber>P018092</poNumber>
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>140.000</quantity>
      </itemInfo>
   </manufacturer>
</isfData>


Expected output- :

<isfData xmlns:link="http://www.cws.com/Schemas/UN/2011/11">
   <manufacturer>
      <manufacturerName/>
      <address1/>
      <address2/>
      <city/>
      <countryCode/>
      <postalCode/>
      <itemInfo>
         <poNumber>P018091</poNumber>         
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>100.000</quantity>
      </itemInfo>
      <itemInfo>
         <poNumber>P018091</poNumber>
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>300.000</quantity>
      </itemInfo>
      <itemInfo>
         <poNumber>P018092</poNumber>
         <itemDescription>BUCKET BAG</itemDescription>
         <countryOrigin>ID</countryOrigin>
         <quantity>140.000</quantity>
      </itemInfo>
   </manufacturer>
</isfData>
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,934 questions
Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,221 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 25,956 Reputation points
    2024-06-23T16:44:02.91+00:00

    Hi @Mahesh Madhusanka,

    I made the following modifications:

    <?xml version='1.0'?>
    <xsl:stylesheet xmlns:link="http://www.cws.com/Schemas/UN/2011/11"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                    xmlns:ns0="http://ua.com/3pl/FMS/XSDLibrary"
                    xmlns:ms="urn:schemas-microsoft-com:xslt"
                    xmlns:dt="urn:schemas-microsoft-com:datatypes"
                    xmlns:gt="http://www.gtech.com/lsp/2009-09-23"
                    version="2.0"
                    exclude-result-prefixes="ns0 ms dt gt">
    	<xsl:output indent="yes"
    	            method="xml"
    	            omit-xml-declaration="yes"/>
    				
    	<xsl:template match="/link:UniversalInterchange">
    		<isfData>
    			<manufacturer>
    				<xsl:for-each select="link:Body/link:UniversalShipment/link:Shipment/link:SubShipmentCollection/link:SubShipment">
    					<manufacturerName>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:CompanyName"/>
    					</manufacturerName>
    					<address1>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Address1"/>
    					</address1>
    					<address2>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Address2"/>
    					</address2>
    					<city>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:City"/>
    					</city>
    					<countryCode>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Country/link:Code"/>
    					</countryCode>
    					<postalCode>
    						<xsl:value-of select="link:OrganizationAddressCollection/link:OrganizationAddress[link:AddressType='Manufacturer']/link:Postcode"/>
    					</postalCode>
    				</xsl:for-each>
    				<xsl:for-each select="link:Body/link:UniversalShipment/link:Shipment/link:SubShipmentCollection/link:SubShipment/link:RelatedShipmentCollection/link:RelatedShipment/link:Order/link:OrderLineCollection/link:OrderLine">
    					<itemInfo>
    						<!--<xsl:for-each select="//link:RelatedShipment/link:Order/link:OrderNumber[1]">-->
    							<poNumber>
    								<!--<xsl:value-of select="."/>-->
    								<xsl:value-of select="ancestor::link:Order/link:OrderNumber"/>
    							</poNumber>
    						<!--</xsl:for-each>-->
    						<itemDescription>BUCKET BAG</itemDescription>
    						<countryOrigin>ID</countryOrigin>
    						<quantity>
    							<xsl:value-of select="link:PackageQty"/>
    						</quantity>
    					</itemInfo>
    				</xsl:for-each>
    			</manufacturer>
    		</isfData>
    	</xsl:template>
    </xsl:stylesheet>
    

2 additional answers

Sort by: Most helpful
  1. gekka 9,826 Reputation points MVP
    2024-06-23T13:45:21.7366667+00:00
    <xsl:for-each select="link:Body/link:UniversalShipment/link:Shipment/link:SubShipmentCollection/link:SubShipment/link:RelatedShipmentCollection/link:RelatedShipment/link:Order/link:OrderLineCollection/link:OrderLine">
    	<itemInfo>
    		<poNumber>
    			<xsl:value-of select="ancestor::link:Order/link:OrderNumber"/>
    			<!--<xsl:value-of select="../../link:OrderNumber"/>-->
    		</poNumber>
    		<itemDescription>BUCKET BAG</itemDescription>
    		<countryOrigin>ID</countryOrigin>
    		<quantity>
    			<xsl:value-of select="link:PackageQty"/>
    		</quantity>
    	</itemInfo>
    </xsl:for-each>
    
    1 person found this answer helpful.

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.