Subject: RE: Problem for : increment a variable in a for-each?
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Mon, 29 Nov 2004 08:49:25 -0000
|
I don't fully understand your problem, and the code layout has become
unreadable. But I would think you can solve the problem using the functions
position() and last().
position() gives the position of a node in the sequence of nodes currently
selected by xsl:for-each or xsl:apply-templates, or being filtered by a
predicate. last() gives the number of nodes in such a sequence.
position()=last() is true if the current node is the last one.
position()=last()-1 is true if it is the last but one.
Sometimes you need to know the absolute position of a node in the source
tree, regaredless of the processing order. In such cases you can use
constructs such as xsl:number or count(preceding-sibling::*).
Michael Kay
http://www.saxonica.com
> -----Original Message-----
> From: que Li [mailto:queincanada@xxxxxxxx]
> Sent: 29 November 2004 04:33
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: RE: Problem for : increment a variable in a for-each?
>
> Hi Michael:
> Thans for help. In actually I found It is good for
> me to use the xpath to find the logical chilld node to
> format them.
>
>
> But I have last probelm:
> How can I get the current node position on the special
> list?
>
> Example:
> <Subject_List>
> <Subject_ID>
> <Subject_Title>
> <Subject_Parent_ID>
> </Subject_List>
> There have three type of node
>
>
> Type A; (Logical parent node)
> Subject_Parent_ID =1
>
> Type B: (Logical child node and its logical parent
> node exist)
>
> Type C: (Logical child node and its logical parent
> node do not exist)
> Subject_Parent_ID not eauql 1 but the logical parent
> node exist
>
> my question:
> If the current node is belong to type A or type B. How
> I know It is position for typeA and typeB list.
>
>
> my XSL file:
> <xsl:template match="SubjectLists">
> <xsl:apply-templates />
> </xsl:template>
> <xsl:template match="Subject_List" >
> <xsl:choose>
> <xsl:when test="Subject_Parent_ID=1">
> <xsl:choose>
>
> <xsl:when
> test="not(preceding-sibling::Subject_List)">
>
> <b><xsl:value-of select="Subject_Title"/></b>
>
> </xsl:when>
>
> <xsl:when
> test="not(following-sibling::Subject_List)">
>
> <b> ;<xsl:value-of
> select="Subject_Title"/></b>
>
> </xsl:when>
>
> <xsl:otherwise>
>
> <b> and <xsl:value-of
> select="Subject_Title"/></b>
>
> </xsl:otherwise>
> </xsl:choose>
> <xsl:call-template
> name="findChildNode">
>
> <xsl:with-param name="ParentID"
> select="Subject_ID"/>
> </xsl:call-template>
>
> </xsl:when>
> <xsl:otherwise>
> <xsl:call-template
> name="findParentNode">
> <xsl:with-param
> name="ID" select="Subject_Parent_ID"/>
> </xsl:call-template>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:template>
> <xsl:template name="findChildNode">
> <xsl:param name="ParentID"/>
> <xsl:for-each
> select="../Subject_List[Subject_Parent_ID=$ParentID]">
> <xsl:choose>
> <xsl:when
> test="position()=1 and last() >2 ">
>
> <b> ( <xsl:value-of
> select="Subject_Title"/></b>
> </xsl:when>
> <!--only one-->
>
> <xsl:when test="position()=1 and last()=1">
>
> <b> ( <xsl:value-of
> select="Subject_Title"/> )</b>
> </xsl:when>
> <!--only two-->
> <xsl:when
> test="position()=1 and
> last()=2 ">
>
> <b> ( <xsl:value-of
> select="Subject_Title"/> </b>
> </xsl:when>
> <xsl:when
> test="position()!=1 and
> position()=last()">
> <b> and
> <xsl:value-of select="Subject_Title"/> )</b>
>
> </xsl:when>
> <xsl:otherwise>
> <b> ; <xsl:value-of
> select="Subject_Title"/></b>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:for-each>
> </xsl:template>
> <xsl:template name="findParentNode" >
> <xsl:param name="ID"/>
> <xsl:if
> test="count(../Subject_List[Subject_ID=$ID]) = 0">
> <xsl:choose>
>
> <xsl:when
> test="not(preceding-sibling::Subject_List)">
>
> <b><xsl:value-of select="Subject_Title"/></b>
>
> </xsl:when>
>
> <xsl:when
> test="not(following-sibling::Subject_List)">
>
> <b> ;<xsl:value-of
> select="Subject_Title"/></b>
>
> </xsl:when>
>
> <xsl:otherwise>
>
> <b> and <xsl:value-of
> select="Subject_Title"/></b>
> </xsl:otherwise>
> </xsl:choose>
> </xsl:if>
> </xsl:template>
> XML file:
> <root>
> <SubjectLists>
> <Subject_List>
> <Subject_ID>31</Subject_ID>
> <Subject_Title>Biology</Subject_Title>
> <Subject_Parent_ID>30</Subject_Parent_ID>
> </Subject_List>
> <Subject_List>
> <Subject_ID>32</Subject_ID>
> <Subject_Title>Chemistry</Subject_Title>
> <Subject_Parent_ID>30</Subject_Parent_ID>
> </Subject_List>
> <Subject_List>
> <Subject_ID>24</Subject_ID>
> <Subject_Title>Legal Studies</Subject_Title>
> <Subject_Parent_ID>1</Subject_Parent_ID>
> </Subject_List>
> <Subject_List>
> <Subject_ID>17</Subject_ID>
> <Subject_Title>Music</Subject_Title>
> <Subject_Parent_ID>14</Subject_Parent_ID>
> </Subject_List>
> <Subject_List>
> <Subject_ID>30</Subject_ID>
> <Subject_Title>Science</Subject_Title>
> <Subject_Parent_ID>1</Subject_Parent_ID>
> </Subject_List>
> <Subject_List>
> <Subject_ID>18</Subject_ID>
> <Subject_Title>Visual Arts</Subject_Title>
> <Subject_Parent_ID>14</Subject_Parent_ID>
> </Subject_List>
> </SubjectLists>
> </root>
>
>
>
> Now the result is:
>
> and Legal Studies and Music and Science ( Biology and
> Chemistry ) ;Visual Arts
>
>
> the correct result should be:
>
> Legal Studies ; Music ; Science ( Biology and
> Chemistry ) and Visual Arts
>
>
> Thanks
> Helena
>
>
> ______________________________________________________________
> ________
> Post your free ad now! http://personals.yahoo.ca
|