2011-08-22 42 views
0

我很努力地創建一個列表,以顯示父節點('文件夾')屬性'摺疊'爲'是'或'沒有'。 結果應僅顯示列表的前兩個級別,而不顯示如下所示的第三個級別。XSLT:父節點的屬性條件下的輸出列表

  • 1st。級別:顯示
  • 2nd。級別:顯示
  • 3rd。等級:無顯示

的想法是與<xsl:if test="not(parent::yes)">檢查「folder'屬性<folder folded="yes">。 這應該有資格獲得第三名。級別不顯示,但不知何故,它不會做我想做的事情。 我可能使用錯誤的構造和/或語法。 援助非常感謝,謝謝。

一些內容的XML結構:

<xbel> 
    <folder folded="yes"> 
     <level>1</level> 
     <title>bookmarks</title> 
     <desc>my bookmarks</desc> 
     <folder folded="no"> 
      <level>2</level> 
      <title>Android</title> 
      <desc>my Android</desc> 
      <bookmark href="http://www.phonesreview.co.uk/"> 
       <title>HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend</title> 
       <desc>The new HTC Sync 3.0.5422 update will be most welcome...</desc> 
      </bookmark> 
      <folder folded="no"> 
       <level>3</level> 
       <title>Apps</title> 
       <desc>Android Apps</desc> 
       <bookmark href="http://www.androidzoom.com/"> 
        <title>Android Communication Apps</title> 
        <desc>Download Communication Apps for Android.</desc> 
       </bookmark> 
       <bookmark href="http://www.htc.com/"> 
        <title>HTC - Android</title> 
        <desc>Apps for HTC-Android.</desc> 
       </bookmark> 
      </folder> 
     </folder> 
    </folder> 
</xbel> 

的XSLT:

<!--creates a nested list of elements named 'folder'--> 
<xsl:template match="folder" mode="linklist"> 
    <li> 
     <xsl:if test="folder/level = 2"> 
       Level:<xsl:value-of select="level"/>/
       Title:<xsl:value-of select="title"/>/
       Desc:<xsl:value-of select="desc"/> 
     <ul> 
      <xsl:apply-templates mode="linklist" /> 
     </ul> 
     </xsl:if> 
    </li> 
</xsl:template> 

<xsl:template match="bookmark" mode="linklist"> 
    <li> <!-- this bookmark is just another item in the list of bookmarks --> 
     <!-- the title --> 
      <a rel="nofollow" href="{@href}"><xsl:value-of select="title"/></a> 
     <!-- the description --> 
     <xsl:if test="desc"> 
      <span class="bookmarkDesc"> 
       <xsl:value-of select="desc"/> 
      </span> 
     </xsl:if> 
    </li> 
</xsl:template> 

樣式表HTML

<body> 

<ul> 
    <xsl:apply-templates mode="linklist" /> 
</ul> 

</body> 

所生成的輸出(級別:1-3)

Level:1/Title:bookmarks/Desc:my bookmarks 
     Level:2/Title:Android/Desc:my Android 
      HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend ... 
      Level:3/Title:Apps/Desc:Android Apps 
       Android Communication AppsDownload Communication Apps for Android. 
       HTC - AndroidApps for HTC-Android. 

的預期輸出:(級別:1-2)

Level:1/Title:bookmarks/Desc:my bookmarks 
     Level:2/Title:Android/Desc:my Android 
      HTC Sync 3.0.5422 Update: Aria, Desire, Hero, Legend ... 

我嘗試這樣做模板,但其輸出所述最後兩個節點,我需要兩個第一個節點。

<xsl:template match="folder[parent::folder/@folded = 'yes']" mode="linklist"> 

回答

1

最簡單的變化,你可以做,以防止展開folder元素的處理是添加燕子他們空的模板(即不產生輸出):

<xsl:template match="folder[@folded='no']" mode="linklist"/> 

所有folder元素沒有一個folded屬性等於no將使用您現有的模板進行處理;那些做的將被這個新的捕獲。

相反,如果你想處理具有每個folder元素或者自己的folded屬性等於yes或其父的(如在所更新XML實例),然後用下面的模板:

<xsl:template match="folder[@folded='yes' or ../@folded='yes']" mode="linklist"> 
    <!-- body elided --> 
</xsl:template> 

你可能還需要包含一個空的模板,用於隱藏所有其他folder元素:

<xsl:template match="folder" mode="linklist" /> 
+0

謝謝:請注意第二個。要顯示節點具有屬性'@摺疊=「no''及其父僅具有屬性'@摺疊=」 yes'' – snahl

+0

@snahl - 它沒有在我回答的時間。您已編輯。一旦理解了這些變更的要求,我就可以更新我的答案。您只想輸出「摺疊」屬性爲「是」或其父文件夾具有「摺疊」屬性等於「是」的文件夾。是對的嗎? –

+0

正確,我在閱讀您的回覆後編輯了XML。目標是僅輸出那些'父文件夾'具有摺疊屬性等於'是'的文件夾。 – snahl