2013-05-16 137 views
0

我有以下HTML XSL轉換工作集類:XSLT:基於元素子孫

<div id="context"> 
    <p>Sometimes, there is content here.</p> 
</div> 

<div id="main-content"> 
    <p>There is always content here.</p> 
</div> 

<div id="related"> 
    <img src="CMS PREVIEW ICON - ADMIN ONLY"/> 
    <p>Sometimes, there is content here.</p> 
    <p>The image is always the first child only if the user is inside the CMS, but it should be ignored if there is not other content present.</p> 
</div> 

目前,我正在試圖調整對main-content DIV class屬性和related DIV ,根據related是否有任何後代(不是CMS圖標)。下面是我有:

<xsl:template match="div[@id='main-content']"> 
    <xsl:copy> 
     <!-- copy the current body node contents --> 
     <xsl:attribute name="class"> 
      <xsl:choose> 
       <xsl:when test="//div[@id='related']/descendant::* and name(//div[@id='related']/*[1]) != 'img' or count(//div[@id='related']/descendant::* > 1) and name(//div[@id='related']/*[1]) != 'img'">span6</xsl:when> 
       <!-- left nav but no right col --> 
       <xsl:when test="not(//div[@id='related']/descendant::*) or (count(//div[@id='related']/descendant::* = 1) and name(//div[@id='related']/*[1]) = 'img')">span9</xsl:when>      
       <!-- no left nav and populated right col --> 
       <xsl:when test="//div[@id='related']/descendant::* and (count(//div[@id='related']/descendant::* = 1) and name(//div[@id='related']/*[1]) != 'img') and not(//div[@class='data-entry wide'])">span9</xsl:when>      
       <xsl:otherwise>span12</xsl:otherwise> 
      </xsl:choose> 
     </xsl:attribute> 
     <xsl:apply-templates select="@*|node()"/> 
     <!-- output the rest --> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="div[@id='related']"> 
    <xsl:copy> 
     <!-- copy the current body node contents --> 
     <xsl:attribute name="class"> 
      <xsl:choose> 
       <xsl:when test="count(* = 0) or (count(* = 1) and name(*[1]) = 'img')">hidden</xsl:when> 
       <xsl:when test="descendant::*">span3</xsl:when> 
       <xsl:otherwise>hidden</xsl:otherwise> 
      </xsl:choose> 
     </xsl:attribute> 
     <xsl:apply-templates select="@*|node()"/> 
     <!-- output the rest --> 
    </xsl:copy> 
</xsl:template> 

然後,如果related給出一類的隱藏,我後來刪除它,以便

我認爲這是不佔用帶寬,DOM空間等。會正常工作,因爲它似乎在xpath中獲得正確的值,但它不會像它應該那樣去除元素。這是一個有點怪異,因爲我需要知道:

  1. 是否有不在裏面的CMS
  2. 並且對於CMS內的意見,是否有後裔不屬於意見related任何後代具體圖像(其他圖像將始終包裹在div,鏈接等中)

有什麼想法?

感謝, 喬納森

+0

一方面,'count(* = 0)'是無效的XPath。 'count(*)= 0'是做這件事的有效方法,但是最好使用'not(*)'。類似地,對於count(* = 1),你可以使用'not(* [2])'。我在解密你的第一套''時遇到了一些麻煩。你能爲我們打破他們嗎? – JLRishe

回答

2

即使我不能完全肯定,如果我有你的要求的正確的理解,我給它一個嘗試。

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 

     <xsl:template match="div[@id='main-content']"> 
    <xsl:copy> 
     <!-- copy the current body node contents --> 
     <xsl:attribute name="class"> 
      <xsl:choose> 
       <!-- "related" has any descendants (that are not the CMS icon). --> 
       <xsl:when test="//div[@id='related']/* and 
          count(//div[@id='related']/img) != count(//div[@id='related']/*) ">span6</xsl:when> 
       <xsl:otherwise>span9</xsl:otherwise> 
      </xsl:choose> 
     </xsl:attribute> 
     <xsl:apply-templates select="@*|node()"/> 
     <!-- output the rest --> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="div[@id='related']"> 
    <xsl:copy> 
     <!-- copy the current body node contents --> 
     <xsl:apply-templates select="@*"/> 
     <xsl:choose> 
       <!-- it should be ignored if there is not other content present --> 
       <xsl:when test=" count(img) = count(*)"> 
        <xsl:attribute name="class">hidden</xsl:attribute> 
       </xsl:when> 
       <xsl:otherwise> 
        <xsl:attribute name="class">span3</xsl:attribute> 
        <xsl:apply-templates select="node()"/> 

       </xsl:otherwise> 
      </xsl:choose> 
     <!-- output the rest --> 
    </xsl:copy> 
</xsl:template> 

    <xsl:template match="node() | @*"> 
     <xsl:copy> 
      <xsl:apply-templates select="node() | @*" /> 
     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 
+0

這對我來說非常有效;非常感謝。 –