XSLT and a Commerce Server Catalog Query

I am becoming more and more convinced that you need to have good XSLT skills to work with the Commerce Server Adapters for BizTalk.  Well, I came across a situation that called for a custom XSLT solution.  I have written about some of these cases in past blog entries and as I find more situations I will add blog entries.

 

In this case, I needed to download images from a third party site for products in our catalog that were missing images.   I needed to query Commerce Server and pass in a list of items that I wanted to return.  In this case it was based on the VendorUPC element (as shown in the screen shot below).

 

 

 

 

 I needed to create the query so that it would look like:

 

<CommerceServerCatalogQuery ProductCatalog="Images" SearchClause="VendorUPC='100' Or VendorUPC='101' Or VendorUPC='102'"></CommerceServerCatalogQuery>

 

So, the issue was how do I append all of the VendorUPC elements to create the SearchClause.  This is where the custom XSLT comes in.

 

Here is the XSLT that produces the above xml output.

 

<xsl:attribute name="SearchClause">

                <xsl:variable name="ItemCount" select="count(/*[local-name()='Root' and namespace-uri()='https://Ecommerce.Schemas.ImageDownloadData']/ImageData)" />

                <xsl:choose>

                                <xsl:when test="$ItemCount = 0"></xsl:when>

                                <xsl:when test="$ItemCount = 1">VendorUPC='<xsl:value-of select="/*[local-name()='Root' and namespace-uri()='https://Ecommerce.Schemas.ImageDownloadData']/ImageData[1]/VendorUPC" />'</xsl:when>

                                <xsl:otherwise>VendorUPC='<xsl:value-of select="/*[local-name()='Root' and namespace-uri()='https://Ecommerce.Schemas.ImageDownloadData']/ImageData[1]/VendorUPC" />'

                                <xsl:for-each select="/*[local-name()='Root' and namespace-uri()='https://Ecommerce.Schemas.ImageDownloadData']/ImageData[position()>1]"> Or VendorUPC='<xsl:value-of select="VendorUPC" />'

                                </xsl:for-each></xsl:otherwise>

                </xsl:choose>

</xsl:attribute>

 

This can be used any time you need to iterate over a repeating source structure and either 'Or' or 'And' values together in a single output element.