xsl-list
[Top] [All Lists]

Re: Problem for : increment a variable in a for-each?

2004-11-26 02:33:02
Hello, 

may be I'm a bit clueless, but I can't figure out why you do all the
counting stuff. Have you tried  XPath expressions with predicates? I
guess, the following does what you are looking for.

Markus

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
        <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
        <xsl:template match="/">
                                <xsl:apply-templates/>
        </xsl:template>
<!-- the last one has to be <strong/> -->
        <xsl:template match="list[position() =last() and ParentID = '21']">
                <strong>
                        <xsl:value-of select="Title"/>
                </strong>
        </xsl:template>
<!-- the second one has to be <italic/> -->
        <xsl:template match="list[position() = '2' and ParentID = '21']">
                <italic>
                        <xsl:value-of select="Title"/>
                </italic>
        </xsl:template>
<!-- and another matcher... -->
        <xsl:template match="list[position() !=last() and ParentID = '21']">
                <whatever>
                        <xsl:value-of select="Title"/>
                </whatever>
        </xsl:template>
</xsl:stylesheet>



On Thu, 25 Nov 2004 22:52:07 -0500 (EST), que Li 
<queincanada(_at_)yahoo(_dot_)ca> wrote:
Hi:

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 ?

My code

<xsl:template match="root">

<xsl:for each select="list">

<xsl:if test="parented =1">

<xsl:call-template name= ¡°findChildNode¡±>

  <xsl:with-param name=¡±ID¡± select="ID¡±/>

</xsl:call-template>

</xsl:for-each>

</xsl:template>

<xsl:template name="findChildNode¡±>

<xsl:param name="ID¡±>

<xsl:param name="count¡± select="0¡±/>

<xsl:for-each select="../list¡±>

   <xsl:if test="parentID=$ID¡±>

    <xsl:call-template  name="formatChild¡±>

<xsl:with-param name="count" select="$count+1¡±/>

   </xsl:if>

</xsl:for each>

</xsl:template>

<xsl:template name="formatChid¡±>

   <xsl:param name="count¡±>

<xsl:variable name="TotalChildCount¡± select=¡±?¡±/>
(Note: I don't know how I can get all child node count
based on the one parentID)

            <xsl:choose>

<xsl:when  test="$count=1¡±>do firsth thing</xsl:when>
<xsl:when  test="$count &gt; 1 and $count &lt;
$TotalChildCount¡±>do second  thing</xsl:when>

<xsl:when  test="$count=$TotalCount¡±>do third
thing</xsl:when>

          </xsl:choose>

</xsl:template>

Question:

 1.   Why every time the $count is 0 and never
incresement  ?
2.   How I can get the total child count value based
on the parent ID?

Thanks for any help
Helena

______________________________________________________________________
Post your free ad now! http://personals.yahoo.ca

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>