Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

xslt: substring-before

I have the folowing xml code:

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use xsl:when and contains:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...