Reputation: 11
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
<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>
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 AblumOrderItem
Desired output XML
<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>
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 based on 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
<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>
Upvotes: 1
Views: 1271
Reputation: 101652
Treemonkey has demonstrated the bugs you had in your path. In case you are interested in how to insert those nodes in the particular location you indicated (before ShipAddress), this is how you could do it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />
<xsl:param name="TESTname">PACK_TEST</xsl:param>
<xsl:param name="TESTvalue">
<xsl:value-of select="AlbumOrder/AlbumOrderItem/Package"/>
</xsl:param>
<xsl:template match="@* | node()" name="Copy">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="AlbumOrderItem[ProductCategory = 'ALBUM']/ShipAddress">
<xsl:element name="{$TESTname}">
<xsl:value-of select="$TESTvalue" />
</xsl:element>
<xsl:call-template name="Copy" />
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Reputation: 2163
Missing quotes and wrong case see below
<xsl:template match="AlbumOrderItem[ProductCategory='ALBUM']">
Upvotes: 1