XSLT Help

Here should go questions about transforming XML with XSLT and FOP.
akr
Posts: 1
Joined: Wed Dec 02, 2020 11:14 pm

XSLT Help

Post by akr »

I'm using a for-each loop for items packed in to a box. I may have 5 or 50 items in a box. Each box is assigned an ID. I need to associate the box ID with each item. However, my for-each loop xpath is as follows. xml/order/packages/package/items/item. I need to construct the new XML but also for each item i need to grab the result from a preceding value, xml/order/packages/package/boxid.

How do I grab box id from each for-each loop that's keyed on item?

XML in

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <order>
        <number>12345</number>
        <packages>
            <package>
                <boxID>000111</boxID>
                <weight>10</weight>
                <items>
                    <item>
                        <id>11111</id>
                       <size>8x10</size>
                    </item>
                    <item>
                        <id>22222</id>
                        <size>8x10</size>
                    </item>
                    <item>
                        <id>33333</id>
                        <size>8x10</size>
                    </item>
                    <item>
                        <id>44444</id>
                        <size>8x10</size>
                    </item>
                </items>
            </package>
                <package>
                    <boxID>000222</boxID>
                    <weight>10</weight>
                    <items>
                        <item>
                            <id>11111</id>
                            <size>8x10</size>
                        </item>
                        <item>
                            <id>22222</id>
                            <size>8x10</size>
                        </item>
                        <item>
                            <id>33333</id>
                            <size>8x10</size>
                        </item>
                        <item>
                            <id>44444</id>
                            <size>8x10</size>
                        </item>
                    </items>
                </package>
        </packages>
    </order>
</xml>

Output XML

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <shipped>
        <order>
           <number>12345</number>
        <items>
            <item>
                <boxID>000111</boxID>
                <weight>10</weight>
                <id>11111</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000111</boxID>
                <weight>10</weight>
                <id>22222</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000111</boxID>
                <weight>10</weight>
                <id>33333</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000111</boxID>
                <weight>10</weight>
                <id>4444</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000222</boxID>
                <weight>10</weight>
                <id>11111</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000222</boxID>
                <weight>10</weight>
                <id>22222</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000222</boxID>
                <weight>10</weight>
                <id>33333</id>
                <size>8x10</size>
            </item>
            <item>
                <boxID>000222</boxID>
                <weight>10</weight>
                <id>4444</id>
                <size>8x10</size>
            </item>
        </items>
        </order>
    </shipped>
</xml>


Thanks,
Adam


From: Adam Rothenberger <AKR@brprinters.com>
Date: Tuesday, December 1, 2020 at 3:53 PM
To: Oxygen XML Editor Support <info@oxygenxml.com>
Subject: XSLT Questions

Hello,
I am stumped on an XSLT.

I need to loop an XML with a for-each but still get variables from child nodes to iterate through.

I need to for-each on “shipment/packages/package/items/item”

But

I also need to pull tracking number for each of those loops from “shipment/packages/package/trackingNumber”.


Each item could share the same tracking number or different, back up the XPath.


How do I look back to the tracking number if I’m in a for-each loop to “shipment/packages/package/items/item”? I need to display one SSCC variable per tem loop.

