用xsl绑定

时间:2009-05-26 21:37:52

标签: xml xslt xhtml

我开始认为问题不在于绑扎码。不知何故计数不正确。可能是在xml中,我有约。 150个案例研究节点?或者它可以按我执行for-each和if?

的顺序排列
<!-- XML -->
<case-studies>

    <case-study>    
        <name>Company A</name>
        <solutionType>Mobility</solutionType>
        <solutionType>Convergence</solutionType>
              <solution category="Business services">Product</solution>     
        <solution category="Business services">Industry</solution>      
        <solution category="#">A-Z</solution>       
        <product>Product 1</product> 
    </case-study>
</case-studies>

我正在尝试创建一个表格,在for-each中交替显示灰色行。下面的代码会返回一个不稳定的效果。

由于      

    <xsl:sort select="../name" />
    <xsl:if test="@category[. = $solName]">

        <tr>
        <xsl:if test="(position() mod 2 = 1)">
            <xsl:attribute name="bgcolor">#e7e7e7</xsl:attribute>                
        </xsl:if>    

          <td class="cell1">

            <img src="/images/icons/infoWhite.gif" style="margin:3px 3px 0 0px;" id="{../name}" onmouseover="xstooltip_show('{../url}', '{../name}', 0, 10);" onmouseout="xstooltip_hide('{../url}');" />
            <div id="{../url}" class="xstooltip" style="margin:10px 0 0 10px;">
              <div class="floatLeft"><strong>Product(s):</strong></div> 
              <div class="margLeft10 floatLeft">
                <xsl:for-each select="../product/prodName">     
                    <div class="clearRight"><xsl:value-of select="."/></div>
                </xsl:for-each>             
                </div>
            </div> 
          </td> 
          <td class="cell2" style="padding-top:2px;">» <a href="{../url}"><xsl:value-of select="../name"/></a></td>
          <td class="cell3">
            <xsl:for-each select="../solutionType">         
                <div class="clearRight"><xsl:value-of select="."/></div>
            </xsl:for-each>                
          </td>
        </tr>
    </xsl:if>        
</xsl:for-each>

3 个答案:

答案 0 :(得分:3)

This might work

<xsl:for-each select="NewDataSet/authors">
<xsl:sort select="au_lname"/>

<xsl:if test="position()  mod 2 = 1">
<tr bgcolor="#aaccff">
    <td><xsl:value-of select="au_lname"/></td>
    <td><xsl:value-of select="au_fname"/></td>
    <td><xsl:value-of select="phone"/></td>
</tr>
</xsl:if>

<xsl:if test="position()  mod 2 = 0">
<tr bgcolor="#ffccaa">
    <td><xsl:value-of select="au_lname"/></td>
    <td><xsl:value-of select="au_fname"/></td>
    <td><xsl:value-of select="phone"/></td>
</tr>
</xsl:if>

</xsl:for-each>

答案 1 :(得分:1)

查看您的XSLT,它表明您正在迭代“解决方案”元素。这是对的吗?

我认为问题在于,当你检查位置()时,无论先前的解决方案元素是否被条件忽略,它都会进入所有先前的解决方案元素。

我可以提出两种解决这个问题的可能性。

首先,尝试将position()上的测试更改为匹配先前元素数量的count()。例如:

<xsl:if test="(count(preceding-sibling::solution[@category=$solName]) mod 2 = 0)">

或者,您可以尝试将@category上的测试添加到并删除条件。例如:

<xsl:for-each select="solution[@category = $solName]">
    <xsl:sort select="../name" />
    <tr>
        <xsl:if test="(position() mod 2 = 1)">

我希望这是有道理的!

答案 2 :(得分:1)

问题是您需要使用xsl:elementxsl:attribute正常运行。您无法设置静态元素的属性。你必须让xslt为你生成元素。

<xsl:sort select="../name" />
  <xsl:if test="@category[. = $solName]">
    <!-- use xsl:element to create an element -->
    <xsl:element name="tr">
      <xsl:if test="position() mod 2 = 1">
        <!-- then xsl:attribute will function as expected -->
        <xsl:attribute name="bgcolor">#e7e7e7</xsl:attribute>
      </xsl:if>

      <!-- snip -->

    </xsl:element>
  </xsl:if>
</xsl:for-each>

我建议你使用类名和css(背景颜色)而不是内联样式:

<xsl:sort select="../name" />
  <xsl:if test="@category[. = $solName]">
    <!-- use xsl:element to create an element -->
    <xsl:element name="tr">
      <xsl:if test="position() mod 2 = 1">
        <!-- then xsl:attribute will function as expected -->
        <xsl:attribute name="class">alternateRow</xsl:attribute>
      </xsl:if>

      <!-- snip -->

    </xsl:element>
  </xsl:if>

示例CSS(针对我的备用解决方案):

.alternateRow { background-color: #e7e7e7; }

此外,您不需要在测试周围加上括号。如果我有多个测试,为了便于阅读,我将它们包装在括号中,但只有一个条件,不会添加parentehses。