我想更改图像路径。
显示图像的代码为:
<xsl:value-of select="image"/>
它从提要中采取的图像路径是:
https://www.website.com/media/catalog/product/1/7/image.jpg
我想要的是:
https://www.website.com/media/catalog/product/thumbnail/folder/1/7/image.jpg
我尝试了以下内容:
<xsl:template name="string-replace-all">
<xsl:param name="text"/>
<xsl:param name="replace"/>
<xsl:param name="with"/>
<xsl:choose>
<xsl:when test="contains($text,$replace)">
<xsl:value-of select="substring-before($text,$replace)"/>
<xsl:value-of select="$with"/>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)"/>
<xsl:with-param name="replace" select="$replace"/>
<xsl:with-param name="with" select="$with"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="image"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="root">
<xsl:for-each select="product[position() = 1]">
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="'https://www.website.com/media/catalog/product/'"/>
<xsl:with-param name="replace" select="'product/'" />
<xsl:with-param name="with" select="'product/thumbnail/folder/'"/>
</xsl:call-template>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
输出为:
https://www.website.com/media/catalog/product/thumbnail/folder/https://www.website.com/media/catalog/product/image.jpg
没有收到真正的错误消息,它只是显示了两倍的HTTP Part
这难道不简单吗?例如,考虑:
XML
<root>
<product>
<image>https://www.website.com/media/catalog/product/1/7/image.jpg</image>
</product>
<product>
<image>https://www.website.com/media/catalog/product/2/56/image.jpg</image>
</product>
<product>
<image>https://www.website.com/media/catalog/product/3/123/image.jpg</image>
</product>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<images>
<xsl:for-each select="product">
<image>
<xsl:text>https://www.website.com/media/catalog/product/thumbnail/folder/</xsl:text>
<xsl:value-of select="substring-after(image, 'https://www.website.com/media/catalog/product/')" />
</image>
</xsl:for-each>
</images>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<images>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/1/7/image.jpg</image>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/2/56/image.jpg</image>
<image>https://www.website.com/media/catalog/product/thumbnail/folder/3/123/image.jpg</image>
</images>