Code: Select all

 
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"  >
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    
    <xsl:template match="/">
        
        
        <DeliveryMessageBook DeliveryMessageType="DeliveryMessage" DeliveryMessageStatusType="Original">            <DeliveryMessageBookShipment>
                <DeliveryShipmentId>1</DeliveryShipmentId>
                <xsl:for-each select="shipment/packages/package/items/item">
                    
                    <xsl:variable name="sscc">
                        <xsl:value-of select="shipment/packages/package/trackingNumber"/>
           
                    </xsl:variable>
             
                    <xsl:variable name="depth">
                        <xsl:value-of select="shipment/packages/package/depth"/>
                    </xsl:variable>
                    <xsl:variable name="height">
                        <xsl:value-of select="shipment/packages/package/height"/>
                    </xsl:variable>
                    <xsl:variable name="width">
                        <xsl:value-of select="shipment/packages/package/width"/>
                    </xsl:variable>
                    <xsl:variable name="track">
                        <xsl:value-of select="shipment/packages/package/shipmentIndex"/>
                    </xsl:variable>
                    
                    <DeliveryShipmentBookLineItem>
                        <DeliveryShipmentLineItemNumber><xsl:value-of select="shipmentIndex"/></DeliveryShipmentLineItemNumber>
                        <PurchaseOrderInformation>
                            <PurchaseOrderNumber><xsl:value-of select="/shipment/packages[1]/package[1]/items[1]/item[1]/purchaseOrder[1]"/></PurchaseOrderNumber>
                            <PurchaseOrderIssuedDate>
                                <Date>
                                    <Year>2020</Year>
                                    <Month>10</Month>
                                    <Day>27</Day>
                                </Date>
                            </PurchaseOrderIssuedDate>
                        </PurchaseOrderInformation>
                        <PurchaseOrderLineItemNumber><xsl:value-of select="shipmentIndex"/></PurchaseOrderLineItemNumber>
                        <DeliveryMessageReference DeliveryMessageReferenceType="BillOfLadingNumber"
                            ><xsl:value-of select="$track"/></DeliveryMessageReference>
                        <Quantity QuantityType="Count">
                            <Value UOM="Box">1</Value>
                        </Quantity>
                        <InformationalQuantity QuantityType="NetWeight">
                            <Value UOM="Pound"><xsl:value-of select="round(weight)"/></Value>
                        </InformationalQuantity>
                        <InformationalQuantity QuantityType="Count">
                            <Value UOM="Unit"><xsl:value-of select="scanned"/></Value>
                        </InformationalQuantity>
                        <BookPackageInformation PackageType="Box" MixedProductPalletIndicator="Yes">
                            <Identifier IdentifierCodeType="SerialisedShippingContainerCode"
                                IdentifierType="Barcode">0</Identifier>
                            <ItemCount>
                                <Value UOM="Box">0</Value>
                            </ItemCount>
                            <Quantity QuantityType="Count">
                                <Value UOM="Unit">0</Value>
                            </Quantity>
                            <PackageReference PackageReferenceType="TrackingNumber" AssignedBy="Buyer"
                                ><xsl:value-of select="$sscc"/></PackageReference>
                        
                                
                                <BoxItem>
                                    <Identifier IdentifierCodeType="SerialisedShippingContainerCode"
                                        IdentifierType="Barcode"></Identifier>
                                    <ItemCount>
                                        <Value UOM="Box">1</Value>
                                    </ItemCount>
                                    <Quantity QuantityType="Count">
                                        <Value UOM="Unit">1</Value>
                                    </Quantity>
                                    <UnitItem>
                                        <UnitCharacteristics>
                                            <Height>
                                                <Value UOM="Inch"><xsl:value-of select="TrimSizeHeight"/></Value>
                                            </Height>
                                            <Thickness>
                                                <Value UOM="Inch"><xsl:value-of select="SpineSize"/></Value>
                                            </Thickness>
                                            <Width>
                                                <Value UOM="Inch"><xsl:value-of select="TrimSizeWidth"/></Value>
                                            </Width>
                                            <Weight>
                                                <Value UOM="Pound"><xsl:value-of select="weight"/></Value>
                                            </Weight>
                                        </UnitCharacteristics>
                                        <Quantity QuantityType="Count">
                                            <Value UOM="Unit"><xsl:value-of select="count"/></Value>
                                        </Quantity>
                                    </UnitItem>
                                    <BoxCharacteristics>
                                        <Height>
                                            <Value UOM="Inch"><xsl:value-of select="$depth"/></Value>
                                        </Height>
                                        <Length>
                                            <Value UOM="Inch"><xsl:value-of select="$width"/></Value>
                                        </Length>
                                        <Width>
                                            <Value UOM="Inch"><xsl:value-of select="$height"/></Value>
                                        </Width>
                                        <Weight>
                                            <Value UOM="Pound">.5</Value>
                                        </Weight>
                                    </BoxCharacteristics>
                                </BoxItem>
                          
                        </BookPackageInformation>
                  
                            <Product>
                                <ProductIdentifier Agency="Buyer" ProductIdentifierType="PartNumber"
                                    ><xsl:value-of select="sourceItemId"/></ProductIdentifier>
                                <BookManufacturing>
                                    <PackagingCharacteristics>
                                        <PalletPackagingCharacteristics>
                                            <PalletCharacteristics>
                                                <PalletLength>
                                                    <Value UOM="Inch">0</Value>
                                                </PalletLength>
                                                <PalletWidth>
                                                    <Value UOM="Inch">0</Value>
                                                </PalletWidth>
                                                <MaximumHeight>
                                                    <Value UOM="Inch">0</Value>
                                                </MaximumHeight>
                                            </PalletCharacteristics>
                                            <MaximumGrossWeight>
                                                <Value UOM="Pound">0</Value>
                                            </MaximumGrossWeight>
                                        </PalletPackagingCharacteristics>
                                    </PackagingCharacteristics>
                                </BookManufacturing>
                                
                            </Product>
                      
                    </DeliveryShipmentBookLineItem>
                </xsl:for-each>
                <ShipmentSummary>
                    <TotalQuantity QuantityType="Count">
                        <Value UOM="Unit"><xsl:value-of select="scanned"/></Value>
                    </TotalQuantity>
                </ShipmentSummary>
            </DeliveryMessageBookShipment>
            <DeliveryMessageBookSummary>
                <TotalNumberOfShipments>1</TotalNumberOfShipments>
                <TotalQuantity QuantityType="Count">
                    <Value UOM="Unit"><xsl:value-of select="shipment/scannedQuantity"/></Value>
                </TotalQuantity>
                <TotalInformationalQuantity QuantityType="NetWeight">
                    <Value UOM="Pound"><xsl:value-of select="sum(shipment/packages/package/totalWeight)"/></Value>
                </TotalInformationalQuantity>
            </DeliveryMessageBookSummary>
        </DeliveryMessageBook>
        
        
    </xsl:template>
    
    
    
</xsl:stylesheet>
  
Thank you,

Adam Rothenberger
Vice President of Technology
BRprinters
Mobile: 260-318-1991
Email: akr@brprinters.com
/var/folders/_k/cc6j_n_n0cv07yrpvyh72qpw0000gn/T/com.microsoft.Outlook/WebArchiveCopyPasteTempFiles/cidimage003.jpg@01D3E222.40ED5F20
Radu
Posts: 9053
Joined: Fri Jul 09, 2004 5:18 pm

Re: XSLT Help

Post by Radu »

Hi Adam,

Once you are inside the for-each loop, the context element changes to each item element.
So if the current context element is the item, then you can gain access to the ancestor package something like this:

Code: Select all

      <xsl:for-each select="shipment/packages/package/items/item">
        <xsl:variable name="parentPackage" select="ancestor::package"/>
        <xsl:variable name="sscc">
          <xsl:value-of select="$parentPackage/trackingNumber"/>
        </xsl:variable>
        
         <xsl:variable name="depth">
          <xsl:value-of select="$parentPackage/depth"/>
        </xsl:variable>
        ......
   
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply