RE: [xsl] Problem for : increment a variable in a for-each?

Subject: RE: [xsl] Problem for : increment a variable in a for-each?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Sat, 27 Nov 2004 09:46:52 -0000
> But I still can't figure out how I can format the
> child node since I can't get how many child node.
> example for the ID=21 ,It has three child (which ID
> are 22,23,24). sorry that I am so poor skill for xsl
> language.

Your description confused me at first because you are using "parent" and
"child" to describe logical relationships in your data, not the XML parent
and child axes. But in fact the way you traverse a logical tree using XSLT
is very similar to the way you traverse the physical tree: the difference is
that when you call <xsl:apply-templates, you select the logical children
rather than the physical children.

The best way to do this is with keys. Define a key like this:

<xsl:key name="k" match="list" use="ParentId"/>

then when a <list> element is the context node, you can process its logical
children using

<xsl:apply-templates select="key('k', ID)"/>

Michael Kay
http://www.saxonica.com/
 
> What I need do:
> Format the parent node title (Which parentID is 1 and
> ID is 21) then
> find all child ID which parent_ID is 21 then format
> the child ID based on there position.If the first
> child
> then I need do format 1,if the last child then I do
> format 2,If not first and last one then I format3
> 
> My code:
>  <xsl:template match="Lists"> 
>     <xsl:apply-templates/>
>  </xsl:template>
> 
>  <xsl:template match="List" >
>     <xsl:choose>
>        <xsl:when test="Parent_ID = 1">
>          <!--parent Node-->
> 	<xsl:variable name="ID" select="Subject_ID"/>
> 	<xsl:choose>
> 	<xsl:when test="position()=1">
> 	 <b><xsl:value-of select="Title"/></b>
> 	</xsl:when>
> 	<xsl:when test="position() = last()">
> 	<b> and <xsl:value-of select="Title"/></b>
> 	</xsl:when>
> 	<xsl:when test="position() &gt;1 and position()
> &lt;last() ">
> 	<b>; <xsl:value-of select="Title"/></b>
>       </xsl:when>
> 	</xsl:choose>	
> 	<!--need find child node and format child node
> title-->
> <!--??? How I can get know how many child node for
> this current node-->
>        <xsl:for-each select="../List">
>           <xsl:choose>
>           <xsl:when test="Parent_ID=$ID">
>            <b><xsl:value-of select="Title"/></b>      
>                                        </xsl:when>
> </xsl:choose>							
> 					
> Thanks
> Helena	
> 								
> 					</xsl:for-each>
> 								
> 			</xsl:when>
> 								
> 	<xsl:otherwise>
> 								
>    <!--parentID not1 should be child-->
> 								
> 	</xsl:otherwise> 
> 								
> </xsl:choose>
> </xsl:template>
> 
> 
> 
> 
>  --- Andrew Welch <ajwelch@xxxxxxxxxxxxxxx> wrote: 
> > 
> > 
> > > I found several people asked the same question
> > with me
> > > "Re: [xsl] how to increment a variable in a
> > for-each
> > > loop" but it really didn't resolve my problems. So
> > > just wonder could I get help ? I spent lots of
> > time on
> > > it and can't get solution!
> > > 
> > > My xml file
> > > <root> 
> > > <list>
> > >     <ID>21</ID>
> > >    <Title>text1</Title>
> > >    <ParentID>1<ParentID>
> > > </list>
> > > <list>
> > >     <ID>22</ID>
> > >    <Title>text2</Title>
> > >    <ParentID>21<ParentID>
> > > </list>
> > > 
> > > <list>
> > >     <ID>23</ID>
> > >    <Title>text3</Title>
> > >    <ParentID>21<ParentID>
> > > </list>    
> > > 
> > > <list>
> > >     <ID>24</ID>
> > >    <Title>text4</Title>
> > >    <ParentID>21<ParentID>
> > > </list>  
> > > 
> > > <list>
> > >     <ID>25</ID>
> > >    <Title>text5</Title>
> > >    <ParentID>1<ParentID>
> > > </list>  
> > > 
> > > </root>
> > > 
> > > What I want do: I need do different thing  for the
> > 
> > > node which  parentID is 21(by passing in
> > )(exampel:If
> > > it is first child then I need bold. if not first
> > and
> > > not last one then I need do second thing. If the
> > node
> > > is last child then I need do third things.
> > > 
> > > I try to use the call:template and xsl:for each to
> > > pass the parameter but the count is always set to
> > > initial since I run so many for each 
> > > what I shoud do ?
> > 
> > Try the following stylesheet, hopefully it's clear
> > enough.  A good rule
> > of thumb - always use templates!  If you are new to
> > the language, using
> > xsl:for-each will tempt you into the wrong way of
> > thinking, whereas
> > using templates forces you to understand whats going
> > on in the
> > background (although it seems completely wrong to
> > start with :)
> > 
> > <xsl:stylesheet version="1.0"
> >  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> > 
> > <xsl:param name="ID" select="'21'"/>
> > 
> > <xsl:template match="/">
> > 	<xsl:apply-templates/>
> > </xsl:template>
> > 
> > <xsl:template match="list">
> > 	<xsl:choose>
> > 		<xsl:when test="ID = $ID and
> > not(preceding-sibling::list)">
> > 			<!-- I'm equal to 21 and at the start of the
> > list -->
> > 		</xsl:when>
> > 	    <xsl:when test="ID = $ID and
> > not(following-sibling::list)">
> > 			<!-- I'm equal to 21 and at the end of the list
> > -->
> > 	    </xsl:when>
> > 	    <xsl:when test="ID = $ID">
> > 			<!-- I'm equal to 21 and in the middle of the
> > list -->
> > 	    </xsl:when>
> > 	    <xsl:otherwise>
> > 			<!-- I'm not equal to 21! -->
> > 	    </xsl:otherwise>
> > 	</xsl:choose>
> > </xsl:template>
> > 
> > </xsl:stylesheet>
> > 
> > 
> > cheers
> > andrew
> > 
> >  
> 
> ______________________________________________________________
> ________ 
> Post your free ad now! http://personals.yahoo.ca

Current Thread