从逗号分隔的字符串[XML / XSL]创建选择下拉列表

时间:2018-06-26 10:41:35

标签: html asp.net xml xslt xslt-1.0

我正在一个旧的电子商务网站上运行,该网站运行的是ASPDotNetStoreFront的非常旧的版本,完整的免责声明我不精通XML / XSL,我只是必须深入研究并尝试通过查看其他代码来使代码工作代码。

基本上,某些产品的销售数量受到限制。产品页面以逗号分隔的字符串形式接收该信息。例如

"5,10,15,20"

我在下面设置了一个参数来收集这些数据,这些参数可以正常工作

 <xsl:param name="restrictedquantities">
      <xsl:value-of select="/root/Products2/Product/RestrictedQuantities" />
 </xsl:param>     

因此,我需要将数量作为选择标签中的单独选项输出,如下所示:

<select>
     <option value="5">5</option>
     <option value="10">10</option>
     <option value="15">15</option>
     <option value="20">20</option>
</select>

我已经设法使其98%的代码与下面的代码一起工作,我从其他堆栈溢出问题中找到的大部分代码都在尝试将其修补在一起,

<xsl:when test="$restrictedquantities != ''">
    <select>
        <xsl:call-template name="split">
            <xsl:with-param name="s" select="$restrictedquantities" />
        </xsl:call-template>
     </select>
</xsl:when>

然后在下面的模板之外,我创建了另一个模板,该模板通过逗号分隔字符串,并且在输出时,我将标签放在值周围。

<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 -->
        &lt;option value="<xsl:value-of select="substring-before($s, ',')" />"&gt;<xsl:value-of select="substring-before($s, ',')" />&lt;/option&gt;
      </xsl:when>
      <xsl:otherwise>
        <!-- No comma left in the remaining part: print the rest -->
        &lt;option value="<xsl:value-of select="$s" />"&gt;<xsl:value-of select="$s" />&lt;/option&gt;
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

这会导致下面的输出,在我的模板输出周围似乎输出双引号,使它损坏。

Console output of my select tag 我以为我需要以某种方式转义我的代码,但不确定。我觉得我正在强迫XSL做一些原本不想做的事情。

任何帮助或替代方案都是很棒的 谢谢

1 个答案:

答案 0 :(得分:1)

这可能是由于您尝试创建option标签的方式令人困惑

 &lt;option value="<xsl:value-of select="substring-before($s, ',')" />"&gt;<xsl:value-of select="substring-before($s, ',')" />&lt;/option&gt;

您将在此处输出文本,而不是创建新元素。你真的应该这样做...

<option value="{substring-before($s, ',')}"><xsl:value-of select="substring-before($s, ',')" /></option>

请注意在创建属性时使用Attribute Value Templates(花括号)。

还请注意,您也不需要“拆分”模板上的xmlns="http://www.w3.org/1999/XSL/Transform"。确实,如果您现在将其留在原处,将导致错误,因为这将意味着处理器会将option视为xsl元素,并会因为无法识别而抱怨。

无论如何,请尝试使用此模板

<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>