0

Any one Comfortable with XML and XSLT here: In my XMLl I am trying to color two different "p" tags with different attributes using XSLT below:

<xsl:template match="p">
 <xsl:choose>
  <xsl: when test="p/@color='red'"><span style="color:#00ff00"><xsl:value-of select="p"/></span></xsl:when>
  <xsl: when test="p/@color='green'"> <span style="color:#ff0000"> <xsl:value-of select="p"/></span></xsl:when>
  <xsl: otherwise><xsl:value-of select="p"/></xsl:otherwise>
 </xsl:choose>
</xsl:template>

XML looks like this

<html>
<body>
<p color="red">Last paragraph incorrect</p>
<p>A Second paragraph</p>
<p color="green">The First paragraph</p>
<p color="green">Anthony Gonsalwes</p>
</body>
<body>
<l color="green"> kumarswamy</l>
</body>
</html>
U. Windl
  • 3,480
  • 26
  • 54

1 Answers1

2

Your template matches p. This establishes the context for the relative paths you use inside the template. It means that your condition:

<xsl:when test="p/@color='red'">

will be true only if the current p has a child p element with a color attribute with value of 'red'. This is of course not true for any p element in your XML. You should be testing instead:

<xsl:when test="@color='red'">

and instead of:

<xsl:value-of select="p"/> 

you need:

<xsl:value-of select="."/> 
y.arazim
  • 866
  • 2
  • 10