tags:

views:

76

answers:

2

I have a loop in xsl and depending upon the attribute value, i want to write some text.

XML is as follows:

<ROWS><ROW oid="28439"><EFL eid="8" fid="27672" count="2">
 <MK id="3" val="0"/>
 <MK id="11" val="0578678             "/> </ROW></ROWS>

XSL is as follows:

<xsl:for-each select="EFL/MK">
         <xsl:value-of select="@id" />: 
        <xsl:value-of select="@val" />
      </xsl:for-each>

I want to have the following output:
3:0 & 11:057868

"&" is needed if there are more than one "MK" tags. Count of "MK" tags is available in "Count" attribute of "Row" tag.

Can you please tell me how to put the following if condition in XSL:

if Count Atrribute available in Row tag has a value greater than 1 Then Display "&" EndIF

Thanks

A: 

Try this code:

<xsl:for-each select="EFL/MK">
  <xsl:value-of select="@id" />: 
  <xsl:value-of select="@val" />
  <xsl:if test="../@count &gt; 1">
    <xsl:if test="not(position()=last())"><![CDATA[ &  ]]></xsl:if>
  </xsl:if>
</xsl:for-each>
antyrat
It's very rare that using CDATA is a good idea, and there's no evidence in this case that it's necessary.
Robert Rossney
+1  A: 

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="ROWS/ROW">
   <xsl:for-each select="EFL/MK">
    <xsl:value-of select=
     "concat(substring(' &amp; ', 1 div (position() > 1)),
             @id,
             ':',
             @val,
             substring('&#xA;', 1 div (position() = last()))
             )
     "/>
   </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document (corrected to be well-formed):

<ROWS>
 <ROW oid="28439">
   <EFL eid="8" fid="27672" count="2">
    <MK id="3" val="0"/>
    <MK id="11" val="0578678"/>
   </EFL>
 </ROW>
</ROWS>

produces the wanted, correct result:

3:0 & 11:0578678
Dimitre Novatchev