ppp
ppp

Reputation: 303

how to get child nodes in xsl

here my code-

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="ArrayOfLinkEntity" name="bindLink">
  <ul>
   <xsl:for-each select="LinkEntity[ParentLinkId=0]">
     <li>
       <xsl:variable name="linkId" select="LinkId"/>
       <xsl:variable name="child" select="count(/ArrayOfLinkEntity/LinkEntity[ParentLinkId=$linkId])"/>
       <xsl:value-of select="$child"/>
       <xsl:choose>
         <xsl:when test="($child &gt; 0)">
           <a href="#"  data-flexmenu="flexmenu1" onclick="javascript:setPageLinkId({$linkId});">
             <xsl:value-of select="LinkTitle"/>
             <img src="../images/down.gif" border="0"/>
           </a>
         </xsl:when>

         <xsl:otherwise >
           <a href="#" onclick="javascript:setPageLinkId({$linkId});">
             <xsl:value-of select="LinkTitle"/>
           </a>
         </xsl:otherwise>
       </xsl:choose>
     </li>
   </xsl:for-each>
  </ul>
 </xsl:template>
</xsl:stylesheet>

but I am getting $child=0 always.but there exists children. my xml structure-

<ArrayOfLinkEntity xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <LinkEntity>
    <EntityId>00000000-0000-0000-0000-000000000000</EntityId> 
    <LinkId>1</LinkId> 
    <SequenceNo>1</SequenceNo> 
    <ParentLinkId>0</ParentLinkId> 
    <LinkTitle>Home</LinkTitle> 
    <SubLink />
  </LinkEntity> ...  
</ArrayOfLinkEntity>

What should I do? Please suggest.

Upvotes: 0

Views: 535

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

but I am getting $child=0 always.but there exists children.

If by "children" you mean a LinkEntity with ParentLinkId child that is equal to the LinkId of the current node, the result you get is correct.

The only LinkEntity has an LinkId 1, but there are no LinkEntity elements in the provided XML document whose ParentLinkId is 1.

You need to show a complete (but the shortest possible) XML document on which your code exhibits this issue. Without being able to repro the problem, no one can give you a logical advice.

Upvotes: 1

Zkoh
Zkoh

Reputation: 2942

From your following code:

<xsl:variable name="linkId" select="LinkId"/>
      <xsl:variable name="child" select="count(/ArrayOfLinkEntity/LinkEntity[ParentLinkId=$linkId])"/>

This occurs within a for-each loop where the only nodes being looped over are LinkEntity's with ParentLinkId = 0. But from your source XML, the value of LinkId = 1, and in your variable assignment for $child, you are selecting on LinkEntity's with ParentLinkId = 1, which don't exist in your source XML data.

If I misunderstood something please let me know, but from what I can see that may be the problem.

Upvotes: 0

Related Questions