如何根据for-each循环中的属性值设置条件?

时间:2022-01-12 19:37:41

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

我在xsl中有一个循环,根据属性值,我想写一些文本。

XML is as follows:

XML是如下:

<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如下:

<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

我想要有以下输出: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.

如果有多个“MK”标记,则需要“&”。在“Row”标记的“Count”属性中可以找到“MK”标记的计数。

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

请告诉我如何在XSL中放入以下条件:

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

如果行标记中可用的Count Atrribute的值大于1,则显示“&”EndIF

Thanks

谢谢

2 个解决方案

#1


-1  

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>

#2


1  

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

当应用于提供的XML文档时(更正为格式良好):

<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

#1


-1  

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>

#2


1  

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

当应用于提供的XML文档时(更正为格式良好):

<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