Reputation:
This one however is a bit different, as I have absolutely no clue about XSL, and need to get this template working for our shipping software.
I've got this doing what I want for the most part, learning as I go as I did with most other languages. But I can't seem to get the recursive loop going that I need.
Basically I need it to use the numeric value of $order/Item/Quantity
to determine how many times to loop each of the "Name" variables in the for-each.
It will currently print the line items as it is supposed to, but only one of them. I've played with various different recursive templates that have been posted here, but due to my lack of knowledge in XSL I've been unable to get it working, so have reverted back to the original (before I started working on the quantity part).
Here is the entire template,
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:sw="http://www.interapptive.com/shipworks" extension-element-prefixes="sw">
<!-- Imports -->
<xsl:import href="System\Snippets" />
<xsl:output method="html" encoding="utf-8" indent="yes" />
<!-- Start of template -->
<xsl:template match="/"><xsl:apply-templates /></xsl:template>
<xsl:template match="ShipWorks">
<!-- Controls display of thumbnail images -->
<xsl:variable name="thumbnailsEnabled" select="false()" />
<xsl:variable name="thumbnailWidth" select="'50px'" />
<xsl:variable name="thumbnailHeight" select="'50px'" />
<!-- Width defined by the template PageSettings -->
<xsl:variable name="pageWidth" select="concat(Template/Output/ContentWidth, ' in')" />
<!-- Default font. Specified as a variable since GMail and Outlook behave differently. -->
<xsl:variable name="pageFont" select="'font-family: Arial; font-size: 8pt;'" />
<!-- These styles are used on multiple td's so to avoid copy\paste errors they are defined once here. We have to do this since GMail doesn't support <style> in the <head>. -->
<xsl:variable name="orderDetailHeaderStyle" select="'border: 1px solid dimgray; background-color: #F3F3F3; font-weight: bold; padding: 3px;'" />
<xsl:variable name="orderDetailAttributeStyle" select="'color: #808080; padding: 0px 8px 2px 2px;'" />
<xsl:variable name="orderChargeStyle" select="'white-space: nowrap; text-align: right; padding: 1px 8px 3px 16px;'" />
<html>
<head>
<title>Can label - gallon - Paints/Primers</title>
<style>
body, table { <xsl:value-of select="$pageFont" /> }
</style>
</head>
<body style="{$pageFont}">
<xsl:variable name="orderCount" select="count(Customer/Order)" />
<xsl:if test="$orderCount = 1">
<xsl:variable name="order" select="Customer/Order[1]" />
<xsl:for-each select="$order/Item">
<table style="width: 100%; height: 100%;" cellspacing="0">
<tr>
<td style="{$orderDetailAttributeStyle}; width: 100%; height: 100%; vertical-align: middle;" align="center">
<!-- this is what needs to be repeated based on the value of $order/Item/Quantity -->
<xsl:choose>
<xsl:when test="(contains(Name, 'Eggshell'))">
<span>Eggshell</span>
</xsl:when>
<xsl:when test="(contains(Name, 'Matte'))">
<span>Matte</span>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
<!-- End of repeated content, thanks in advance!!! -->
</td>
</tr>
</table>
</xsl:for-each>
</xsl:if>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
EDIT:
Here is the input that ShipWorks provides. The output should be a page with either "Matte" or "Eggshell" in the center
<?xml version="1.0" encoding="utf-8"?>
<ShipWorks>
<Generated>2018-03-21T16:09:39.905698Z</Generated>
<Timestamp>1521648579</Timestamp>
<UniqueID></UniqueID>
<Context>Order</Context>
<Template>
<Name>XML Source</Name>
<Folder>System\Utility</Folder>
<Output>
<ContentWidth>7</ContentWidth>
<ContentHeight>9.5</ContentHeight>
</Output>
</Template>
<User ID="1002">
<Username></Username>
<IsAdmin>true</IsAdmin>
</User>
<Store ID="20005">
<StoreName></StoreName>
<StoreType ID="28">
<Code>GENERIC</Code>
<Name>Generic - Module</Name>
</StoreType>
<LastDownload>2018-03-21T16:05:50.933Z</LastDownload>
<Address type="company">
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Not Checked</AddressValidationStatus>
<ResidentialStatus>Unknown</ResidentialStatus>
<POBox>Unknown</POBox>
<USTerritory>Unknown</USTerritory>
<MilitaryAddress>Unknown</MilitaryAddress>
</Address>
</Store>
<Customer ID="401765012">
<Summary>
<TotalSpent>80.4000</TotalSpent>
<OrdersPlaced>1</OrdersPlaced>
</Summary>
<Address type="ship">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Not Checked</AddressValidationStatus>
<ResidentialStatus>Unknown</ResidentialStatus>
<POBox>Unknown</POBox>
<USTerritory>Unknown</USTerritory>
<MilitaryAddress>Unknown</MilitaryAddress>
</Address>
<Address type="bill">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Not Checked</AddressValidationStatus>
<ResidentialStatus>Unknown</ResidentialStatus>
<POBox>Unknown</POBox>
<USTerritory>Unknown</USTerritory>
<MilitaryAddress>Unknown</MilitaryAddress>
</Address>
<Order ID="554407006" storeID="20005">
<Number>71704</Number>
<Date>2018-01-19T00:10:28Z</Date>
<Status>Complete</Status>
<OnlineStatus>Complete</OnlineStatus>
<OnlineStatusCode>C</OnlineStatusCode>
<OnlineCustomerID>11886</OnlineCustomerID>
<IsManual>false</IsManual>
<RequestedShipping>UPS Ground (Signature Required)</RequestedShipping>
<Total>80.4000</Total>
<Address type="ship">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Fixed</AddressValidationStatus>
<ResidentialStatus>Yes</ResidentialStatus>
<POBox>No</POBox>
<USTerritory>No</USTerritory>
<MilitaryAddress>No</MilitaryAddress>
</Address>
<Address type="bill">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Fixed</AddressValidationStatus>
<ResidentialStatus>Yes</ResidentialStatus>
<POBox>No</POBox>
<USTerritory>No</USTerritory>
<MilitaryAddress>No</MilitaryAddress>
</Address>
<Item ID="540525013">
<Name>Gloss Varnish</Name>
<Code></Code>
<SKU></SKU>
<Description></Description>
<Location></Location>
<ISBN></ISBN>
<UPC></UPC>
<Thumbnail></Thumbnail>
<Image></Image>
<UnitPrice>29.7500</UnitPrice>
<UnitPriceWithOptions>29.7500</UnitPriceWithOptions>
<UnitCost>0</UnitCost>
<Weight>0</Weight>
<Quantity>1</Quantity>
<TotalPrice>29.7500</TotalPrice>
<TotalPriceWithOptions>29.7500</TotalPriceWithOptions>
<TotalCost>0</TotalCost>
<TotalWeight>0</TotalWeight>
<Status></Status>
<IsManual>false</IsManual>
<HarmonizedCode></HarmonizedCode>
<Option ID="623514020">
<Name>Size</Name>
<Description>1 Quart</Description>
<UnitPrice>0</UnitPrice>
</Option>
</Item>
<Item ID="540526013">
<Name>Wood Stain</Name>
<Code></Code>
<SKU></SKU>
<Description></Description>
<Location></Location>
<ISBN></ISBN>
<UPC></UPC>
<Thumbnail></Thumbnail>
<Image></Image>
<UnitPrice>20.3500</UnitPrice>
<UnitPriceWithOptions>20.3500</UnitPriceWithOptions>
<UnitCost>0</UnitCost>
<Weight>0</Weight>
<Quantity>1</Quantity>
<TotalPrice>20.3500</TotalPrice>
<TotalPriceWithOptions>20.3500</TotalPriceWithOptions>
<TotalCost>0</TotalCost>
<TotalWeight>0</TotalWeight>
<Status></Status>
<IsManual>false</IsManual>
<HarmonizedCode></HarmonizedCode>
<Option ID="623515020">
<Name>Ordered this color before ?</Name>
<Description>No</Description>
<UnitPrice>0</UnitPrice>
</Option>
<Option ID="623516020">
<Name>Size</Name>
<Description>1 Gallon</Description>
<UnitPrice>0</UnitPrice>
</Option>
</Item>
<Item ID="540527013">
<Name>Matte Interior Wall & Ceiling Paint</Name>
<Code></Code>
<SKU></SKU>
<Description></Description>
<Location></Location>
<ISBN></ISBN>
<UPC></UPC>
<Thumbnail></Thumbnail>
<Image></Image>
<UnitPrice>20.3500</UnitPrice>
<UnitPriceWithOptions>20.3500</UnitPriceWithOptions>
<UnitCost>0</UnitCost>
<Weight>0</Weight>
<Quantity>1</Quantity>
<TotalPrice>20.3500</TotalPrice>
<TotalPriceWithOptions>20.3500</TotalPriceWithOptions>
<TotalCost>0</TotalCost>
<TotalWeight>0</TotalWeight>
<Status></Status>
<IsManual>false</IsManual>
<HarmonizedCode></HarmonizedCode>
<Option ID="623518020">
<Name>Size</Name>
<Description>1 Quart</Description>
<UnitPrice>0</UnitPrice>
</Option>
</Item>
<Charge ID="512146021">
<Type>SHIPPING</Type>
<Description>Shipping</Description>
<Amount>9.9500</Amount>
</Charge>
<Payment />
<Shipment ID="541189031">
<ShipmentType code="0">UPS</ShipmentType>
<Status>Processed</Status>
<Processed>true</Processed>
<ProcessedDate>2018-01-19T16:46:14.09Z</ProcessedDate>
<ProcessedUser ID="8002">
<Username></Username>
<IsAdmin>false</IsAdmin>
</ProcessedUser>
<Voided>false</Voided>
<VoidedDate></VoidedDate>
<ShippedDate>2018-01-19T12:00:00Z</ShippedDate>
<ServiceUsed>UPS Ground</ServiceUsed>
<ReturnShipment>false</ReturnShipment>
<TrackingNumber></TrackingNumber>
<TotalCharges>16.3300</TotalCharges>
<TotalWeight>9</TotalWeight>
<Address type="ship">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Fixed</AddressValidationStatus>
<ResidentialStatus>Yes</ResidentialStatus>
<POBox>No</POBox>
<USTerritory>No</USTerritory>
<MilitaryAddress>No</MilitaryAddress>
</Address>
<Address type="from">
<FullName></FullName>
<FirstName></FirstName>
<MiddleName></MiddleName>
<LastName></LastName>
<Company></Company>
<Line1></Line1>
<Line2></Line2>
<Line3></Line3>
<City></City>
<StateCode></StateCode>
<StateName></StateName>
<PostalCode></PostalCode>
<CountryCode></CountryCode>
<CountryName></CountryName>
<Phone></Phone>
<Fax></Fax>
<Email></Email>
<Website></Website>
<AddressValidationStatus>Not Checked</AddressValidationStatus>
<ResidentialStatus>Unknown</ResidentialStatus>
<POBox>Unknown</POBox>
<USTerritory>Unknown</USTerritory>
<MilitaryAddress>Unknown</MilitaryAddress>
</Address>
<BestRateEvent>None</BestRateEvent>
<ShipSense>
<Status>NotApplied</Status>
<ChangeSets />
</ShipSense>
<BilledType>Actual weight</BilledType>
<BilledWeight>9</BilledWeight>
<Package ID="523087063" TrackingNumber="">
<Insurance>
<Provider>Carrier</Provider>
<InsuredValue>100.0000</InsuredValue>
</Insurance>
<Dimensions>
<Length>13</Length>
<Width>13</Width>
<Height>7.25</Height>
</Dimensions>
<TotalWeight>9</TotalWeight>
</Package>
<Labels type="thermal">
<Package ID="523087063">
<Primary>
<Label name="Label" thermal="ZPL"></Label>
</Primary>
</Package>
</Labels>
<RequestedLabelFormat>Thermal - ZPL</RequestedLabelFormat>
</Shipment>
</Order>
</Customer>
</ShipWorks>
Upvotes: 2
Views: 137
Reputation: 66781
This task would be easier with XSLT 2.0 (or greater), because you could create a sequence of numbers with a range expression i.e. (1 to Quantity)
to iterate over.
With XSLT 1.0, one approach would be to use a recursive template call in which you pass the content that you would like to repeat in one parameter, and use another parameter to indicate how many times to repeat it. In each invocation of that template, produce the desired content and then decrement the quantity and call the template again until the parameter indicating how many times to repeat is equal to 0.
With this template added to your stylesheet:
<xsl:template name="repeat">
<xsl:param name="content"/>
<xsl:param name="times"/>
<xsl:if test="$times > 0">
<xsl:copy-of select="$content"/>
<xsl:call-template name="repeat">
<xsl:with-param name="content" select="$content"/>
<xsl:with-param name="times" select="$times - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
You could adjust your processing of the Item
elements to generate the content for the Item/Name
, and repeat it for as many times as the Quantity
value:
<xsl:for-each select="$order/Item">
<table style="width: 100%; height: 100%;" cellspacing="0">
<tr>
<!-- this is what needs to be repeated based on the value of $order/Item/Quantity -->
<xsl:variable name="name">
<td style="{$orderDetailAttributeStyle}; width: 100%; height: 100%; vertical-align: middle;" align="center">
<xsl:choose>
<xsl:when test="(contains(Name, 'Eggshell'))">
<span>Eggshell</span>
</xsl:when>
<xsl:when test="(contains(Name, 'Matte'))">
<span>Matte</span>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</td>
</xsl:variable>
<xsl:call-template name="repeat">
<xsl:with-param name="content" select="$name"/>
<xsl:with-param name="times" select="number(Quantity)"/>
</xsl:call-template>
<!-- End of repeated content, thanks in advance!!! -->
</tr>
</table>
</xsl:for-each>
Upvotes: 1
Reputation: 601
As Tim's suggestion, would be nice to see the XML example, but as far as I have noticed from the code, when you are defining the variable for "order" you select only the first value of it.
<xsl:variable name="order" select="Customer/Order[1]" />
This will select the first Order value, and when you select for-each $order, It will give you only the one value, not all of them since you restricted it with [1], therefore, It is normal to get only one value logically.
Upvotes: 1