如何在for循环中分配变量并在XSLT 1.0中的for循环外部使用它

时间:2013-12-16 07:20:03

标签: xslt-1.0

我有一个xml看起来像这样

<component name="main">
    <component name="sub">
        <component name="a">
             <issues>
                <warning> </warning>
             </issues>
        </component>
        <component name="b">
             <issues>
                 <warning> </warning>
             </issues>
        </component>
        <component name="c">
           <issues>
              <error> </error>
           </issues>
        </component>
     </component>
 </component>

我希望输出看起来像下面的

"main": [
    {
        composite:"sub"
        result: "failure"
    }
]

结果可以有3个值。其中列出如下 成功:没有错误没有警告 失败:错误超过零 不稳定:零错误超过零警告

结果应基于子组件结果。在上面的示例中,“sub”的结果失败,因为其子组件c之一有错误标记。

我的代码如下所示。它没有给我想要的结果。请帮帮我

    <xsl:param name="error">0</xsl:param>
<xsl:param name="warning">0</xsl:param>
<xsl:param name="Success">0</xsl:param>
<xsl:for-each select="./components/components">     
<xsl:if test="./issues/error">        
<xsl:param name="error" select="'1'" />     
</xsl:if>
<xsl:if test= "./issues/warning">   
<xsl:param name="warning" select="'1'" />       
</xsl:if>
<xsl:if test= "not(./issues/warning) and not(./issues/error)">  
<xsl:param name="Success" select="'1'" />       
</xsl:if>
</xsl:for-each> 
<xsl:if test= "$error = '1'">
"result":"Failure"
</xsl:if>
<xsl:if test="$error !='1' and $warning = '1'"> 
"result":"Unstable"
</xsl:if>
<xsl:if test="$error !='1' and $warning != '1' and $warning = '1'"> 
"result":"Success"
</xsl:if>   

1 个答案:

答案 0 :(得分:0)

无需变量:

<xsl:for-each select="./components">
  <xsl:text>"result":</xsl:text>
  <xsl:choose>
    <xsl:when test="count(./components/issues/error) &gt; 1">
      <xsl:text>"Failure"</xsl:text>
    </xsl:when>
    <xsl:when test="count(./components/issues/warning) &gt; 1">
      <xsl:text>"Unstable"</xsl:text>
    </xsl:when>
    <xsl:otherwise>
      <xsl:text>"Success"</xsl:text>
    </xsl:otherwise>
  </xsl:choose>
</xsl:for-each>
相关问题