简体   繁体   中英

How to place if condition based on attribute value within for-each loop

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

This transformation :

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM