|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with creating XSLT
I have this XML:
<AmazonEnvelope xsi:noNamespaceSchemaLocation="amzn-envelope.xsd"> <Message> <MessageID>1</MessageID> - <OrderReport> <AmazonOrderID>058-0986845-5785165</AmazonOrderID> <AmazonSessionID>103-5085382-1579837</AmazonSessionID> <OrderDate>2004-08-30T19:34:43-07:00</OrderDate> <OrderPostedDate>2004-08-30T19:34:52-07:00</OrderPostedDate> - <BillingData> <BuyerEmailAddress>dadolf3@comcast.net</BuyerEmailAddress> <BuyerName>Douglas Adolf</BuyerName> <BuyerPhoneNumber>(505)298-2270</BuyerPhoneNumber> </BillingData> - <FulfillmentData> <FulfillmentMethod>Ship</FulfillmentMethod> <FulfillmentServiceLevel>Standard</FulfillmentServiceLevel> - <Address> <Name>Douglas Adolf</Name> <AddressFieldOne>11105 Double Eagle NE</AddressFieldOne> <City>Albuquerque</City> <StateOrRegion>NM</StateOrRegion> <PostalCode>87111</PostalCode> <CountryCode>US</CountryCode> <PhoneNumber>(505)298-2270</PhoneNumber> </Address> </FulfillmentData> - <Item> <AmazonOrderItemCode>13487926545166</AmazonOrderItemCode> <SKU>11320221E</SKU> - <Title> Prince Triple Threat Scream OS Grip Size: 5/8 Tennis Racquet </Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> - <ItemPrice> - <Component> <Type>Principal</Type> <Amount currency="USD">89.95</Amount> </Component> - <Component> <Type>Shipping</Type> <Amount currency="USD">1.39</Amount> </Component> - <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> - <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> - <ItemFees> - <Fee> <Type>Commission</Type> <Amount currency="USD">-13.49</Amount> </Fee> </ItemFees> - <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> - <Component> <Type>Shipping</Type> <Amount currency="USD">-1.39</Amount> </Component> </Promotion> </Item> - <Item> <AmazonOrderItemCode>51817123432614</AmazonOrderItemCode> <SKU>3151016/DarkGray</SKU> <Title>Wilson H2Overgrip (3) Color: Dark Gray</Title> <Quantity>2</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> - <ItemPrice> - <Component> <Type>Principal</Type> <Amount currency="USD">5.98</Amount> </Component> - <Component> <Type>Shipping</Type> <Amount currency="USD">2.78</Amount> </Component> - <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> - <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> - <ItemFees> - <Fee> <Type>Commission</Type> <Amount currency="USD">-0.90</Amount> </Fee> </ItemFees> - <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> - <Component> <Type>Shipping</Type> <Amount currency="USD">-2.78</Amount> </Component> </Promotion> </Item> - <Item> <AmazonOrderItemCode>16079481441886</AmazonOrderItemCode> <SKU>3132018/Black</SKU> <Title>Duratac Color: Black Replacement Grip</Title> <Quantity>2</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> - <ItemPrice> - <Component> <Type>Principal</Type> <Amount currency="USD">7.98</Amount> </Component> - <Component> <Type>Shipping</Type> <Amount currency="USD">2.78</Amount> </Component> - <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> - <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> - <ItemFees> - <Fee> <Type>Commission</Type> <Amount currency="USD">-1.20</Amount> </Fee> </ItemFees> - <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> - <Component> <Type>Shipping</Type> <Amount currency="USD">-2.78</Amount> </Component> </Promotion> </Item> </OrderReport> </Message> - <Message> <MessageID>2</MessageID> - <OrderReport> <AmazonOrderID>058-3665855-3201150</AmazonOrderID> <AmazonSessionID>104-4395839-5827108</AmazonSessionID> <OrderDate>2004-08-30T16:59:10-07:00</OrderDate> <OrderPostedDate>2004-08-30T19:38:32-07:00</OrderPostedDate> - <BillingData> <BuyerEmailAddress>dancassey@cox.net</BuyerEmailAddress> <BuyerName>Daniel A. Cassey</BuyerName> <BuyerPhoneNumber>623-374-7116</BuyerPhoneNumber> </BillingData> - <FulfillmentData> <FulfillmentMethod>Ship</FulfillmentMethod> <FulfillmentServiceLevel>Standard</FulfillmentServiceLevel> - <Address> <Name>Daniel A. Cassey</Name> <AddressFieldOne>41004 N. Congressional Dr.</AddressFieldOne> <City>Anthem</City> <StateOrRegion>AZ</StateOrRegion> <PostalCode>85086</PostalCode> <CountryCode>US</CountryCode> <PhoneNumber>623-374-7116</PhoneNumber> </Address> </FulfillmentData> - <Item> <AmazonOrderItemCode>62029783008806</AmazonOrderItemCode> <SKU>215101719B</SKU> - <Title> Wilson Fli-By Tennis Shoes Mens Black/Off White/Black - S1360 Size: 9.5 </Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> - <ItemPrice> - <Component> <Type>Principal</Type> <Amount currency="USD">44.95</Amount> </Component> - <Component> <Type>Shipping</Type> <Amount currency="USD">3.48</Amount> </Component> - <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> - <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> - <ItemFees> - <Fee> <Type>Commission</Type> <Amount currency="USD">-7.26</Amount> </Fee> </ItemFees> </Item> - <Item> <AmazonOrderItemCode>30539998874590</AmazonOrderItemCode> <SKU>215101729B</SKU> - <Title> Wilson Fli-By Tennis Shoes Mens White/ Pewter/Red - S1361 Size: 9.5 </Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> - <ItemPrice> - <Component> <Type>Principal</Type> <Amount currency="USD">44.95</Amount> </Component> - <Component> <Type>Shipping</Type> <Amount currency="USD">3.47</Amount> </Component> - <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> - <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> - <ItemFees> - <Fee> <Type>Commission</Type> <Amount currency="USD">-7.26</Amount> </Fee> </ItemFees> </Item> </OrderReport> </Message> </AmazonEnvelope> I want to add the Shipping Totals for each of the different orders (Message) into one total that I store in the Orders Table of my DB. I guess I need to use recursion, but nothing I have read on this is making any sense to me. It also does not help that they are using a <Component> tag then a <Type> to identify what each pricing item is. How do I return just the Shipping value, then add them up for a grand total? (Better formated example available on request) Also, while I'm at it. Is there a way to setup a sort of auto-increment number? I have used (xsl:value-of select="position()" />, but this is reset to 1 when the master node changes, and I would instead like to continue with the numbers in order. |
|
#2
|
|||
|
|||
|
Yeah it seems recursion is what you need. You have 2 templates, the first one is the recursive one and the second one carries out what you need to do. But the important thing is that the second one has parameters for the information you need to change.
So in the first template, you set your parameter to 0, put in a loop to go through each node you have, calling the second template (passing the parameter) within the loop & getting the total. Set the parameter equal to itself + the total. If there's a second node, it will loop a second time but this time calling the second template and passing a different parameter value (0 + the total of first loop). And this will continue for how ever many nodes you need to process. You'll no doubt have to use your XPath well in order to get the right nodes. I hope this helps in some way...... |
|
#3
|
|||
|
|||
|
Re: Help with creating XSLT
ok, I'm still having problems here as I'm a newbie with XSLT....
Let us work through this..... I have this XSLT.... <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl utput method="html" indent="yes"/><xsl:include href="Style1.xsl"/> <xsl:template match="/"> <!-- Call the cosmetic formatting template --> <xsl:call-template name="Style1"/> <html><body> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="AmazonEnvelope/Message"> <h1>Order Number: <xsl:value-of select="OrderReport/AmazonOrderID" /></h1> <xsl:apply-templates select="OrderReport/Item/ItemPrice" /> </xsl:template> <xsl:template match="AmazonEnvelope/Message/OrderReport/Item/ItemPrice"> <xsl:variable name="MyTotal"> <xsl:call-template name="TotalShipping"> <xsl:with-param name="componentList" select="//Component/Type" /> <xsl:with-param name="Amount" select="//Component/Amount" /> </xsl:call-template> </xsl:variable> Testing Total <xsl:value-of select="$MyTotal" /> </xsl:template> <xsl:template name="TotalShipping"> <xsl aram name="componentList" /><xsl aram name="Amount" /><xsl aram name="Total" select="0" /><xsl:choose> <xsl:when test="$componentList"> <!-- Store Components into another variable --> <xsl:variable name="remainingComponents" select="$componentList[position() != 1]" /> <xsl:variable name="remainingAmounts" select="$Amount[position() != 1]" /> <!-- If Component = Shipping Then Add Amount to Total --> <xsl:variable name="newTotal"> <xsl:choose> <xsl:when test="$componentList[1] = 'Shipping'"> <xsl:value-of select="$Total + $Amount" /> </xsl:when> <xsl therwise><xsl:value-of select="$Total" /> </xsl therwise> </xsl:choose> </xsl:variable> <!-- Recursivly call the current template with the new total --> <xsl:call-template name="TotalShipping"> <xsl:with-param name="Component" select="$remainingComponents" /> <xsl:with-param name="Amount" select="$remainingAmounts" /> <xsl:with-param name="Total" select="$newTotal" /> </xsl:call-template> This is inside the Component List<xsl:value-of select="$componentList" /><br /> </xsl:when> <xsl therwise><!-- This should be the shipping total --> <xsl:value-of select="$Total" /> This is inside the Otherwise<xsl:value-of select="$componentList" /><br /> </xsl therwise> </xsl:choose> </xsl:template> </xsl:stylesheet> But I'm always getting a 0 total. I need to look at the Type in the component and only add the amount if the type is shipping. Here is a small bit of the XML: <Item> <AmazonOrderItemCode>44968472139374</AmazonOrderItemCode> <SKU>31510331/Natural</SKU> <Title>Wilson Sensation Size: 16G Color: Natural Tennis String</Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> <ItemPrice> <Component> <Type>Principal</Type> <Amount currency="USD">7.99</Amount> </Component> <Component> <Type>Shipping</Type> <Amount currency="USD">2.32</Amount> </Component> <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> <ItemFees> <Fee> <Type>Commission</Type> <Amount currency="USD">-1.20</Amount> </Fee> </ItemFees> <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> <Component> <Type>Shipping</Type> <Amount currency="USD">-2.32</Amount> </Component> </Promotion> </Item> <Item> <AmazonOrderItemCode>47521631197630</AmazonOrderItemCode> <SKU>2131016B11B</SKU> <Title>New Balance Ladies CT 820 B Tennis Shoes Size: 10</Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> <ItemPrice> <Component> <Type>Principal</Type> <Amount currency="USD">67.95</Amount> </Component> <Component> <Type>Shipping</Type> <Amount currency="USD">2.31</Amount> </Component> <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> <ItemFees> <Fee> <Type>Commission</Type> <Amount currency="USD">-10.19</Amount> </Fee> </ItemFees> <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> <Component> <Type>Shipping</Type> <Amount currency="USD">-2.31</Amount> </Component> </Promotion> </Item> <Item> <AmazonOrderItemCode>09196731372550</AmazonOrderItemCode> <SKU>9151013TE</SKU> <Title>Wilson Total Eclipse Hat - Z1350</Title> <Quantity>1</Quantity> <ProductTaxCode>A_GEN_NOTAX</ProductTaxCode> <ItemPrice> <Component> <Type>Principal</Type> <Amount currency="USD">24.95</Amount> </Component> <Component> <Type>Shipping</Type> <Amount currency="USD">2.32</Amount> </Component> <Component> <Type>Tax</Type> <Amount currency="USD">0.00</Amount> </Component> <Component> <Type>ShippingTax</Type> <Amount currency="USD">0.00</Amount> </Component> </ItemPrice> <ItemFees> <Fee> <Type>Commission</Type> <Amount currency="USD">-3.74</Amount> </Fee> </ItemFees> <Promotion> <PromotionClaimCode>_SITE_WIDE_</PromotionClaimCode> <MerchantPromotionID>AugFreeShipping</MerchantPromotionID> <Component> <Type>Shipping</Type> <Amount currency="USD">-2.32</Amount> </Component> </Promotion> </Item> This has 3 Item nodes that must be looked at, then in the ItemPrice Node there are 4 Component Nodes (I only need the one with a Type = Shipping). So I need to add the 3 Item/ItemPrice/Component/Amount fields where the Type = Shipping. Once I have that figured out, then I'll need to look at the Promotion node to see if shipping needs to be backed out..... Once again I apologize if this seems basic to anyone, but I am new to XSLT. |
|
#4
|
|||
|
|||
|
Help with creating XSLT - Solution
ok, this seems to be the solution:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xslutput method="html" indent="yes"/> <xsl:include href="Style1.xsl"/> <xsl:template match="/"> <!-- Call the cosmetic formatting template --> <xsl:call-template name="Style1"/> <html><body> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="AmazonEnvelope/Message"> <h1>Order Number: <xsl:value-of select="OrderReport/AmazonOrderID" /></h1> Total Shipping: <xsl:value-of select="sum(OrderReport/Item/*/Component[Type='Shipping']/Amount)" /> </xsl:template> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Help with creating XSLT |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|