EDN Admin
Well-known member
Hey,
I am new to XSLT and finding way make look up from external text file. I am using XSLT to transform XML to text file. I have a requirement to map external image name if the SKU, exists in my lookup file.
Example:- I have SKUs 1, 2, 3, 4, 5 in XML, on of the SKU 2 has some problem with image, for those SKUs I want to map corrected image from lookup file by mapping SKU(I already has list of these images based on SKU, need to map images names from lookup file
on finding matching SKUs else populate the image <largeFrontImage> element from XML).
Can some one help me on this.
Following is the XML snippet
--------------------------------------------------------------------------------------------------------
<products><br/>
<product><sku>4776078</sku><br/>
<name>Acer - 10.1" Aspire One Netbook - 1GB Memory - 320GB Hard Drive - Espresso Black</name><br/>
<regularPrice>268.98</regularPrice><br/>
<largeFrontImage>http://images.bestbuy.com/BestBuy_US/images/products/4776/4776078_sa.jpg</largeFrontImage><br/>
<image>http://images.bestbuy.com/BestBuy_US/images/products/4776/4776078_sc.jpg</image><br/>
<product><br/>
</products>
------------------------------------------------------------------------------------------------------------------
My XSLT
---------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?><br/>
<br/>
<!-- New document created with EditiX at Thu Feb 02 17:16:58 GMT+05:30 2012 --><br/>
<br/>
<xsl:stylesheet version="2.0"<br/>
<br/>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<br/>
<br/>
xmlns:xs="http://www.w3.org/2001/XMLSchema"<br/>
xmlns:fn="http://www.w3.org/2005/xpath-functions"<br/>
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"<br/>
xmlns:err="http://www.w3.org/2005/xqt-errors"<br/>
exclude-result-prefixes="xs xdt err fn <br/>
<xslutput method="text" indent="yes"/><br/>
<br/>
<br/>
<xslaram name="separator" select="	"/><br/>
<xslaram name="empty" select=""/><br/>
<xslaram name="line-separator" select=" "/><br/>
<br/>
<br/>
<xsl:template match="/ <br/>
<xsl:apply-templates select="//products"/><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="300x250"/><br/>
</xsl:apply-templates><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="728x90"/><br/>
</xsl:apply-templates><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="160x600"/><br/>
</xsl:apply-templates><br/>
<br/>
</xsl:template><br/>
<br/>
<br/>
<br/>
<xsl:template match="products <br/>
<xsl:text>PRODUCT_SKU	PRODUCT_NAME	PRICE	IMAGE_URL</xsl:text>
<br/>
<xsl:value-of select="$line-separator"/>
<br/>
</xsl:template><br/>
<br/>
<xsl:template match="product <br/>
<xslaram name="adsize"/><br/>
<xsl:if test="(manufacturer=Acer) or (manufacturer=Asus) or (manufacturer=ASUS) or (manufacturer=Dell) or (manufacturer=HP) or (manufacturer=Lenovo) or (manufacturer=Samsung) or (manufacturer=Sony) or (manufacturer=Toshiba) or (manufacturer=Apple®
or (manufacturer=Apple) or (manufacturer=Gateway) <br/>
<br/>
<br/>
<xsl:value-of select="sku"/><br/>
<xsl:value-of select="$separator"/><br/>
<xsl:value-of select="translate(./name,
, )"/><br/>
<xsl:value-of select="$separator"/><br/>
<xsl:value-of select="regularPrice"/><br/>
<xsl:value-of select="$separator"/><br/>
<br/>
<xsl:apply-templates select ="largeFrontImage" /><br/>
<xsl:value-of select="$line-separator"/><br/>
<br/>
<br/>
</xsl:if> <br/>
<br/>
<br/>
</xsl:template><br/>
<br/>
<br/>
<br/>
<xsl:template match="includedItem <br/>
<xsl:value-of select=translate(.,"
","")/><br/>
<xsl:if test="position() < last() <br/>
<xsl:text>**</xsl:text><br/>
</xsl:if><br/>
</xsl:template><br/>
<br/>
<br/>
<xsl:template match="largeFrontImage <br/>
<xsl:choose><br/>
<xsl:when test =". = $empty <br/>
<xsl:value-of select="../image"/><br/>
</xsl:when><br/>
<xsltherwise><br/>
<xsl:value-of select="."/><br/>
</xsltherwise><br/>
</xsl:choose><br/>
<br/>
</xsl:template><br/>
<br/>
</xsl:stylesheet>
----------------------------------------------------------------------------------------
View the full article
I am new to XSLT and finding way make look up from external text file. I am using XSLT to transform XML to text file. I have a requirement to map external image name if the SKU, exists in my lookup file.
Example:- I have SKUs 1, 2, 3, 4, 5 in XML, on of the SKU 2 has some problem with image, for those SKUs I want to map corrected image from lookup file by mapping SKU(I already has list of these images based on SKU, need to map images names from lookup file
on finding matching SKUs else populate the image <largeFrontImage> element from XML).
Can some one help me on this.
Following is the XML snippet
--------------------------------------------------------------------------------------------------------
<products><br/>
<product><sku>4776078</sku><br/>
<name>Acer - 10.1" Aspire One Netbook - 1GB Memory - 320GB Hard Drive - Espresso Black</name><br/>
<regularPrice>268.98</regularPrice><br/>
<largeFrontImage>http://images.bestbuy.com/BestBuy_US/images/products/4776/4776078_sa.jpg</largeFrontImage><br/>
<image>http://images.bestbuy.com/BestBuy_US/images/products/4776/4776078_sc.jpg</image><br/>
<product><br/>
</products>
------------------------------------------------------------------------------------------------------------------
My XSLT
---------------------------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8" ?><br/>
<br/>
<!-- New document created with EditiX at Thu Feb 02 17:16:58 GMT+05:30 2012 --><br/>
<br/>
<xsl:stylesheet version="2.0"<br/>
<br/>
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"<br/>
<br/>
xmlns:xs="http://www.w3.org/2001/XMLSchema"<br/>
xmlns:fn="http://www.w3.org/2005/xpath-functions"<br/>
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"<br/>
xmlns:err="http://www.w3.org/2005/xqt-errors"<br/>
exclude-result-prefixes="xs xdt err fn <br/>
<xslutput method="text" indent="yes"/><br/>
<br/>
<br/>
<xslaram name="separator" select="	"/><br/>
<xslaram name="empty" select=""/><br/>
<xslaram name="line-separator" select=" "/><br/>
<br/>
<br/>
<xsl:template match="/ <br/>
<xsl:apply-templates select="//products"/><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="300x250"/><br/>
</xsl:apply-templates><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="728x90"/><br/>
</xsl:apply-templates><br/>
<br/>
<br/>
<xsl:apply-templates select="//product <br/>
<xsl:with-param name="adsize" select="160x600"/><br/>
</xsl:apply-templates><br/>
<br/>
</xsl:template><br/>
<br/>
<br/>
<br/>
<xsl:template match="products <br/>
<xsl:text>PRODUCT_SKU	PRODUCT_NAME	PRICE	IMAGE_URL</xsl:text>
<br/>
<xsl:value-of select="$line-separator"/>
<br/>
</xsl:template><br/>
<br/>
<xsl:template match="product <br/>
<xslaram name="adsize"/><br/>
<xsl:if test="(manufacturer=Acer) or (manufacturer=Asus) or (manufacturer=ASUS) or (manufacturer=Dell) or (manufacturer=HP) or (manufacturer=Lenovo) or (manufacturer=Samsung) or (manufacturer=Sony) or (manufacturer=Toshiba) or (manufacturer=Apple®
or (manufacturer=Apple) or (manufacturer=Gateway) <br/>
<br/>
<br/>
<xsl:value-of select="sku"/><br/>
<xsl:value-of select="$separator"/><br/>
<xsl:value-of select="translate(./name,
, )"/><br/>
<xsl:value-of select="$separator"/><br/>
<xsl:value-of select="regularPrice"/><br/>
<xsl:value-of select="$separator"/><br/>
<br/>
<xsl:apply-templates select ="largeFrontImage" /><br/>
<xsl:value-of select="$line-separator"/><br/>
<br/>
<br/>
</xsl:if> <br/>
<br/>
<br/>
</xsl:template><br/>
<br/>
<br/>
<br/>
<xsl:template match="includedItem <br/>
<xsl:value-of select=translate(.,"
","")/><br/>
<xsl:if test="position() < last() <br/>
<xsl:text>**</xsl:text><br/>
</xsl:if><br/>
</xsl:template><br/>
<br/>
<br/>
<xsl:template match="largeFrontImage <br/>
<xsl:choose><br/>
<xsl:when test =". = $empty <br/>
<xsl:value-of select="../image"/><br/>
</xsl:when><br/>
<xsltherwise><br/>
<xsl:value-of select="."/><br/>
</xsltherwise><br/>
</xsl:choose><br/>
<br/>
</xsl:template><br/>
<br/>
</xsl:stylesheet>
----------------------------------------------------------------------------------------
View the full article