2013-03-13 163 views
0

我是比较新的XSLT的,我想要做的事,如果nodecount不等于0,但XSLT不工作,如果条件XSLT如果条件没有节点

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<link href="CSS.css" rel="stylesheet" type="text/css" /> 
</head> 

<body> 
<div class="main_cont"> 
<div class="page3"> 
<xsl:if test="(Count(/PrecisionVestor/OtherIncomeSection/OtherIncome) != 0)"> 
    <div class="rent_roll"> 


    <div class="othr_inc"> 
     <div class="fst_r_box"> 
     <div class="fst_r_box_fst_row" style="width:100%; height:30px; float:left;"> 
      <div class="item" style="width:25%; height:30px; float:left; line-height:30px;">Description</div> 
      <div class="cost" style="width:18%; height:30px; float:left; text-align:center; line-height:30px;">Monthly Rent</div> 
      <div class="cost" style="width:18%; height:30px; float:left; text-align:center; line-height:30px;">Lease Start</div> 
      <div class="cost" style="width:18%; height:30px; float:left; text-align:center; line-height:30px;">Lease End</div> 
      <div class="cost" style="width:18%; height:30px; float:left; text-align:center; line-height:30px; ">Escalator</div> 
     </div> 
     <xsl:for-each select="PrecisionVestor/OtherIncomeSection/OtherIncome"> 
     <div class="fst_r_box_fst_row_inn"> 
      <div class="item_b" style="width:25%; "><xsl:value-of select="Description"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="MonthlyRent"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="LeaseStart"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="LeaseEnd"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="Escalator"/></div> 
     </div> 
     </xsl:for-each> 

     <xsl:for-each select="PrecisionVestor/OtherIncomeSection/TotalOtherIncome">  
     <div class="fst_r_box_btm_row"> 
      <div class="item_b" style="width:25%; "><xsl:value-of select="Description"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="MonthlyRent"/></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"></div> 
      <div class="cost_b" style="width:12%; margin-right:6%; text-align:right; float:left;"><xsl:value-of select="Escalator"/></div> 
     </div> 
     </xsl:for-each> 
     </div> 

    </div> 
</xsl:if> 
</div> 
</div> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 

下面是XML XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="xslStyle1.xsl"?> 
<PrecisionVestor> 
    <OtherIncomeSection> 
     <OtherIncome> 
      <Description>Rent</Description> 
      <MonthlyRent>200</MonthlyRent> 
      <LeaseStart>3/12/13</LeaseStart> 
      <LeaseEnd>3/12/14</LeaseEnd> 
      <Escalator>2</Escalator> 
     </OtherIncome> 
     <TotalOtherIncome> 
      <Description>Totals</Description> 
      <MonthlyRent>200</MonthlyRent> 
      <Escalator>2</Escalator> 
     </TotalOtherIncome> 
    </OtherIncomeSection> 
</PrecisionVestor> 

这是工作的罚款,直到我说,如果条件一旦我添加的,如果条件我收到空白的HTML

回答

3

的一个问题是,你是在用Count大写C时,SH应该是:

<xsl:if test="count(/PrecisionVestor/OtherIncomeSection/OtherIncome) != 0"> 

但是count()实际上并不需要检查节点的存在。你可以不是简单地做到这一点:

<xsl:if test="/PrecisionVestor/OtherIncomeSection/OtherIncome"> 

,导致该XSLT失败的另一个问题是,你缺少的<xsl:if>标签之前关闭</div>标签。如果这两个问题都解决了,那么XSLT应该运行。


我也建议整理的XSLT了一下,像这样:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" /> 
    <xsl:template match="/"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <title>Untitled Document</title> 
     <link href="CSS.css" rel="stylesheet" type="text/css" /> 
     <style> 
      .item, .item_b { width: 25%; } 
      .item, .cost { height: 30px; float:left; line-height: 30px; } 
      .cost { width: 18%; text-align: center; } 
      .cost_b { width: 12%; margin-right: 6%; text-align: right; float: left; } 
     </style> 
     </head> 

     <body> 
     <div class="main_cont"> 
      <div class="page3"> 
      <xsl:apply-templates select="PrecisionVestor/OtherIncomeSection[OtherIncome]" /> 
      </div> 
     </div> 
     </body> 
    </html> 
    </xsl:template> 

    <xsl:template match="OtherIncomeSection"> 
    <div class="rent_roll"> 

     <div class="othr_inc"> 
     <div class="fst_r_box"> 
      <div class="fst_r_box_fst_row" style="width:100%; height:30px; float:left;"> 
      <div class="item">Description</div> 
      <div class="cost">Monthly Rent</div> 
      <div class="cost">Lease Start</div> 
      <div class="cost">Lease End</div> 
      <div class="cost">Escalator</div> 
      </div> 
      <xsl:apply-templates select="OtherIncome"> 
      <xsl:with-param name="rowClass" select="'fst_r_box_fst_row_inn'" /> 
      </xsl:apply-templates> 
      <xsl:apply-templates select="OtherIncome"> 
      <xsl:with-param name="rowClass" select="'fst_r_box_btm_row'" /> 
      </xsl:apply-templates> 
     </div> 

     </div> 
    </div> 
    </xsl:template> 

    <xsl:template match="OtherIncome | TotalOtherIncome"> 
    <xsl:param name="rowClass" /> 
    <div class="$rowClass"> 
     <xsl:apply-templates select="Description | MonthlyRent" /> 
     <xsl:apply-templates select="LeaseStart | LeaseEnd" /> 
     <xsl:apply-templates select="self::TotalOtherIncome" mode="leaseColumns" /> 
     <xsl:apply-templates select="Escalator" /> 
    </div> 
    </xsl:template> 

    <xsl:template match="Description"> 
    <div class="item_b"> 
     <xsl:value-of select="."/> 
    </div> 
    </xsl:template> 

    <xsl:template match="MonthlyRent | LeaseStart | LeaseEnd | Escalator"> 
    <div class="cost_b"> 
     <xsl:value-of select="."/> 
    </div> 
    </xsl:template> 

    <xsl:template match="TotalOtherIncome" mode="leaseColumns"> 
    <div class="cost_b"></div> 
    <div class="cost_b"></div> 
    </xsl:template> 
</xsl:stylesheet>