Reputation: 3
I'm working on a legacy eCommerce website thats running a really old version of ASPDotNetStoreFront, full disclaimer I'm not proficient in XML/XSL I'm just having to dive in and try and get code working by looking at other code.
Essentially some products are restricted in what quantities it can be sold in. The product page receives this as a comma separated string .e.g
"5,10,15,20"
I've set up a param below to gather this data, which works correctly
<xsl:param name="restrictedquantities">
<xsl:value-of select="/root/Products2/Product/RestrictedQuantities" />
</xsl:param>
From that I need to output the quantities as separate options in a select tag such as below
<select>
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="20">20</option>
</select>
I've managed to get it 98% working with the below code, most of the code i've found from other stack overflow questions and have been trying to patch it together,
<xsl:when test="$restrictedquantities != ''">
<select>
<xsl:call-template name="split">
<xsl:with-param name="s" select="$restrictedquantities" />
</xsl:call-template>
</select>
</xsl:when>
Then outside my template below i've created another template which splits the string via commas, and as it outputs i'm putting tags around the values.
<xsl:template name="split" xmlns="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="s" />
<xsl:param name="withcomma" select="false()" />
<xsl:choose>
<xsl:when test="contains($s, ',')">
<!-- if there is still a comma, call me again
with everything after the first comma... -->
<xsl:call-template name="split">
<xsl:with-param name="s" select="substring-after($s, ',')" />
<xsl:with-param name="withcomma" select="true()" />
</xsl:call-template>
<!-- ...and print afterwards the current part -->
<option value="<xsl:value-of select="substring-before($s, ',')" />"><xsl:value-of select="substring-before($s, ',')" /></option>
</xsl:when>
<xsl:otherwise>
<!-- No comma left in the remaining part: print the rest -->
<option value="<xsl:value-of select="$s" />"><xsl:value-of select="$s" /></option>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This results in the output below where it seems to output double quotes around my templates output rendering it broken.
Console output of my select tag I'm thinking I need to escape my code somehow but I'm unsure. I feel like I'm forcing XSL to do something that it wasn't meant to do.
Any help or alternatives would be fantastic Thanks
Upvotes: 0
Views: 139
Reputation: 70638
It's probably due to the confusing way you are trying to create the option
tags
<option value="<xsl:value-of select="substring-before($s, ',')" />"><xsl:value-of select="substring-before($s, ',')" /></option>
You are outputing text here, not creating new elements. You should really be doing this...
<option value="{substring-before($s, ',')}"><xsl:value-of select="substring-before($s, ',')" /></option>
Note the use of Attribute Value Templates (the curly braces) in creating the attributes.
Also note that you also don't need the xmlns="http://www.w3.org/1999/XSL/Transform"
on the "split" template. Indeed, it would cause an error if you left it in now, as it would mean the processor would treat option
as an xsl element, and complain because it didn't recognise it.
Anyway, try this template instead
<xsl:template name="split">
<xsl:param name="s" />
<xsl:param name="withcomma" select="false()" />
<xsl:choose>
<xsl:when test="contains($s, ',')">
<xsl:call-template name="split">
<xsl:with-param name="s" select="substring-after($s, ',')" />
<xsl:with-param name="withcomma" select="true()" />
</xsl:call-template>
<option value="{substring-before($s, ',')}"><xsl:value-of select="substring-before($s, ',')" /></option>
</xsl:when>
<xsl:otherwise>
<option value="{$s}"><xsl:value-of select="$s" /></option>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Upvotes: 1