开发者

Check the existence on an Attribute and non-emptiness of its value

开发者 https://www.devze.com 2023-01-07 23:52 出处:网络
<ROWS> <ROW oid=\"28439\"> <EFL eid=\"8\" fid=\"27672\" count=\"2\" Name = \"A : bbb\">
<ROWS>
     <ROW oid="28439">
        <EFL eid="8" fid="27672" count="2" Name = "A : bbb">
            <MK id="3" val="0"/>
            <MK id="11" val="0578678"/>
        </EFL>
   </ROW>
</ROWS>

I have the above xml, i want to have the following loop in XSL

if Name attribute in EFL tag Exists And Not Empty Then

      Display the value of Name attribute

Else

      do something (that i know how to write)

Endif

Please note that this IF Condition will be written within for-each loop on Row tag. So, th开发者_开发百科at 's why we can not use Match.

Thanks


Refer the following XSL. It works but havent tested corner cases.

<xsl:stylesheet version = '1.0'
 xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

 <xsl:template match="/ROWS">
     <xsl:for-each select="ROW">
         <xsl:choose>
             <xsl:when test='string-length(EFL/@Name)>0'>
                 <xsl:value-of select="EFL/@Name"/> 
             </xsl:when>

             <xsl:otherwise>I know  what to do here....</xsl:otherwise>
         </xsl:choose>
     </xsl:for-each>
 </xsl:template>


Alejandro's quite right; to extrapolate Tushar's example:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="text" indent="yes"/>

  <xsl:template match="/ROWS">
    <xsl:apply-templates select="ROW" />
  </xsl:template>

  <xsl:template match="ROW[string-length(EFL/@Name)>0]">
    <xsl:value-of select="EFL/@Name" />
  </xsl:template>

  <xsl:template match="ROW">
    <xsl:text>Something else..</xsl:text>
  </xsl:template>
</xsl:stylesheet>

Depending on the nature of your problem as a whole, this might be a better option; either is perfectly valid though. Strictly speaking in this example, even the template matching /ROWS is unnecessary, but it probably will be for anything more complex.

0

精彩评论

暂无评论...
验证码 换一张
取 消