本文介绍了xslt:子串之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xml 代码:

I have the folowing xml code:

<weather-code>14 3</weather-code>
<weather-code>12</weather-code>
<weather-code>7 3 78</weather-code>

现在我只想获取每个节点的第一个数字来设置背景图像.因此,对于每个节点,我都有以下 xslt:

Now i'd like to only grab the first number of each node to set a background image. So for each node i have the folowing xslt:

<xsl:attribute name="style">
  background-image:url('../icon_<xsl:value-of select="substring-before(weather-code, ' ')" />.png');
</xsl:attribute>

问题是之前的子字符串在没有空格时不返回任何内容.有什么简单的方法可以解决这个问题吗?

Problem is that substring before doesn't return anything when there's no space. Any easy way around this?

推荐答案

您可以使用 xsl:whencontains:

<xsl:attribute name="style">
  <xsl:choose>
    <xsl:when test="contains(weather-code, ' ')">
      background-image:url('../icon_<xsl:value-of select="substring-before(weather-code, ' ')" />.png');
    </xsl:when>
    <xsl:otherwise>background-image:url('../icon_<xsl:value-of select="weather-code" />.png');</xsl:otherwise>
  </xsl:choose>
</xsl:attribute>

这篇关于xslt:子串之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:00