Add node based on parameter of XML using XSL

tauntontower

New Member
My objective is to add a new element to the XML file if one of the current child elements is equal to a condition. If that condition is not met continue to use the current template of the original XML file. I have a sample XML file below which currently containts 2 ordered items. The overall structure of the XML file will remain the same but the number of items ordered will be variable which can add additional AlbumOrderItems as customer orders will be unique with each XML file. Original XML file\[code\]<AlbumOrder> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <NumItems>2</NumItems> <DateTime>03/14/2013 12:16 AM</DateTime> <AlbumOrderItem> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <PartnerOrderItemID>1</PartnerOrderItemID> <DateTime>03/14/2013 12:16 AM</DateTime> <ProductCategory>ALBUM</ProductCategory> <Quantity>2</Quantity> <ShipAddress> <FirstName>Joe</FirstName> <LastName>Black</LastName> </ShipAddress> </AlbumOrderItem><AlbumOrderItem> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <PartnerOrderItemID>2</PartnerOrderItemID> <DateTime>03/14/2013 12:16 AM</DateTime> <ProductCategory>CARD</ProductCategory> <Quantity>1</Quantity> <Package>10</Package> <NumPrints>10</NumPrints> <ShipAddress> <FirstName>Joe</FirstName> <LastName>Black</LastName> </ShipAddress> </AlbumOrderItem> </AlbumOrder>\[/code\]If the ProductCategory is equal to "ALBUM" then add a new element to AlbumOrderItem.The sample XML output below contains two new added elements titled:Package and NumPrints these were both added to the first AblumOrderItemDesired output XML\[code\]<AlbumOrder> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <NumItems>2</NumItems> <DateTime>03/14/2013 12:16 AM</DateTime> <AlbumOrderItem> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <PartnerOrderItemID>1</PartnerOrderItemID> <DateTime>03/14/2013 12:16 AM</DateTime> <ProductCategory>ALBUM</ProductCategory> <Quantity>2</Quantity> <Package>XY</Package> ****NODE to add <NumPrints>Z</NumPrints> ****NODE to add<ShipAddress> <FirstName>Joe</FirstName> <LastName>Black</LastName> </ShipAddress> </AlbumOrderItem><AlbumOrderItem> <PartnerCode>ABC Company</PartnerCode> <AffiliateCode>abcpro</AffiliateCode> <PartnerOrderID>449</PartnerOrderID> <PartnerOrderItemID>2</PartnerOrderItemID> <DateTime>03/14/2013 12:16 AM</DateTime> <ProductCategory>CARD</ProductCategory> <Quantity>1</Quantity> <Package>10</Package> <NumPrints>10</NumPrints> <ShipAddress> <FirstName>Joe</FirstName> <LastName>Black</LastName> </ShipAddress> </AlbumOrderItem> </AlbumOrder>\[/code\]I am currently working with the file below but it doesn't seem to apply the desired changes. It seems to just produce a full duplicate of the original XML probably basedon the default XSL template/rules. I'm not sure if there are syntax issues below especially with the template match parameters. I'm also not sure how to search or loop through all of the ProductCategory elements in the entire XML file. If you have any questions please let me know. Any help would be greatly appreciated in order to add nodes to AlbumOrderItem if ProductCategory is equal to ALBUM and to account for multiple AlbumOrderItems sections. Current XSL script\[code\] <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:param name="TESTname">PACK_TEST</xsl:param> <xsl:param name="TESTvalue"><xsl:value-of select="AlbumOrder/AlbumOrderItem/Package"/> </xsl:param> <xsl:output method="xml"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="AlbumOrderItem[ProductCategory=Album]"> <xsl:element name="{$TESTname}"><xsl:value-of select="$TESTvalue"/></xsl:element> <xsl:apply-templates select="node()|@*"/> </xsl:template> </xsl:stylesheet> \[/code\]
 
Back
Top