2017-01-04 118 views
0

有沒有方法可以確定根級節點是否包含任何子節點? 我有建立起一個下拉菜單,導航菜單的代碼文件,但對於具有低於他們沒有節點的根節點我想不同的模板適用於他們:xsl檢查根節點是否包含任何子節點

<?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="/Home"> 
     <xsl:apply-templates select="root" /> 
    </xsl:template> 

    <xsl:template match="root"> 

     <ul class="nav navbar-nav"> 
      <xsl:apply-templates select="node"> 
       <xsl:with-param name="level" select="0"/> 
      </xsl:apply-templates> 
     </ul> 

    </xsl:template> 


    <xsl:template match="node"> 
     <xsl:param name="level" /> 
     <xsl:choose> 
      <xsl:when test="$level=0"> 
       <li> 
        <xsl:attribute name="class"> 
         <xsl:if test="@breadcrumb = 1">active</xsl:if> 
         <xsl:if test="node"> 
          <xsl:text>&#32;dropdown</xsl:text> 
         </xsl:if> 


        </xsl:attribute> 
        <xsl:choose> 
         <xsl:when test="@enabled = 1"> 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
           <xsl:attribute name="class"> 
            <xsl:if test="node"> 
             <xsl:text>dropdown-toggle</xsl:text> 
            </xsl:if> 
           </xsl:attribute> 
           <xsl:if test="node"> 
            <xsl:attribute name="data-toggle">dropdown</xsl:attribute> 
           </xsl:if> 
           <xsl:value-of select="@text" /> 
           <xsl:if test="node"> 
            <b class="caret"></b> 
           </xsl:if> 
          </a> 
         </xsl:when> 
         <xsl:otherwise> 

          <xsl:value-of select="@text" /> 

         </xsl:otherwise> 
        </xsl:choose> 
        <xsl:if test="node"> 
         <ul class="dropdown-menu"> 
          <xsl:apply-templates select="node"> 
           <xsl:with-param name="level" select="$level + 1" /> 
          </xsl:apply-templates> 
         </ul> 
        </xsl:if> 
       </li> 
      </xsl:when> 
      <xsl:otherwise> 
       <li> 
        <xsl:attribute name="class"> 
         <xsl:if test="@breadcrumb = 1">active</xsl:if> 
         <xsl:if test="node"> 
          <xsl:text>&#32;dropdown</xsl:text> 
         </xsl:if> 
        </xsl:attribute> 
        <xsl:choose> 
         <xsl:when test="@enabled = 1"> 
          <a href="{@url}"> 
           <xsl:value-of select="@text" /> 
          </a> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="@text" /> 
         </xsl:otherwise> 
        </xsl:choose> 
       </li> 
       <xsl:if test="node"> 
        <!-- no extra level in default bootstrap --> 
       </xsl:if> 
      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 
+0

怎麼樣的功能數量,並可以發佈一個最小的XML? – user993553

回答

3

你的問題不清楚。如果根節點沒有任何子節點,則XML文檔爲空。也許你的意思是根元素;會有這樣的一個元素,它很容易地看到,如果有任何的子節點使用:

test="/*/node()" 

xsl:ifxsl:when指令。


或者,你可以使用兩個模板 - 一個匹配與子節點根元素:

<xsl:template match="/*[node()]"> 

,一個用於另一種情況:

<xsl:template match="/*[not(node())]"> 
+0

我懷疑「根級節點」的OP意味着「元素名爲root」。 –

+0

@MichaelKay使用「level」這個詞與這種解釋衝突。 –

+0

我們都必須猜測...... –

0

您可以使用此XSLT -file:

<?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="/*[count(descendant::*) = 0]"> 
     No siblings 
    </xsl:template> 

    <xsl:template match="/*[count(descendant::*) > 0]"> 
     Has siblings 
    </xsl:template> 

</xsl:stylesheet> 

隨着從根元素中的一個或多個同胞的輸入XML文件這樣

<?xml version="1.0"?> 
<root> 
    <a /> 
</root> 

它將輸出「有兄弟姐妹」。

並用一個空的根標籤的輸入文件中像這樣

<?xml version="1.0"?> 
<root> 
</root> 

它會輸出「沒有兄弟姐妹」。

0

如果我理解正確的問題,嘗試添加模板規則

<xsl:template match="root[not(*)]"/> 
相關問題