English 中文(简体)
Test for Descendant Items
原标题:

I am trying to create a simple inventory of items in a Sitecore website using an xslt. The problem is that I can t find a way to test whether an item has descendant items beneath it.

It s easy to get the top level set like this:

<xsl:template match="*" mode="main">
<table width="100%" class="alternating">
   <xsl:for-each select="./item">
        <tr>
 <td width="100px"><sc:image field="Image" mh="100" mw="100" /></td>
<td style="vertical-align:top"><h2><sc:text field="Title"/></h2></td>
<td style="vertical-align:top"><xsl:value-of select="sc:path(.)" /></td>
        </tr>
   </xsl:for-each>
</table>
</xsl:template>

This creates a nice, flat table of the main image, the title and the path of each item just below where you start. The problem is that I can’t find a way to easily test whether one of these items has descendants. That would make the code look something like this:

<xsl:template match="*" mode="main">
<table width="100%" class="alternating">
   <xsl:for-each select="./item">
        <tr>
 <td width="100px"><sc:image field="Image" mh="100" mw="100" /></td>
<td style="vertical-align:top"><h2><sc:text field="Title"/></h2></td>
<td style="vertical-align:top"><xsl:value-of select="sc:path(.)" /></td>
<td>
    <!—test whether the item has descendants -->
    <xsl:if test=?????>
        <!—loop through descendant items -->
        <xsl:for-each select="./item">
        Render information about each descendant item
        </xsl:for-each>
    </xsl:if>
</td>
        </tr>
   </xsl:for-each>
</table>
</xsl:template>
问题回答

There is no need to test for descendents. Just use:

...
<td>        
<!-- loop through descendant items if any -->        
<xsl:for-each select="./item">        
<!-- Render information about each descendant item -->      
</xsl:for-each>    
</xsl:if>
</td>
...

If there are no descendents nothing will be output for that node.

Rashmi is correct, the for-each should not execute if there are no children.

But incidentally, just to answer the question, you can perform a test

<xsl:if test="count(item) != 0">

You might want to test for descendants if you are rendering a list and don t want the wrapping tags when that aren t any descendants then you may want the if:

<xsl:if test="count(./item) &gt; 0">
  <ul>
    <xsl:for-each select="./item">
      <li>
         <!-- content here -->
      </li>
    </xsl:for-each>
  </ul>
</xsl:if>




相关问题
When test hanging in an infinite loop

I m tokenising a string with XSLT 1.0 and trying to prevent empty strings from being recognised as tokens. Here s the entire function, based on XSLT Cookbook: <xsl:template name="tokenize"> ...

quick xslt for-each question

Let s say I have an XML document that has this: <keywords> <keyword>test</keyword> <keyword>test2</keyword> <keyword>test3</keyword> <keyword>test4</...

XSLT Transform XML with Namespaces

I m trying to transform some XML into HTML using XSLT. Problem: I can t get it to work. Can someone tell me what I m doing wrong? XML: <ArrayOfBrokerage xmlns:i="http://www.w3.org/2001/...

XSLT output to HTML

In my XSLT file, I have the following: <input type="button" value= <xsl:value-of select="name">> It s an error as it violates XML rule. What I actually want is having a value from an ...

Mangling IDs and References to IDs in XML

I m trying to compose xml elements into each other, and the problem I am having is when there s the same IDs. Basically what I need to do is mangle all the IDs in an xml file, as well as the ...

Sharepoint 2007 Data view Webpart custom parameters

I m sort of new to the custom parameters that can be setup on a DataView Webpart. There are 6 options: - None - Control - Cookie - Form - QueryString - Server Variable I think that None, Cookie and ...

热门标签