xslt:substring-before

Jul*_*lle 12 xslt substring

我有以下的xml代码:

<weather-code>14 3</weather-code>
<weather-code>12</weather-code>
<weather-code>7 3 78</weather-code>
Run Code Online (Sandbox Code Playgroud)

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

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

问题是,当没有空格时,substring之前不会返回任何内容.有什么简单的方法吗?

Ode*_*ded 23

你可以使用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>
Run Code Online (Sandbox Code Playgroud)


Led*_*und 23

你可以确保总有一个空间,也许不是最漂亮的,但至少它是紧凑的:)

<xsl:value-of select="substring-before( concat( weather-code, ' ' ) , ' ' )" />
Run Code Online (Sandbox Code Playgroud)