2013-02-17 35 views
0

我在XSLT文件中設置了樣式元素,因此所有樣式都在這裏完成。我想從最後一個列表元素中刪除邊框底部,但不知道如何在XSLT中應用此邊框。這裏是我的代碼:是否有可能使用內聯樣式的最後一個孩子而不是CSS文件?

   <xsl:element name="div"> 
       <xsl:attribute name="style"> 
        <xsl:text>width:120px; margin:0 auto; padding: 0; border: 1px solid black; border-radius: 15px;padding-bottom: 20px; background: #6A819E; margin-top: 20px;</xsl:text> 
       </xsl:attribute> 
       <xsl:element name="ul"> 
        <xsl:attribute name="style"> 
         <xsl:text>width:120px; margin:0 auto; padding: 0; background: #6A819E;</xsl:text> 
        </xsl:attribute> 
        <xsl:for-each select="flights/flight"> 
         <xsl:apply-templates select="route" /> 
        </xsl:for-each> 
       </xsl:element> 
      </xsl:element> 
     </xsl:element> 
    </xsl:element> 
</xsl:template> 


<xsl:template match="route"> 
    <xsl:element name="li"> 
     <xsl:attribute name="style"> 
      <xsl:text>list-style-type:none; width:120px; margin:0 auto; margin-top: 20px; border-bottom: 1px solid black; text-align:center; background: #6A819E;</xsl:text> 
      <xsl:if test="position() = last()">border: none;</xsl:if> 
     </xsl:attribute> 
     <a><xsl:attribute name="href">map.php?a=<xsl:value-of select="from/latitude" />&amp;b=<xsl:value-of select="from/longitude" />&amp;c=<xsl:value-of select="to/latitude" />&amp;d=<xsl:value-of select="to/longitude" />&amp;e=<xsl:value-of select="routename" /></xsl:attribute><xsl:attribute name="style"> 
       <xsl:text> text-decoration:none; color:black;</xsl:text> 
      </xsl:attribute> 
      <xsl:value-of select="routename" /> 
     </a> 
    </xsl:element> 

您可以在列表中看到的風格我申請最後孩子到了最後,我現在這是錯誤的,但我想不出另一種方式來做到這一點。我還可以問,這是使用XSLT文件應用樣式的正確方法嗎?

回答

1

你應該嘗試像:

<xsl:attribute name="style"> 
     <xsl:text>list-style-type:none; width:120px; margin:0 auto; margin-top: 20px; border-bottom: 1px solid black; text-align:center;</xsl:text> 
     <xsl:if test="position() = last()">border: none;</xsl:if> 
</xsl:attribute> 

您還可以使用,而不是使用內嵌樣式的CSS類。在這種情況下,您應該可以使用CSS中的最後一個子選擇器(但是,此選擇器在IE7,IE8(http://caniuse.com/#search=%3Alast-child)中不受支持。

更新1:如果存在文本節點,上述操作將不起作用與路徑要素,所以不同的方法是使用最後一個元素不同的模板

<xsl:template match="route[last()]"> 
    <!-- Special behavior for last element --> 
</xsl:template> 

更新2:使用if語句,而忽略所有比路線不同的節點的另一種選擇是:

<xsl:if test="not(following-sibling::route)">border:none;</xsl:if> 
+0

感謝您的回答,我三編輯你給我的代碼,它會從所有列表元素中刪除邊框,而不僅僅是底部元素。所以我仍然可以鏈接到XSLT文件中的外部CSS文件呢?謝謝! – deucalion0 2013-02-17 10:49:05

+0

是的,你只需要在你的頭文件上使用鏈接標籤,就像你通常在HTML文件中做的那樣。 – 2013-02-17 10:54:01

+0

我不知道爲什麼上面的代碼不工作沒有源代碼(可能與路由標記之間的文本節點有關)。如果您發佈其餘代碼,我可以修復我發佈的代碼。 – 2013-02-17 10:56:12

相關問題